84612024-01-16 20:57:29anonMaximális kaktusz (45 pont)cpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base45/45
1Elfogadva0/03ms1828 KiB
2Elfogadva0/03ms2220 KiB
3Elfogadva1/13ms2268 KiB
4Elfogadva1/13ms2484 KiB
5Elfogadva1/13ms2568 KiB
6Elfogadva2/23ms2692 KiB
7Elfogadva2/23ms2944 KiB
8Elfogadva2/23ms2920 KiB
9Elfogadva1/13ms3124 KiB
10Elfogadva1/13ms3204 KiB
11Elfogadva1/13ms3296 KiB
12Elfogadva1/13ms3296 KiB
13Elfogadva1/13ms3428 KiB
14Elfogadva1/13ms3612 KiB
15Elfogadva1/13ms3740 KiB
16Elfogadva1/13ms3648 KiB
17Elfogadva1/13ms3936 KiB
18Elfogadva1/132ms9884 KiB
19Elfogadva1/126ms10200 KiB
20Elfogadva1/134ms9808 KiB
21Elfogadva1/132ms9912 KiB
22Elfogadva2/228ms12268 KiB
23Elfogadva2/223ms12080 KiB
24Elfogadva2/221ms12300 KiB
25Elfogadva2/221ms12372 KiB
26Elfogadva1/13ms4184 KiB
27Elfogadva1/13ms4244 KiB
28Elfogadva1/13ms4448 KiB
29Elfogadva1/127ms12492 KiB
30Elfogadva1/126ms12180 KiB
31Elfogadva1/126ms12668 KiB
32Elfogadva1/125ms12672 KiB
33Elfogadva2/227ms12692 KiB
34Elfogadva2/227ms12536 KiB
35Elfogadva2/224ms12672 KiB
36Elfogadva2/224ms12668 KiB