82942024-01-14 11:32:24zsomborMaximális kaktusz (45 pont)cpp17Wrong answer 9/4554ms13320 KiB
#include <iostream>
#include <vector>
using namespace std;

int n, m, a, b, ans = 0;
vector <vector <int>> g(1e5);
vector <int> p(1e5, 0);
vector <bool> fa(1e5, true);
vector <bool> szabad;

void dfs1(int x) {
    for (int i : g[x]) {
        if (i == p[x]) continue;
        if (p[i]) {
            a = x;
            if (fa[x]) while (a != p[i]) { fa[a] = false; a = p[a]; }
            continue;
        }
        p[i] = x;
        dfs1(i);
    }
}

void dfs2(int x) {
    fa[x] = false;
    int c = 0;
    for (int i : g[x]) {
        if (!fa[i]) continue;
        p[i] = x;
        dfs2(i);
        if (szabad[i]) c++;
    }
    if (!szabad[x]) return;
    if (c >= 2) {
        ans++;
        szabad[x] = false;
    }
    if (c == 1 && p[x] > 0) {
        ans++;
        szabad[x] = false;
        szabad[p[x]] = false;
    }
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    p[1] = -1;
    dfs1(1);
    fill(p.begin(), p.end(), 0);
    szabad = fa;
    for (int i = 1; i <= n; i++) {
        if (!fa[i]) continue;
        p[i] = -1;
        dfs2(i);
    }
    cout << ans;
}
SubtaskSumTestVerdictTimeMemory
base9/45
1Accepted0/06ms7260 KiB
2Wrong answer0/04ms7600 KiB
3Accepted1/14ms7784 KiB
4Accepted1/14ms8004 KiB
5Accepted1/16ms8076 KiB
6Accepted2/24ms8420 KiB
7Wrong answer0/24ms8528 KiB
8Accepted2/26ms8720 KiB
9Wrong answer0/14ms8684 KiB
10Accepted1/14ms8720 KiB
11Wrong answer0/14ms8840 KiB
12Wrong answer0/14ms8872 KiB
13Wrong answer0/14ms8876 KiB
14Wrong answer0/14ms8948 KiB
15Wrong answer0/16ms8924 KiB
16Wrong answer0/16ms8820 KiB
17Wrong answer0/16ms8944 KiB
18Wrong answer0/150ms12232 KiB
19Wrong answer0/146ms12508 KiB
20Wrong answer0/152ms12460 KiB
21Wrong answer0/154ms12536 KiB
22Wrong answer0/239ms12964 KiB
23Wrong answer0/239ms12992 KiB
24Wrong answer0/239ms12836 KiB
25Wrong answer0/239ms13052 KiB
26Accepted1/14ms9420 KiB
27Wrong answer0/14ms9416 KiB
28Wrong answer0/16ms9560 KiB
29Wrong answer0/148ms12860 KiB
30Wrong answer0/148ms12936 KiB
31Wrong answer0/145ms12868 KiB
32Wrong answer0/146ms13060 KiB
33Wrong answer0/248ms13036 KiB
34Wrong answer0/248ms13128 KiB
35Wrong answer0/246ms13320 KiB
36Wrong answer0/243ms13296 KiB