#include <bits/stdc++.h>
#define pii pair<int, int>
#define ti3 tuple<int, int, int>
#define ti4 tuple<int, int, int, int>
#define int long long
// Honestly, I love you, to the mysterious (SinB) place ~ SinB, Sunny Summer
using namespace std;
int p[500005];
int fs(int x){
if (p[x] == x) return x;
else return p[x] = fs(p[x]);
}
bool ss(int x, int y){
return fs(x) == fs(y);
}
void ms(int x, int y){ // x <- y
if (ss(x, y)) return;
p[fs(y)] = fs(x);
}
vector<int> adjlist[500005];
int tpar[500005], dep[500005];
void dfs(int x, int p, int d){
tpar[x] = p;
dep[x] = d;
for (auto i : adjlist[x]){
if (i == p) continue;
dfs(i, x, d+1);
}
}
int prvState[500005], deg[500005];
main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n, k; cin >> n >> k;
for (int i = 0; i < n - 1; i++){
int a, b; cin >> a >> b;
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
dfs(1, -1, 0);
iota(p, p+n+2, 0);
for (int i = 1; i <= n; i++){
int x; cin >> x;
if (prvState[x]){
int a = fs(i), b = fs(prvState[x]);
while (!ss(a, b)){
if (dep[a] > dep[b]) swap(a, b);
ms(tpar[b], b);
b = fs(b);
}
}
prvState[x] = i;
}
for (int i = 1; i <= n; i++){
for (auto j : adjlist[i]){
if (!ss(i, j)) deg[fs(i)]++;
}
}
int cnt = 0;
for (int i = 1; i <= n; i++){
if (deg[i] == 1) cnt++;
}
cout << (cnt + 1) / 2 << endl;
}
Compilation message
mergers.cpp:31:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
31 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11984 KB |
Output is correct |
2 |
Correct |
7 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12024 KB |
Output is correct |
4 |
Correct |
7 ms |
11984 KB |
Output is correct |
5 |
Correct |
7 ms |
11984 KB |
Output is correct |
6 |
Correct |
7 ms |
12112 KB |
Output is correct |
7 |
Correct |
8 ms |
12112 KB |
Output is correct |
8 |
Correct |
8 ms |
11984 KB |
Output is correct |
9 |
Correct |
7 ms |
11984 KB |
Output is correct |
10 |
Correct |
7 ms |
12080 KB |
Output is correct |
11 |
Correct |
7 ms |
11984 KB |
Output is correct |
12 |
Correct |
7 ms |
12108 KB |
Output is correct |
13 |
Correct |
7 ms |
12088 KB |
Output is correct |
14 |
Correct |
6 ms |
11984 KB |
Output is correct |
15 |
Correct |
8 ms |
11984 KB |
Output is correct |
16 |
Correct |
7 ms |
12028 KB |
Output is correct |
17 |
Correct |
7 ms |
12048 KB |
Output is correct |
18 |
Correct |
7 ms |
12076 KB |
Output is correct |
19 |
Correct |
8 ms |
12024 KB |
Output is correct |
20 |
Correct |
7 ms |
11984 KB |
Output is correct |
21 |
Correct |
9 ms |
12068 KB |
Output is correct |
22 |
Correct |
7 ms |
11984 KB |
Output is correct |
23 |
Correct |
7 ms |
12048 KB |
Output is correct |
24 |
Correct |
7 ms |
11984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11984 KB |
Output is correct |
2 |
Correct |
7 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12024 KB |
Output is correct |
4 |
Correct |
7 ms |
11984 KB |
Output is correct |
5 |
Correct |
7 ms |
11984 KB |
Output is correct |
6 |
Correct |
7 ms |
12112 KB |
Output is correct |
7 |
Correct |
8 ms |
12112 KB |
Output is correct |
8 |
Correct |
8 ms |
11984 KB |
Output is correct |
9 |
Correct |
7 ms |
11984 KB |
Output is correct |
10 |
Correct |
7 ms |
12080 KB |
Output is correct |
11 |
Correct |
7 ms |
11984 KB |
Output is correct |
12 |
Correct |
7 ms |
12108 KB |
Output is correct |
13 |
Correct |
7 ms |
12088 KB |
Output is correct |
14 |
Correct |
6 ms |
11984 KB |
Output is correct |
15 |
Correct |
8 ms |
11984 KB |
Output is correct |
16 |
Correct |
7 ms |
12028 KB |
Output is correct |
17 |
Correct |
7 ms |
12048 KB |
Output is correct |
18 |
Correct |
7 ms |
12076 KB |
Output is correct |
19 |
Correct |
8 ms |
12024 KB |
Output is correct |
20 |
Correct |
7 ms |
11984 KB |
Output is correct |
21 |
Correct |
9 ms |
12068 KB |
Output is correct |
22 |
Correct |
7 ms |
11984 KB |
Output is correct |
23 |
Correct |
7 ms |
12048 KB |
Output is correct |
24 |
Correct |
7 ms |
11984 KB |
Output is correct |
25 |
Correct |
7 ms |
12112 KB |
Output is correct |
26 |
Correct |
8 ms |
12288 KB |
Output is correct |
27 |
Correct |
8 ms |
12240 KB |
Output is correct |
28 |
Correct |
9 ms |
12424 KB |
Output is correct |
29 |
Correct |
8 ms |
12368 KB |
Output is correct |
30 |
Correct |
9 ms |
12308 KB |
Output is correct |
31 |
Correct |
6 ms |
11984 KB |
Output is correct |
32 |
Correct |
8 ms |
12352 KB |
Output is correct |
33 |
Correct |
8 ms |
12076 KB |
Output is correct |
34 |
Correct |
8 ms |
12216 KB |
Output is correct |
35 |
Correct |
9 ms |
12328 KB |
Output is correct |
36 |
Correct |
8 ms |
12352 KB |
Output is correct |
37 |
Correct |
8 ms |
12240 KB |
Output is correct |
38 |
Correct |
7 ms |
12040 KB |
Output is correct |
39 |
Correct |
11 ms |
12288 KB |
Output is correct |
40 |
Correct |
8 ms |
12220 KB |
Output is correct |
41 |
Correct |
8 ms |
12216 KB |
Output is correct |
42 |
Correct |
8 ms |
12368 KB |
Output is correct |
43 |
Correct |
11 ms |
12280 KB |
Output is correct |
44 |
Correct |
7 ms |
11984 KB |
Output is correct |
45 |
Correct |
8 ms |
12368 KB |
Output is correct |
46 |
Correct |
8 ms |
12240 KB |
Output is correct |
47 |
Correct |
7 ms |
11984 KB |
Output is correct |
48 |
Correct |
11 ms |
12240 KB |
Output is correct |
49 |
Correct |
8 ms |
12368 KB |
Output is correct |
50 |
Correct |
7 ms |
12368 KB |
Output is correct |
51 |
Correct |
9 ms |
12240 KB |
Output is correct |
52 |
Correct |
8 ms |
12288 KB |
Output is correct |
53 |
Correct |
9 ms |
12368 KB |
Output is correct |
54 |
Correct |
8 ms |
12240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11984 KB |
Output is correct |
2 |
Correct |
7 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12024 KB |
Output is correct |
4 |
Correct |
7 ms |
11984 KB |
Output is correct |
5 |
Correct |
7 ms |
11984 KB |
Output is correct |
6 |
Correct |
7 ms |
12112 KB |
Output is correct |
7 |
Correct |
8 ms |
12112 KB |
Output is correct |
8 |
Correct |
8 ms |
11984 KB |
Output is correct |
9 |
Correct |
7 ms |
11984 KB |
Output is correct |
10 |
Correct |
7 ms |
12080 KB |
Output is correct |
11 |
Correct |
7 ms |
11984 KB |
Output is correct |
12 |
Correct |
7 ms |
12108 KB |
Output is correct |
13 |
Correct |
7 ms |
12088 KB |
Output is correct |
14 |
Correct |
6 ms |
11984 KB |
Output is correct |
15 |
Correct |
8 ms |
11984 KB |
Output is correct |
16 |
Correct |
7 ms |
12028 KB |
Output is correct |
17 |
Correct |
7 ms |
12048 KB |
Output is correct |
18 |
Correct |
7 ms |
12076 KB |
Output is correct |
19 |
Correct |
8 ms |
12024 KB |
Output is correct |
20 |
Correct |
7 ms |
11984 KB |
Output is correct |
21 |
Correct |
9 ms |
12068 KB |
Output is correct |
22 |
Correct |
7 ms |
11984 KB |
Output is correct |
23 |
Correct |
7 ms |
12048 KB |
Output is correct |
24 |
Correct |
7 ms |
11984 KB |
Output is correct |
25 |
Correct |
6 ms |
11984 KB |
Output is correct |
26 |
Correct |
54 ms |
19720 KB |
Output is correct |
27 |
Correct |
70 ms |
19604 KB |
Output is correct |
28 |
Correct |
8 ms |
12240 KB |
Output is correct |
29 |
Correct |
7 ms |
12112 KB |
Output is correct |
30 |
Correct |
8 ms |
12084 KB |
Output is correct |
31 |
Correct |
67 ms |
19680 KB |
Output is correct |
32 |
Correct |
8 ms |
12220 KB |
Output is correct |
33 |
Correct |
70 ms |
22052 KB |
Output is correct |
34 |
Correct |
90 ms |
19568 KB |
Output is correct |
35 |
Correct |
8 ms |
12232 KB |
Output is correct |
36 |
Correct |
90 ms |
19508 KB |
Output is correct |
37 |
Correct |
10 ms |
12240 KB |
Output is correct |
38 |
Correct |
11 ms |
12216 KB |
Output is correct |
39 |
Correct |
52 ms |
19744 KB |
Output is correct |
40 |
Correct |
8 ms |
12368 KB |
Output is correct |
41 |
Correct |
69 ms |
19432 KB |
Output is correct |
42 |
Correct |
93 ms |
20064 KB |
Output is correct |
43 |
Correct |
7 ms |
12084 KB |
Output is correct |
44 |
Correct |
78 ms |
22012 KB |
Output is correct |
45 |
Correct |
79 ms |
21796 KB |
Output is correct |
46 |
Correct |
7 ms |
12240 KB |
Output is correct |
47 |
Correct |
9 ms |
12216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
19884 KB |
Output is correct |
2 |
Correct |
56 ms |
21668 KB |
Output is correct |
3 |
Correct |
8 ms |
12232 KB |
Output is correct |
4 |
Correct |
8 ms |
12240 KB |
Output is correct |
5 |
Correct |
7 ms |
11984 KB |
Output is correct |
6 |
Correct |
6 ms |
12076 KB |
Output is correct |
7 |
Correct |
7 ms |
12240 KB |
Output is correct |
8 |
Correct |
68 ms |
20976 KB |
Output is correct |
9 |
Correct |
8 ms |
12348 KB |
Output is correct |
10 |
Correct |
117 ms |
20388 KB |
Output is correct |
11 |
Correct |
7 ms |
12076 KB |
Output is correct |
12 |
Correct |
98 ms |
19916 KB |
Output is correct |
13 |
Correct |
107 ms |
20980 KB |
Output is correct |
14 |
Correct |
90 ms |
21432 KB |
Output is correct |
15 |
Correct |
59 ms |
19724 KB |
Output is correct |
16 |
Correct |
10 ms |
12348 KB |
Output is correct |
17 |
Correct |
10 ms |
12084 KB |
Output is correct |
18 |
Correct |
64 ms |
21584 KB |
Output is correct |
19 |
Correct |
81 ms |
22452 KB |
Output is correct |
20 |
Correct |
8 ms |
12348 KB |
Output is correct |
21 |
Correct |
6 ms |
12080 KB |
Output is correct |
22 |
Correct |
51 ms |
21160 KB |
Output is correct |
23 |
Correct |
8 ms |
12240 KB |
Output is correct |
24 |
Correct |
73 ms |
20276 KB |
Output is correct |
25 |
Correct |
61 ms |
23112 KB |
Output is correct |
26 |
Correct |
8 ms |
12368 KB |
Output is correct |
27 |
Correct |
8 ms |
12348 KB |
Output is correct |
28 |
Correct |
8 ms |
12216 KB |
Output is correct |
29 |
Correct |
11 ms |
12216 KB |
Output is correct |
30 |
Correct |
8 ms |
12368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11984 KB |
Output is correct |
2 |
Correct |
7 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12024 KB |
Output is correct |
4 |
Correct |
7 ms |
11984 KB |
Output is correct |
5 |
Correct |
7 ms |
11984 KB |
Output is correct |
6 |
Correct |
7 ms |
12112 KB |
Output is correct |
7 |
Correct |
8 ms |
12112 KB |
Output is correct |
8 |
Correct |
8 ms |
11984 KB |
Output is correct |
9 |
Correct |
7 ms |
11984 KB |
Output is correct |
10 |
Correct |
7 ms |
12080 KB |
Output is correct |
11 |
Correct |
7 ms |
11984 KB |
Output is correct |
12 |
Correct |
7 ms |
12108 KB |
Output is correct |
13 |
Correct |
7 ms |
12088 KB |
Output is correct |
14 |
Correct |
6 ms |
11984 KB |
Output is correct |
15 |
Correct |
8 ms |
11984 KB |
Output is correct |
16 |
Correct |
7 ms |
12028 KB |
Output is correct |
17 |
Correct |
7 ms |
12048 KB |
Output is correct |
18 |
Correct |
7 ms |
12076 KB |
Output is correct |
19 |
Correct |
8 ms |
12024 KB |
Output is correct |
20 |
Correct |
7 ms |
11984 KB |
Output is correct |
21 |
Correct |
9 ms |
12068 KB |
Output is correct |
22 |
Correct |
7 ms |
11984 KB |
Output is correct |
23 |
Correct |
7 ms |
12048 KB |
Output is correct |
24 |
Correct |
7 ms |
11984 KB |
Output is correct |
25 |
Correct |
7 ms |
12112 KB |
Output is correct |
26 |
Correct |
8 ms |
12288 KB |
Output is correct |
27 |
Correct |
8 ms |
12240 KB |
Output is correct |
28 |
Correct |
9 ms |
12424 KB |
Output is correct |
29 |
Correct |
8 ms |
12368 KB |
Output is correct |
30 |
Correct |
9 ms |
12308 KB |
Output is correct |
31 |
Correct |
6 ms |
11984 KB |
Output is correct |
32 |
Correct |
8 ms |
12352 KB |
Output is correct |
33 |
Correct |
8 ms |
12076 KB |
Output is correct |
34 |
Correct |
8 ms |
12216 KB |
Output is correct |
35 |
Correct |
9 ms |
12328 KB |
Output is correct |
36 |
Correct |
8 ms |
12352 KB |
Output is correct |
37 |
Correct |
8 ms |
12240 KB |
Output is correct |
38 |
Correct |
7 ms |
12040 KB |
Output is correct |
39 |
Correct |
11 ms |
12288 KB |
Output is correct |
40 |
Correct |
8 ms |
12220 KB |
Output is correct |
41 |
Correct |
8 ms |
12216 KB |
Output is correct |
42 |
Correct |
8 ms |
12368 KB |
Output is correct |
43 |
Correct |
11 ms |
12280 KB |
Output is correct |
44 |
Correct |
7 ms |
11984 KB |
Output is correct |
45 |
Correct |
8 ms |
12368 KB |
Output is correct |
46 |
Correct |
8 ms |
12240 KB |
Output is correct |
47 |
Correct |
7 ms |
11984 KB |
Output is correct |
48 |
Correct |
11 ms |
12240 KB |
Output is correct |
49 |
Correct |
8 ms |
12368 KB |
Output is correct |
50 |
Correct |
7 ms |
12368 KB |
Output is correct |
51 |
Correct |
9 ms |
12240 KB |
Output is correct |
52 |
Correct |
8 ms |
12288 KB |
Output is correct |
53 |
Correct |
9 ms |
12368 KB |
Output is correct |
54 |
Correct |
8 ms |
12240 KB |
Output is correct |
55 |
Correct |
6 ms |
11984 KB |
Output is correct |
56 |
Correct |
54 ms |
19720 KB |
Output is correct |
57 |
Correct |
70 ms |
19604 KB |
Output is correct |
58 |
Correct |
8 ms |
12240 KB |
Output is correct |
59 |
Correct |
7 ms |
12112 KB |
Output is correct |
60 |
Correct |
8 ms |
12084 KB |
Output is correct |
61 |
Correct |
67 ms |
19680 KB |
Output is correct |
62 |
Correct |
8 ms |
12220 KB |
Output is correct |
63 |
Correct |
70 ms |
22052 KB |
Output is correct |
64 |
Correct |
90 ms |
19568 KB |
Output is correct |
65 |
Correct |
8 ms |
12232 KB |
Output is correct |
66 |
Correct |
90 ms |
19508 KB |
Output is correct |
67 |
Correct |
10 ms |
12240 KB |
Output is correct |
68 |
Correct |
11 ms |
12216 KB |
Output is correct |
69 |
Correct |
52 ms |
19744 KB |
Output is correct |
70 |
Correct |
8 ms |
12368 KB |
Output is correct |
71 |
Correct |
69 ms |
19432 KB |
Output is correct |
72 |
Correct |
93 ms |
20064 KB |
Output is correct |
73 |
Correct |
7 ms |
12084 KB |
Output is correct |
74 |
Correct |
78 ms |
22012 KB |
Output is correct |
75 |
Correct |
79 ms |
21796 KB |
Output is correct |
76 |
Correct |
7 ms |
12240 KB |
Output is correct |
77 |
Correct |
9 ms |
12216 KB |
Output is correct |
78 |
Correct |
49 ms |
19884 KB |
Output is correct |
79 |
Correct |
56 ms |
21668 KB |
Output is correct |
80 |
Correct |
8 ms |
12232 KB |
Output is correct |
81 |
Correct |
8 ms |
12240 KB |
Output is correct |
82 |
Correct |
7 ms |
11984 KB |
Output is correct |
83 |
Correct |
6 ms |
12076 KB |
Output is correct |
84 |
Correct |
7 ms |
12240 KB |
Output is correct |
85 |
Correct |
68 ms |
20976 KB |
Output is correct |
86 |
Correct |
8 ms |
12348 KB |
Output is correct |
87 |
Correct |
117 ms |
20388 KB |
Output is correct |
88 |
Correct |
7 ms |
12076 KB |
Output is correct |
89 |
Correct |
98 ms |
19916 KB |
Output is correct |
90 |
Correct |
107 ms |
20980 KB |
Output is correct |
91 |
Correct |
90 ms |
21432 KB |
Output is correct |
92 |
Correct |
59 ms |
19724 KB |
Output is correct |
93 |
Correct |
10 ms |
12348 KB |
Output is correct |
94 |
Correct |
10 ms |
12084 KB |
Output is correct |
95 |
Correct |
64 ms |
21584 KB |
Output is correct |
96 |
Correct |
81 ms |
22452 KB |
Output is correct |
97 |
Correct |
8 ms |
12348 KB |
Output is correct |
98 |
Correct |
6 ms |
12080 KB |
Output is correct |
99 |
Correct |
51 ms |
21160 KB |
Output is correct |
100 |
Correct |
8 ms |
12240 KB |
Output is correct |
101 |
Correct |
73 ms |
20276 KB |
Output is correct |
102 |
Correct |
61 ms |
23112 KB |
Output is correct |
103 |
Correct |
8 ms |
12368 KB |
Output is correct |
104 |
Correct |
8 ms |
12348 KB |
Output is correct |
105 |
Correct |
8 ms |
12216 KB |
Output is correct |
106 |
Correct |
11 ms |
12216 KB |
Output is correct |
107 |
Correct |
8 ms |
12368 KB |
Output is correct |
108 |
Correct |
385 ms |
58832 KB |
Output is correct |
109 |
Correct |
651 ms |
54928 KB |
Output is correct |
110 |
Correct |
668 ms |
59632 KB |
Output is correct |
111 |
Correct |
557 ms |
67140 KB |
Output is correct |
112 |
Correct |
610 ms |
68900 KB |
Output is correct |
113 |
Correct |
391 ms |
60764 KB |
Output is correct |
114 |
Correct |
596 ms |
50108 KB |
Output is correct |
115 |
Correct |
654 ms |
50060 KB |
Output is correct |
116 |
Correct |
730 ms |
55324 KB |
Output is correct |
117 |
Correct |
626 ms |
60176 KB |
Output is correct |
118 |
Correct |
626 ms |
52288 KB |
Output is correct |
119 |
Correct |
508 ms |
60228 KB |
Output is correct |
120 |
Correct |
536 ms |
69160 KB |
Output is correct |
121 |
Correct |
506 ms |
60212 KB |
Output is correct |
122 |
Correct |
653 ms |
58576 KB |
Output is correct |
123 |
Correct |
317 ms |
61260 KB |
Output is correct |
124 |
Correct |
565 ms |
56528 KB |
Output is correct |