답안 #894685

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894685 2023-12-28T17:21:36 Z Andrey Mergers (JOI19_mergers) C++14
100 / 100
1150 ms 153252 KB
#include <bits/stdc++.h>
using namespace std;

vector<int> haha[500001];
vector<int> col[500001];
vector<int> dep(500001);
vector<int> br(500001);
vector<int> wow[500001];
int bruh[500001][20];

int lca(int a, int b) {
    if(dep[a] < dep[b]) {
        swap(a,b);
    }
    int c = dep[a]-dep[b];
    for(int i = 19; i >= 0; i--) {
        if((1 << i)&c) {
            a = bruh[a][i];
        }
    }
    for(int i = 19; i >= 0; i--) {
        if(bruh[a][i] != -1 && bruh[a][i] != bruh[b][i]) {
            a = bruh[a][i];
            b = bruh[b][i];
        }
    }
    if(a == b) {
        return a;
    }
    else {
        return bruh[a][0];
    }
}

void dfs(int a, int t, int d) {
    dep[a] = d+1;
    bruh[a][0] = t;
    for(int v: haha[a]) {
        if(v != t) {
            dfs(v,a,d+1);
        }
    }
}

void dude(int a, int t) {
    for(int v: haha[a]) {
        if(v != t) {
            dude(v,a);
            br[a]+=br[v];
        }
    }
}

