84402024-01-16 11:06:49anonMaximális kaktusz (45 pont)cpp17Wrong answer 24/4528ms9900 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)
            continue;
        detected = remove_cycles(x, vertex, bad_nodes, vis, graph);
        if(detected) {
            bad_nodes[vertex] = true;
            if(detected == vertex)
                return 0;
            else
                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
base24/45
1Accepted0/03ms2104 KiB
2Accepted0/03ms2108 KiB
3Accepted1/13ms2256 KiB
4Accepted1/13ms2336 KiB
5Accepted1/12ms2336 KiB
6Accepted2/23ms2468 KiB
7Accepted2/23ms2676 KiB
8Wrong answer0/22ms2724 KiB
9Wrong answer0/12ms2728 KiB
10Accepted1/13ms2732 KiB
11Wrong answer0/12ms2732 KiB
12Wrong answer0/13ms2888 KiB
13Wrong answer0/13ms2864 KiB
14Wrong answer0/13ms2864 KiB
15Wrong answer0/13ms3284 KiB
16Accepted1/13ms3392 KiB
17Wrong answer0/13ms3348 KiB
18Wrong answer0/128ms9292 KiB
19Accepted1/124ms9856 KiB
20Wrong answer0/128ms9164 KiB
21Wrong answer0/128ms9416 KiB
22Accepted2/220ms9320 KiB
23Wrong answer0/220ms9532 KiB
24Accepted2/219ms9648 KiB
25Accepted2/219ms9608 KiB
26Accepted1/13ms3636 KiB
27Wrong answer0/13ms3500 KiB
28Wrong answer0/13ms3556 KiB
29Accepted1/125ms9828 KiB
30Wrong answer0/125ms9900 KiB
31Wrong answer0/123ms9660 KiB
32Wrong answer0/123ms9856 KiB
33Wrong answer0/225ms9864 KiB
34Accepted2/224ms9864 KiB
35Accepted2/221ms9500 KiB
36Accepted2/221ms9484 KiB