84542024-01-16 17:25:45anonMaximális kaktusz (45 pont)cpp17Hibás válasz 41/4532ms13140 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 rem;
    vis[vertex] = true;
    vector<ll> nbs;
    for(const auto &x : graph[vertex]) {
        if(!vis[x])
            nbs.push_back(x);
    }
    if(nbs.empty()) {
        if(streak >= 3) {
            ans++;
            streak = 0;
        }
        return streak;
    }
    else if(nbs.size() == 1) {
        if(streak >= 3) {
            ans++;
            streak = 0;
        }
        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.size() == 1) {
            if(streak >= 3) {
                ans++;
                streak = 0;
            }
            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
base41/45
1Elfogadva0/03ms1824 KiB
2Elfogadva0/03ms2228 KiB
3Elfogadva1/13ms2284 KiB
4Elfogadva1/13ms2360 KiB
5Elfogadva1/13ms2560 KiB
6Elfogadva2/23ms2772 KiB
7Elfogadva2/23ms3000 KiB
8Elfogadva2/23ms3196 KiB
9Elfogadva1/13ms3416 KiB
10Elfogadva1/13ms3636 KiB
11Elfogadva1/13ms3712 KiB
12Elfogadva1/13ms3712 KiB
13Elfogadva1/13ms3704 KiB
14Hibás válasz0/13ms3708 KiB
15Elfogadva1/13ms3768 KiB
16Elfogadva1/13ms4024 KiB
17Elfogadva1/13ms4060 KiB
18Elfogadva1/130ms10160 KiB
19Elfogadva1/126ms10432 KiB
20Hibás válasz0/132ms10056 KiB
21Hibás válasz0/132ms9992 KiB
22Elfogadva2/223ms12560 KiB
23Elfogadva2/223ms12520 KiB
24Elfogadva2/221ms12524 KiB
25Elfogadva2/221ms12496 KiB
26Elfogadva1/13ms4384 KiB
27Elfogadva1/13ms4532 KiB
28Hibás válasz0/13ms4604 KiB
29Elfogadva1/127ms12788 KiB
30Elfogadva1/127ms12484 KiB
31Elfogadva1/124ms12988 KiB
32Elfogadva1/126ms12996 KiB
33Elfogadva2/226ms13004 KiB
34Elfogadva2/226ms12972 KiB
35Elfogadva2/224ms13140 KiB
36Elfogadva2/224ms12984 KiB