84612024-01-16 20:57:29anonMaximális kaktusz (45 pont)cpp17Accepted 45/4534ms12692 KiB
#include <bits/stdc++.h>
#define HANDLE_STREAK \
    if(streak >= 3) { \
        ans++; \
        streak = 0; \
    }
#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 rem;
    vis[vertex] = true;
    vector<ll> nbs;
    for(const auto &x : graph[vertex]) {
        if(!vis[x])
            nbs.push_back(x);
    }
    if(nbs.empty()) {
        HANDLE_STREAK;
        return streak;
    }
    else if(nbs.size() == 1) {
        HANDLE_STREAK;
        return dfs(nbs[0], streak + 1, ans, vis, graph);
    }
    else if(nbs.size() >= 2) {
        vector<ll> rs;
        for(const auto &x : nbs) {
            rem = dfs(x, 1, ans, vis, graph);
            if(rem)
                rs.push_back(rem);
        }
        if(rs.empty()) {
            HANDLE_STREAK;
            return streak;
        }
        else if(rs.size() == 1) {
            HANDLE_STREAK;
            ans += (streak + rs[0]) / 3;
            return (streak + rs[0]) % 3;
        }
        else if(rs.size() >= 2) {
            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
base45/45
1Accepted0/03ms1828 KiB
2Accepted0/03ms2220 KiB
3Accepted1/13ms2268 KiB
4Accepted1/13ms2484 KiB
5Accepted1/13ms2568 KiB
6Accepted2/23ms2692 KiB
7Accepted2/23ms2944 KiB
8Accepted2/23ms2920 KiB
9Accepted1/13ms3124 KiB
10Accepted1/13ms3204 KiB
11Accepted1/13ms3296 KiB
12Accepted1/13ms3296 KiB
13Accepted1/13ms3428 KiB
14Accepted1/13ms3612 KiB
15Accepted1/13ms3740 KiB
16Accepted1/13ms3648 KiB
17Accepted1/13ms3936 KiB
18Accepted1/132ms9884 KiB
19Accepted1/126ms10200 KiB
20Accepted1/134ms9808 KiB
21Accepted1/132ms9912 KiB
22Accepted2/228ms12268 KiB
23Accepted2/223ms12080 KiB
24Accepted2/221ms12300 KiB
25Accepted2/221ms12372 KiB
26Accepted1/13ms4184 KiB
27Accepted1/13ms4244 KiB
28Accepted1/13ms4448 KiB
29Accepted1/127ms12492 KiB
30Accepted1/126ms12180 KiB
31Accepted1/126ms12668 KiB
32Accepted1/125ms12672 KiB
33Accepted2/227ms12692 KiB
34Accepted2/227ms12536 KiB
35Accepted2/224ms12672 KiB
36Accepted2/224ms12668 KiB