84512024-01-16 16:17:45anonMaximális kaktusz (45 pont)cpp17Wrong answer 10/4532ms12588 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;
    if(streak >= 3) {
        ans++;
        streak = 0;
    }
    vis[vertex] = true;
    vector<ll> nbs;
    vector<ll> rs;
    for(const auto &x : graph[vertex]) {
        if(!vis[x])
            nbs.push_back(x);
    }
    if(nbs.size() == 1)
        return dfs(nbs[0], streak + 1, ans, vis, graph);
    else {
        for(const auto &x : nbs) {
            rem = dfs(x, 1, ans, vis, graph);
            if(rem)
                rs.push_back(rem);
        }
        if(rs.size() == 1) {
            ans += (streak + rs[0]) / 3;
            return (streak + rs[0]) % 3;
        }
        else if(rs.size() >= 2 && streak)
            ans++;
    }
    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
base10/45
1Accepted0/03ms1828 KiB
2Accepted0/03ms2188 KiB
3Wrong answer0/12ms2104 KiB
4Accepted1/13ms2236 KiB
5Wrong answer0/13ms2444 KiB
6Wrong answer0/23ms2652 KiB
7Accepted2/23ms2752 KiB
8Wrong answer0/23ms2968 KiB
9Wrong answer0/13ms3056 KiB
10Accepted1/13ms3156 KiB
11Wrong answer0/13ms3152 KiB
12Wrong answer0/13ms3396 KiB
13Wrong answer0/13ms3600 KiB
14Wrong answer0/13ms3704 KiB
15Wrong answer0/13ms3864 KiB
16Wrong answer0/13ms3852 KiB
17Wrong answer0/13ms3792 KiB
18Wrong answer0/130ms9768 KiB
19Accepted1/126ms10196 KiB
20Wrong answer0/132ms9708 KiB
21Wrong answer0/132ms9708 KiB
22Wrong answer0/223ms11940 KiB
23Wrong answer0/223ms12188 KiB
24Accepted2/223ms12432 KiB
25Accepted2/221ms12384 KiB
26Accepted1/13ms4148 KiB
27Wrong answer0/13ms4276 KiB
28Wrong answer0/13ms4384 KiB
29Wrong answer0/127ms12564 KiB
30Wrong answer0/126ms11928 KiB
31Wrong answer0/124ms12576 KiB
32Wrong answer0/125ms12564 KiB
33Wrong answer0/225ms12588 KiB
34Wrong answer0/226ms12432 KiB
35Wrong answer0/224ms12568 KiB
36Wrong answer0/224ms12556 KiB