84412024-01-16 11:18:27anonMaximális kaktusz (45 pont)cpp17Wrong answer 23/4529ms11288 KiB
#include <bits/stdc++.h>
#define FastIO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
using namespace std;
typedef long long ll;
ll remove_cycles(ll vertex, ll parent, vector<bool> &bad_nodes, vector<bool> &vis, const vector<vector<ll>> &graph) {
    ll detected, result;
    if(vis[vertex]) {
        bad_nodes[vertex] = true;
        return vertex;
    }
    vis[vertex] = true;
    result = 0;
    for(const auto &x : graph[vertex]) {
        if(x == parent || bad_nodes[x])
            continue;
        detected = remove_cycles(x, vertex, bad_nodes, vis, graph);
        if(detected) {
            bad_nodes[vertex] = true;
            if(detected != vertex)
                result = detected;
        }
    }
    return result;
}
ll dfs(ll vertex, ll streak, ll &ans, vector<bool> &vis, const vector<vector<ll>> &graph) {
    ll nc, rc, res;
    if(streak >= 3) {
        ans++;
        streak = 0;
    }
    vis[vertex] = true;
    nc = 0;
    for(const auto &x : graph[vertex])
        nc += !vis[x];
    rc = 0;
    for(const auto &x : graph[vertex]) {
        if(vis[x])
            continue;
        res = dfs(x, 1 + (nc == 1) * streak, ans, vis, graph);
        if(nc == 1)
            return res;
        else if(res)
            rc++;
    }
    if(nc >= 2 && rc >= 2 && streak) {
        ans++;
        return streak - 1;
    }
    return streak;
}
int main() {
    FastIO;
    ll i, u, v, ans, N, M;
    cin >> N >> M;
    vector<vector<ll>> graph(N + 1);
    for(i = 0; i < M; i++) {
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    vector<bool> vis(N + 1, false);
    vector<bool> bad_nodes(N + 1, false);
    remove_cycles(1, -1, bad_nodes, vis, graph);
    ans = 0;
    for(i = 1; i <= N; i++) {
        if(!bad_nodes[i])
            dfs(i, 1, ans, bad_nodes, graph);
    }
    cout << ans << '\n';
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base23/45
1Accepted0/03ms1824 KiB
2Accepted0/03ms2236 KiB
3Accepted1/13ms2128 KiB
4Accepted1/13ms2340 KiB
5Accepted1/13ms2424 KiB
6Accepted2/23ms2700 KiB
7Accepted2/23ms2908 KiB
8Wrong answer0/23ms3044 KiB
9Wrong answer0/13ms3132 KiB
10Accepted1/13ms3516 KiB
11Wrong answer0/13ms3504 KiB
12Wrong answer0/13ms3716 KiB
13Wrong answer0/13ms3800 KiB
14Wrong answer0/13ms3888 KiB
15Wrong answer0/13ms4208 KiB
16Accepted1/13ms4076 KiB
17Wrong answer0/13ms4348 KiB
18Wrong answer0/128ms10224 KiB
19Accepted1/125ms10676 KiB
20Wrong answer0/129ms10284 KiB
21Wrong answer0/129ms10368 KiB
22Accepted2/221ms10652 KiB
23Wrong answer0/221ms10688 KiB
24Accepted2/220ms10904 KiB
25Accepted2/220ms10676 KiB
26Accepted1/13ms4568 KiB
27Wrong answer0/13ms4632 KiB
28Wrong answer0/13ms4696 KiB
29Accepted1/125ms10972 KiB
30Accepted1/125ms11288 KiB
31Wrong answer0/123ms10984 KiB
32Wrong answer0/123ms11196 KiB
33Wrong answer0/225ms11208 KiB
34Wrong answer0/225ms11212 KiB
35Accepted2/221ms10908 KiB
36Accepted2/223ms10888 KiB