void banana(int a, int t, int l) {
    if(br[a] == 0) {
        if(l != -1) {
            wow[a].push_back(l);
            wow[l].push_back(a);
        }
        l = a;
    }
    for(int v: haha[a]) {
        if(v != t) {
            banana(v,a,l);
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,k,a,b;
    cin >> n >> k;
    for(int i = 0; i < n-1; i++) {
        cin >> a >> b;
        haha[a].push_back(b);
        haha[b].push_back(a);
    }
    dfs(1,-1,0);
    for(long long i = 1; i < 20; i++) {
        for(long long j = 1; j <= n; j++) {
            if(bruh[j][i-1] == -1) {
                bruh[j][i] = -1;
            }
            else {
                bruh[j][i] = bruh[bruh[j][i-1]][i-1];
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        cin >> a;
        col[a].push_back(i);
    }
    for(int i = 1; i <= k; i++) {
        int c = col[i][0];
        for(int v: col[i]) {
            br[v]++;
            c = lca(c,v);
        }
        br[c]-=col[i].size();
    }
    dude(1,-1);
    banana(1,-1,-1);
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        if(wow[i].size() == 1) {
            ans++;
        }
    }
    if(ans == 1) {
        cout << 0;
    }
    else {
        cout << (ans+1)/2;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 41564 KB Output is correct
2 Correct 10 ms 41560 KB Output is correct
3 Correct 10 ms 41564 KB Output is correct
4 Correct 9 ms 41564 KB Output is correct
5 Correct 10 ms 41564 KB Output is correct
6 Correct 10 ms 41564 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 9 ms 41648 KB Output is correct
9 Correct 11 ms 41564 KB Output is correct
10 Correct 10 ms 41564 KB Output is correct
11 Correct 9 ms 41596 KB Output is correct
12 Correct 10 ms 41564 KB Output is correct
13 Correct 9 ms 41404 KB Output is correct
14 Correct 10 ms 41564 KB Output is correct
15 Correct 10 ms 41412 KB Output is correct
16 Correct 11 ms 41640 KB Output is correct
17 Correct 10 ms 41564 KB Output is correct
18 Correct 11 ms 41568 KB Output is correct
19 Correct 10 ms 41644 KB Output is correct
20 Correct 9 ms 41484 KB Output is correct
21 Correct 9 ms 41564 KB Output is correct
22 Correct 10 ms 41564 KB Output is correct
23 Correct 10 ms 41560 KB Output is correct
24 Correct 9 ms 41564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 41564 KB Output is correct
2 Correct 10 ms 41560 KB Output is correct
3 Correct 10 ms 41564 KB Output is correct
4 Correct 9 ms 41564 KB Output is correct
5 Correct 10 ms 41564 KB Output is correct
6 Correct 10 ms 41564 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 9 ms 41648 KB Output is correct
9 Correct 11 ms 41564 KB Output is correct
10 Correct 10 ms 41564 KB Output is correct
11 Correct 9 ms 41596 KB Output is correct
12 Correct 10 ms 41564 KB Output is correct
13 Correct 9 ms 41404 KB Output is correct
14 Correct 10 ms 41564 KB Output is correct
15 Correct 10 ms 41412 KB Output is correct
16 Correct 11 ms 41640 KB Output is correct
17 Correct 10 ms 41564 KB Output is correct
18 Correct 11 ms 41568 KB Output is correct
19 Correct 10 ms 41644 KB Output is correct
20 Correct 9 ms 41484 KB Output is correct
21 Correct 9 ms 41564 KB Output is correct
22 Correct 10 ms 41564 KB Output is correct
23 Correct 10 ms 41560 KB Output is correct
24 Correct 9 ms 41564 KB Output is correct
25 Correct 9 ms 41564 KB Output is correct
26 Correct 11 ms 43868 KB Output is correct
27 Correct 11 ms 43868 KB Output is correct
28 Correct 12 ms 44124 KB Output is correct
29 Correct 12 ms 44164 KB Output is correct
30 Correct 14 ms 44124 KB Output is correct
31 Correct 10 ms 41564 KB Output is correct
32 Correct 12 ms 44124 KB Output is correct
33 Correct 10 ms 41564 KB Output is correct
34 Correct 11 ms 43868 KB Output is correct
35 Correct 12 ms 44124 KB Output is correct
36 Correct 11 ms 43868 KB Output is correct
37 Correct 12 ms 44044 KB Output is correct
38 Correct 10 ms 41564 KB Output is correct
39 Correct 11 ms 43868 KB Output is correct
40 Correct 11 ms 43868 KB Output is correct
41 Correct 11 ms 43868 KB Output is correct
42 Correct 11 ms 43868 KB Output is correct
43 Correct 13 ms 44380 KB Output is correct
44 Correct 10 ms 41564 KB Output is correct
45 Correct 11 ms 43892 KB Output is correct
46 Correct 11 ms 43868 KB Output is correct
47 Correct 10 ms 41564 KB Output is correct
48 Correct 11 ms 43896 KB Output is correct
49 Correct 12 ms 44160 KB Output is correct
50 Correct 12 ms 44124 KB Output is correct
51 Correct 12 ms 43956 KB Output is correct
52 Correct 11 ms 44040 KB Output is correct
53 Correct 11 ms 43868 KB Output is correct
54 Correct 11 ms 43868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 41564 KB Output is correct
2 Correct 10 ms 41560 KB Output is correct
3 Correct 10 ms 41564 KB Output is correct
4 Correct 9 ms 41564 KB Output is correct
5 Correct 10 ms 41564 KB Output is correct
6 Correct 10 ms 41564 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 9 ms 41648 KB Output is correct
9 Correct 11 ms 41564 KB Output is correct
10 Correct 10 ms 41564 KB Output is correct
11 Correct 9 ms 41596 KB Output is correct
12 Correct 10 ms 41564 KB Output is correct
13 Correct 9 ms 41404 KB Output is correct
14 Correct 10 ms 41564 KB Output is correct
15 Correct 10 ms 41412 KB Output is correct
16 Correct 11 ms 41640 KB Output is correct
17 Correct 10 ms 41564 KB Output is correct
18 Correct 11 ms 41568 KB Output is correct
19 Correct 10 ms 41644 KB Output is correct
20 Correct 9 ms 41484 KB Output is correct
21 Correct 9 ms 41564 KB Output is correct
22 Correct 10 ms 41564 KB Output is correct
23 Correct 10 ms 41560 KB Output is correct
24 Correct 9 ms 41564 KB Output is correct
25 Correct 9 ms 41564 KB Output is correct
26 Correct 53 ms 55556 KB Output is correct
27 Correct 61 ms 55156 KB Output is correct
28 Correct 12 ms 43864 KB Output is correct
29 Correct 10 ms 41560 KB Output is correct
30 Correct 10 ms 41640 KB Output is correct
31 Correct 59 ms 55160 KB Output is correct
32 Correct 11 ms 43868 KB Output is correct
33 Correct 80 ms 60684 KB Output is correct
34 Correct 65 ms 55120 KB Output is correct
35 Correct 13 ms 43864 KB Output is correct
36 Correct 99 ms 55568 KB Output is correct
37 Correct 11 ms 43864 KB Output is correct
38 Correct 11 ms 43868 KB Output is correct
39 Correct 47 ms 55380 KB Output is correct
40 Correct 11 ms 44120 KB Output is correct
41 Correct 56 ms 55236 KB Output is correct
42 Correct 72 ms 56908 KB Output is correct
43 Correct 10 ms 41564 KB Output is correct
44 Correct 72 ms 60876 KB Output is correct
45 Correct 84 ms 58184 KB Output is correct
46 Correct 10 ms 43864 KB Output is correct
47 Correct 11 ms 43868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 55500 KB Output is correct
2 Correct 89 ms 61524 KB Output is correct
3 Correct 11 ms 43864 KB Output is correct
4 Correct 13 ms 43868 KB Output is correct
5 Correct 9 ms 41644 KB Output is correct
6 Correct 9 ms 41564 KB Output is correct
7 Correct 11 ms 43868 KB Output is correct
8 Correct 72 ms 56752 KB Output is correct
9 Correct 11 ms 43868 KB Output is correct
10 Correct 74 ms 55368 KB Output is correct
11 Correct 9 ms 41560 KB Output is correct
12 Correct 67 ms 55128 KB Output is correct
13 Correct 92 ms 56948 KB Output is correct
14 Correct 83 ms 61264 KB Output is correct
15 Correct 47 ms 55216 KB Output is correct
16 Correct 11 ms 43864 KB Output is correct
17 Correct 10 ms 41564 KB Output is correct
18 Correct 68 ms 60928 KB Output is correct
19 Correct 104 ms 64496 KB Output is correct
20 Correct 11 ms 43868 KB Output is correct
21 Correct 9 ms 41500 KB Output is correct
22 Correct 56 ms 57256 KB Output is correct
23 Correct 11 ms 43868 KB Output is correct
24 Correct 72 ms 55700 KB Output is correct
25 Correct 148 ms 63056 KB Output is correct
26 Correct 11 ms 44124 KB Output is correct
27 Correct 12 ms 44124 KB Output is correct
28 Correct 11 ms 43864 KB Output is correct
29 Correct 12 ms 43868 KB Output is correct
30 Correct 12 ms 43868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 41564 KB Output is correct
2 Correct 10 ms 41560 KB Output is correct
3 Correct 10 ms 41564 KB Output is correct
4 Correct 9 ms 41564 KB Output is correct
5 Correct 10 ms 41564 KB Output is correct
6 Correct 10 ms 41564 KB Output is correct
7 Correct 9 ms 41564 KB Output is correct
8 Correct 9 ms 41648 KB Output is correct
9 Correct 11 ms 41564 KB Output is correct
10 Correct 10 ms 41564 KB Output is correct
11 Correct 9 ms 41596 KB Output is correct
12 Correct 10 ms 41564 KB Output is correct
13 Correct 9 ms 41404 KB Output is correct
14 Correct 10 ms 41564 KB Output is correct
15 Correct 10 ms 41412 KB Output is correct
16 Correct 11 ms 41640 KB Output is correct
17 Correct 10 ms 41564 KB Output is correct
18 Correct 11 ms 41568 KB Output is correct
19 Correct 10 ms 41644 KB Output is correct
20 Correct 9 ms 41484 KB Output is correct
21 Correct 9 ms 41564 KB Output is correct
22 Correct 10 ms 41564 KB Output is correct
23 Correct 10 ms 41560 KB Output is correct
24 Correct 9 ms 41564 KB Output is correct
25 Correct 9 ms 41564 KB Output is correct
26 Correct 11 ms 43868 KB Output is correct
27 Correct 11 ms 43868 KB Output is correct
28 Correct 12 ms 44124 KB Output is correct
29 Correct 12 ms 44164 KB Output is correct
30 Correct 14 ms 44124 KB Output is correct
31 Correct 10 ms 41564 KB Output is correct
32 Correct 12 ms 44124 KB Output is correct
33 Correct 10 ms 41564 KB Output is correct
34 Correct 11 ms 43868 KB Output is correct
35 Correct 12 ms 44124 KB Output is correct
36 Correct 11 ms 43868 KB Output is correct
37 Correct 12 ms 44044 KB Output is correct
38 Correct 10 ms 41564 KB Output is correct
39 Correct 11 ms 43868 KB Output is correct
40 Correct 11 ms 43868 KB Output is correct
41 Correct 11 ms 43868 KB Output is correct
42 Correct 11 ms 43868 KB Output is correct
43 Correct 13 ms 44380 KB Output is correct
44 Correct 10 ms 41564 KB Output is correct
45 Correct 11 ms 43892 KB Output is correct
46 Correct 11 ms 43868 KB Output is correct
47 Correct 10 ms 41564 KB Output is correct
48 Correct 11 ms 43896 KB Output is correct
49 Correct 12 ms 44160 KB Output is correct
50 Correct 12 ms 44124 KB Output is correct
51 Correct 12 ms 43956 KB Output is correct
52 Correct 11 ms 44040 KB Output is correct
53 Correct 11 ms 43868 KB Output is correct
54 Correct 11 ms 43868 KB Output is correct
55 Correct 9 ms 41564 KB Output is correct
56 Correct 53 ms 55556 KB Output is correct
57 Correct 61 ms 55156 KB Output is correct
58 Correct 12 ms 43864 KB Output is correct
59 Correct 10 ms 41560 KB Output is correct
60 Correct 10 ms 41640 KB Output is correct
61 Correct 59 ms 55160 KB Output is correct
62 Correct 11 ms 43868 KB Output is correct
63 Correct 80 ms 60684 KB Output is correct
64 Correct 65 ms 55120 KB Output is correct
65 Correct 13 ms 43864 KB Output is correct
66 Correct 99 ms 55568 KB Output is correct
67 Correct 11 ms 43864 KB Output is correct
68 Correct 11 ms 43868 KB Output is correct
69 Correct 47 ms 55380 KB Output is correct
70 Correct 11 ms 44120 KB Output is correct
71 Correct 56 ms 55236 KB Output is correct
72 Correct 72 ms 56908 KB Output is correct
73 Correct 10 ms 41564 KB Output is correct
74 Correct 72 ms 60876 KB Output is correct
75 Correct 84 ms 58184 KB Output is correct
76 Correct 10 ms 43864 KB Output is correct
77 Correct 11 ms 43868 KB Output is correct
78 Correct 46 ms 55500 KB Output is correct
79 Correct 89 ms 61524 KB Output is correct
80 Correct 11 ms 43864 KB Output is correct
81 Correct 13 ms 43868 KB Output is correct
82 Correct 9 ms 41644 KB Output is correct
83 Correct 9 ms 41564 KB Output is correct
84 Correct 11 ms 43868 KB Output is correct
85 Correct 72 ms 56752 KB Output is correct
86 Correct 11 ms 43868 KB Output is correct
87 Correct 74 ms 55368 KB Output is correct
88 Correct 9 ms 41560 KB Output is correct
89 Correct 67 ms 55128 KB Output is correct
90 Correct 92 ms 56948 KB Output is correct
91 Correct 83 ms 61264 KB Output is correct
92 Correct 47 ms 55216 KB Output is correct
93 Correct 11 ms 43864 KB Output is correct
94 Correct 10 ms 41564 KB Output is correct
95 Correct 68 ms 60928 KB Output is correct
96 Correct 104 ms 64496 KB Output is correct
97 Correct 11 ms 43868 KB Output is correct
98 Correct 9 ms 41500 KB Output is correct
99 Correct 56 ms 57256 KB Output is correct
100 Correct 11 ms 43868 KB Output is correct
101 Correct 72 ms 55700 KB Output is correct
102 Correct 148 ms 63056 KB Output is correct
103 Correct 11 ms 44124 KB Output is correct
104 Correct 12 ms 44124 KB Output is correct
105 Correct 11 ms 43864 KB Output is correct
106 Correct 12 ms 43868 KB Output is correct
107 Correct 12 ms 43868 KB Output is correct
108 Correct 440 ms 114376 KB Output is correct
109 Correct 853 ms 119004 KB Output is correct
110 Correct 807 ms 127912 KB Output is correct
111 Correct 1150 ms 153252 KB Output is correct
112 Correct 965 ms 140180 KB Output is correct
113 Correct 623 ms 134708 KB Output is correct
114 Correct 514 ms 104560 KB Output is correct
115 Correct 440 ms 104900 KB Output is correct
116 Correct 593 ms 109324 KB Output is correct
117 Correct 830 ms 136752 KB Output is correct
118 Correct 545 ms 106132 KB Output is correct
119 Correct 830 ms 136712 KB Output is correct
120 Correct 984 ms 147240 KB Output is correct
121 Correct 853 ms 136480 KB Output is correct
122 Correct 651 ms 117200 KB Output is correct
123 Correct 594 ms 139480 KB Output is correct
124 Correct 937 ms 129316 KB Output is correct