#include <bits/stdc++.h>
using namespace std;
int n,k;
vector<int> G[1000005];
int c[1000005];
int low[1000005];
int num[1000005];
int p[1000005];
int ct = 0;
int pa[500005];
int eg[500005];
int rk[500005];
void init(int n){
for (int i = 1; i <= n; i++){
pa[i] = i; rk[i] = 0;
eg[i] = 0;
}
}
int fin(int x){
return pa[x] == x ? x : pa[x] = fin(pa[x]);
}
void un(int x, int y){
x = fin(x), y = fin(y);
if (x == y) return;
if (rk[x] < rk[y]) swap(x,y);
pa[y] = x;
eg[x] += eg[y];
if (rk[x] == rk[y]) rk[x]++;
}
void dfs2(int u){
for (auto v : G[u]){
if (v <= n){
if (v == p[u]) continue;
p[v] = u;
dfs2(v);
if (low[v] <= num[u]){
un(u,v);
}
else{
//printf("bridge %d %d\n",u,v);
eg[fin(u)]++;
eg[fin(v)]++;
}
}
}
}
void dfs(int u, int P){
num[u] = low[u] = ct++;
for (auto v : G[u]){
if (num[v] == -1){
//printf("%d -> %d\n",u,v);
dfs(v, u);
low[u] = min(low[v], low[u]);
}
else if (v != P){
low[u] = min(num[v], low[u]);
}
}
}
int main(){
scanf("%d%d",&n,&k);
for (int i = 1; i < n; i++){
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = 1; i <= n; i++){
scanf("%d",&c[i]);
G[i].push_back(n+c[i]);
G[n+c[i]].push_back(i);
}
memset(low,-1,sizeof(low));
memset(num,-1,sizeof(num));
init(n);
dfs(1,-1);
p[1] = 0;
dfs2(1);
int ans = 0;
for (int i = 1; i <= n; i++){
if (i == pa[i] && eg[i] == 1) {
//printf("%d ",i);
ans++;
}
}
printf("%d",(ans+1)/2);
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
mergers.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d%d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~
mergers.cpp:75:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | scanf("%d",&c[i]);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
31572 KB |
Output is correct |
2 |
Correct |
15 ms |
31572 KB |
Output is correct |
3 |
Correct |
16 ms |
31700 KB |
Output is correct |
4 |
Correct |
16 ms |
31572 KB |
Output is correct |
5 |
Correct |
16 ms |
31600 KB |
Output is correct |
6 |
Correct |
17 ms |
31556 KB |
Output is correct |
7 |
Correct |
17 ms |
31572 KB |
Output is correct |
8 |
Correct |
16 ms |
31572 KB |
Output is correct |
9 |
Correct |
15 ms |
31572 KB |
Output is correct |
10 |
Correct |
16 ms |
31572 KB |
Output is correct |
11 |
Correct |
16 ms |
31596 KB |
Output is correct |
12 |
Correct |
15 ms |
31560 KB |
Output is correct |
13 |
Correct |
17 ms |
31596 KB |
Output is correct |
14 |
Correct |
15 ms |
31568 KB |
Output is correct |
15 |
Correct |
15 ms |
31572 KB |
Output is correct |
16 |
Correct |
16 ms |
31596 KB |
Output is correct |
17 |
Correct |
16 ms |
31552 KB |
Output is correct |
18 |
Correct |
17 ms |
31596 KB |
Output is correct |
19 |
Correct |
16 ms |
31640 KB |
Output is correct |
20 |
Correct |
16 ms |
31600 KB |
Output is correct |
21 |
Correct |
15 ms |
31572 KB |
Output is correct |
22 |
Correct |
15 ms |
31600 KB |
Output is correct |
23 |
Correct |
15 ms |
31572 KB |
Output is correct |
24 |
Correct |
16 ms |
31552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
31572 KB |
Output is correct |
2 |
Correct |
15 ms |
31572 KB |
Output is correct |
3 |
Correct |
16 ms |
31700 KB |
Output is correct |
4 |
Correct |
16 ms |
31572 KB |
Output is correct |
5 |
Correct |
16 ms |
31600 KB |
Output is correct |
6 |
Correct |
17 ms |
31556 KB |
Output is correct |
7 |
Correct |
17 ms |
31572 KB |
Output is correct |
8 |
Correct |
16 ms |
31572 KB |
Output is correct |
9 |
Correct |
15 ms |
31572 KB |
Output is correct |
10 |
Correct |
16 ms |
31572 KB |
Output is correct |
11 |
Correct |
16 ms |
31596 KB |
Output is correct |
12 |
Correct |
15 ms |
31560 KB |
Output is correct |
13 |
Correct |
17 ms |
31596 KB |
Output is correct |
14 |
Correct |
15 ms |
31568 KB |
Output is correct |
15 |
Correct |
15 ms |
31572 KB |
Output is correct |
16 |
Correct |
16 ms |
31596 KB |
Output is correct |
17 |
Correct |
16 ms |
31552 KB |
Output is correct |
18 |
Correct |
17 ms |
31596 KB |
Output is correct |
19 |
Correct |
16 ms |
31640 KB |
Output is correct |
20 |
Correct |
16 ms |
31600 KB |
Output is correct |
21 |
Correct |
15 ms |
31572 KB |
Output is correct |
22 |
Correct |
15 ms |
31600 KB |
Output is correct |
23 |
Correct |
15 ms |
31572 KB |
Output is correct |
24 |
Correct |
16 ms |
31552 KB |
Output is correct |
25 |
Correct |
16 ms |
31572 KB |
Output is correct |
26 |
Correct |
18 ms |
31960 KB |
Output is correct |
27 |
Correct |
18 ms |
31824 KB |
Output is correct |
28 |
Correct |
18 ms |
31952 KB |
Output is correct |
29 |
Correct |
20 ms |
31936 KB |
Output is correct |
30 |
Correct |
18 ms |
31872 KB |
Output is correct |
31 |
Correct |
16 ms |
31656 KB |
Output is correct |
32 |
Correct |
18 ms |
32120 KB |
Output is correct |
33 |
Correct |
16 ms |
31572 KB |
Output is correct |
34 |
Correct |
17 ms |
31828 KB |
Output is correct |
35 |
Correct |
18 ms |
31828 KB |
Output is correct |
36 |
Correct |
19 ms |
31868 KB |
Output is correct |
37 |
Correct |
18 ms |
31924 KB |
Output is correct |
38 |
Correct |
16 ms |
31596 KB |
Output is correct |
39 |
Correct |
19 ms |
31976 KB |
Output is correct |
40 |
Correct |
18 ms |
31828 KB |
Output is correct |
41 |
Correct |
17 ms |
31868 KB |
Output is correct |
42 |
Correct |
18 ms |
31860 KB |
Output is correct |
43 |
Correct |
19 ms |
32104 KB |
Output is correct |
44 |
Correct |
16 ms |
31580 KB |
Output is correct |
45 |
Correct |
17 ms |
31900 KB |
Output is correct |
46 |
Correct |
17 ms |
31828 KB |
Output is correct |
47 |
Correct |
16 ms |
31600 KB |
Output is correct |
48 |
Correct |
20 ms |
31820 KB |
Output is correct |
49 |
Correct |
19 ms |
31868 KB |
Output is correct |
50 |
Correct |
19 ms |
31916 KB |
Output is correct |
51 |
Correct |
19 ms |
31840 KB |
Output is correct |
52 |
Correct |
17 ms |
31884 KB |
Output is correct |
53 |
Correct |
18 ms |
31916 KB |
Output is correct |
54 |
Correct |
19 ms |
31772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
31572 KB |
Output is correct |
2 |
Correct |
15 ms |
31572 KB |
Output is correct |
3 |
Correct |
16 ms |
31700 KB |
Output is correct |
4 |
Correct |
16 ms |
31572 KB |
Output is correct |
5 |
Correct |
16 ms |
31600 KB |
Output is correct |
6 |
Correct |
17 ms |
31556 KB |
Output is correct |
7 |
Correct |
17 ms |
31572 KB |
Output is correct |
8 |
Correct |
16 ms |
31572 KB |
Output is correct |
9 |
Correct |
15 ms |
31572 KB |
Output is correct |
10 |
Correct |
16 ms |
31572 KB |
Output is correct |
11 |
Correct |
16 ms |
31596 KB |
Output is correct |
12 |
Correct |
15 ms |
31560 KB |
Output is correct |
13 |
Correct |
17 ms |
31596 KB |
Output is correct |
14 |
Correct |
15 ms |
31568 KB |
Output is correct |
15 |
Correct |
15 ms |
31572 KB |
Output is correct |
16 |
Correct |
16 ms |
31596 KB |
Output is correct |
17 |
Correct |
16 ms |
31552 KB |
Output is correct |
18 |
Correct |
17 ms |
31596 KB |
Output is correct |
19 |
Correct |
16 ms |
31640 KB |
Output is correct |
20 |
Correct |
16 ms |
31600 KB |
Output is correct |
21 |
Correct |
15 ms |
31572 KB |
Output is correct |
22 |
Correct |
15 ms |
31600 KB |
Output is correct |
23 |
Correct |
15 ms |
31572 KB |
Output is correct |
24 |
Correct |
16 ms |
31552 KB |
Output is correct |
25 |
Correct |
17 ms |
31572 KB |
Output is correct |
26 |
Correct |
75 ms |
37992 KB |
Output is correct |
27 |
Correct |
88 ms |
37840 KB |
Output is correct |
28 |
Correct |
17 ms |
31828 KB |
Output is correct |
29 |
Correct |
16 ms |
31540 KB |
Output is correct |
30 |
Correct |
17 ms |
31604 KB |
Output is correct |
31 |
Correct |
87 ms |
37864 KB |
Output is correct |
32 |
Correct |
18 ms |
31868 KB |
Output is correct |
33 |
Correct |
106 ms |
45280 KB |
Output is correct |
34 |
Correct |
92 ms |
37876 KB |
Output is correct |
35 |
Correct |
18 ms |
31828 KB |
Output is correct |
36 |
Correct |
96 ms |
38424 KB |
Output is correct |
37 |
Correct |
18 ms |
31828 KB |
Output is correct |
38 |
Correct |
18 ms |
31828 KB |
Output is correct |
39 |
Correct |
77 ms |
37984 KB |
Output is correct |
40 |
Correct |
19 ms |
31996 KB |
Output is correct |
41 |
Correct |
86 ms |
37836 KB |
Output is correct |
42 |
Correct |
105 ms |
44532 KB |
Output is correct |
43 |
Correct |
18 ms |
31572 KB |
Output is correct |
44 |
Correct |
102 ms |
45108 KB |
Output is correct |
45 |
Correct |
95 ms |
41328 KB |
Output is correct |
46 |
Correct |
18 ms |
31772 KB |
Output is correct |
47 |
Correct |
18 ms |
31828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
37572 KB |
Output is correct |
2 |
Correct |
114 ms |
40152 KB |
Output is correct |
3 |
Correct |
19 ms |
31800 KB |
Output is correct |
4 |
Correct |
19 ms |
31868 KB |
Output is correct |
5 |
Correct |
18 ms |
31772 KB |
Output is correct |
6 |
Correct |
17 ms |
31536 KB |
Output is correct |
7 |
Correct |
19 ms |
31828 KB |
Output is correct |
8 |
Correct |
115 ms |
38808 KB |
Output is correct |
9 |
Correct |
19 ms |
31836 KB |
Output is correct |
10 |
Correct |
91 ms |
37892 KB |
Output is correct |
11 |
Correct |
18 ms |
31572 KB |
Output is correct |
12 |
Correct |
104 ms |
37680 KB |
Output is correct |
13 |
Correct |
112 ms |
38788 KB |
Output is correct |
14 |
Correct |
116 ms |
40296 KB |
Output is correct |
15 |
Correct |
76 ms |
37928 KB |
Output is correct |
16 |
Correct |
19 ms |
31868 KB |
Output is correct |
17 |
Correct |
17 ms |
31636 KB |
Output is correct |
18 |
Correct |
101 ms |
40204 KB |
Output is correct |
19 |
Correct |
122 ms |
44196 KB |
Output is correct |
20 |
Correct |
20 ms |
31868 KB |
Output is correct |
21 |
Correct |
17 ms |
31664 KB |
Output is correct |
22 |
Correct |
94 ms |
38992 KB |
Output is correct |
23 |
Correct |
19 ms |
31868 KB |
Output is correct |
24 |
Correct |
107 ms |
38280 KB |
Output is correct |
25 |
Correct |
126 ms |
42836 KB |
Output is correct |
26 |
Correct |
19 ms |
31956 KB |
Output is correct |
27 |
Correct |
19 ms |
31992 KB |
Output is correct |
28 |
Correct |
21 ms |
31868 KB |
Output is correct |
29 |
Correct |
22 ms |
31852 KB |
Output is correct |
30 |
Correct |
21 ms |
31828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
31572 KB |
Output is correct |
2 |
Correct |
15 ms |
31572 KB |
Output is correct |
3 |
Correct |
16 ms |
31700 KB |
Output is correct |
4 |
Correct |
16 ms |
31572 KB |
Output is correct |
5 |
Correct |
16 ms |
31600 KB |
Output is correct |
6 |
Correct |
17 ms |
31556 KB |
Output is correct |
7 |
Correct |
17 ms |
31572 KB |
Output is correct |
8 |
Correct |
16 ms |
31572 KB |
Output is correct |
9 |
Correct |
15 ms |
31572 KB |
Output is correct |
10 |
Correct |
16 ms |
31572 KB |
Output is correct |
11 |
Correct |
16 ms |
31596 KB |
Output is correct |
12 |
Correct |
15 ms |
31560 KB |
Output is correct |
13 |
Correct |
17 ms |
31596 KB |
Output is correct |
14 |
Correct |
15 ms |
31568 KB |
Output is correct |
15 |
Correct |
15 ms |
31572 KB |
Output is correct |
16 |
Correct |
16 ms |
31596 KB |
Output is correct |
17 |
Correct |
16 ms |
31552 KB |
Output is correct |
18 |
Correct |
17 ms |
31596 KB |
Output is correct |
19 |
Correct |
16 ms |
31640 KB |
Output is correct |
20 |
Correct |
16 ms |
31600 KB |
Output is correct |
21 |
Correct |
15 ms |
31572 KB |
Output is correct |
22 |
Correct |
15 ms |
31600 KB |
Output is correct |
23 |
Correct |
15 ms |
31572 KB |
Output is correct |
24 |
Correct |
16 ms |
31552 KB |
Output is correct |
25 |
Correct |
16 ms |
31572 KB |
Output is correct |
26 |
Correct |
18 ms |
31960 KB |
Output is correct |
27 |
Correct |
18 ms |
31824 KB |
Output is correct |
28 |
Correct |
18 ms |
31952 KB |
Output is correct |
29 |
Correct |
20 ms |
31936 KB |
Output is correct |
30 |
Correct |
18 ms |
31872 KB |
Output is correct |
31 |
Correct |
16 ms |
31656 KB |
Output is correct |
32 |
Correct |
18 ms |
32120 KB |
Output is correct |
33 |
Correct |
16 ms |
31572 KB |
Output is correct |
34 |
Correct |
17 ms |
31828 KB |
Output is correct |
35 |
Correct |
18 ms |
31828 KB |
Output is correct |
36 |
Correct |
19 ms |
31868 KB |
Output is correct |
37 |
Correct |
18 ms |
31924 KB |
Output is correct |
38 |
Correct |
16 ms |
31596 KB |
Output is correct |
39 |
Correct |
19 ms |
31976 KB |
Output is correct |
40 |
Correct |
18 ms |
31828 KB |
Output is correct |
41 |
Correct |
17 ms |
31868 KB |
Output is correct |
42 |
Correct |
18 ms |
31860 KB |
Output is correct |
43 |
Correct |
19 ms |
32104 KB |
Output is correct |
44 |
Correct |
16 ms |
31580 KB |
Output is correct |
45 |
Correct |
17 ms |
31900 KB |
Output is correct |
46 |
Correct |
17 ms |
31828 KB |
Output is correct |
47 |
Correct |
16 ms |
31600 KB |
Output is correct |
48 |
Correct |
20 ms |
31820 KB |
Output is correct |
49 |
Correct |
19 ms |
31868 KB |
Output is correct |
50 |
Correct |
19 ms |
31916 KB |
Output is correct |
51 |
Correct |
19 ms |
31840 KB |
Output is correct |
52 |
Correct |
17 ms |
31884 KB |
Output is correct |
53 |
Correct |
18 ms |
31916 KB |
Output is correct |
54 |
Correct |
19 ms |
31772 KB |
Output is correct |
55 |
Correct |
17 ms |
31572 KB |
Output is correct |
56 |
Correct |
75 ms |
37992 KB |
Output is correct |
57 |
Correct |
88 ms |
37840 KB |
Output is correct |
58 |
Correct |
17 ms |
31828 KB |
Output is correct |
59 |
Correct |
16 ms |
31540 KB |
Output is correct |
60 |
Correct |
17 ms |
31604 KB |
Output is correct |
61 |
Correct |
87 ms |
37864 KB |
Output is correct |
62 |
Correct |
18 ms |
31868 KB |
Output is correct |
63 |
Correct |
106 ms |
45280 KB |
Output is correct |
64 |
Correct |
92 ms |
37876 KB |
Output is correct |
65 |
Correct |
18 ms |
31828 KB |
Output is correct |
66 |
Correct |
96 ms |
38424 KB |
Output is correct |
67 |
Correct |
18 ms |
31828 KB |
Output is correct |
68 |
Correct |
18 ms |
31828 KB |
Output is correct |
69 |
Correct |
77 ms |
37984 KB |
Output is correct |
70 |
Correct |
19 ms |
31996 KB |
Output is correct |
71 |
Correct |
86 ms |
37836 KB |
Output is correct |
72 |
Correct |
105 ms |
44532 KB |
Output is correct |
73 |
Correct |
18 ms |
31572 KB |
Output is correct |
74 |
Correct |
102 ms |
45108 KB |
Output is correct |
75 |
Correct |
95 ms |
41328 KB |
Output is correct |
76 |
Correct |
18 ms |
31772 KB |
Output is correct |
77 |
Correct |
18 ms |
31828 KB |
Output is correct |
78 |
Correct |
76 ms |
37572 KB |
Output is correct |
79 |
Correct |
114 ms |
40152 KB |
Output is correct |
80 |
Correct |
19 ms |
31800 KB |
Output is correct |
81 |
Correct |
19 ms |
31868 KB |
Output is correct |
82 |
Correct |
18 ms |
31772 KB |
Output is correct |
83 |
Correct |
17 ms |
31536 KB |
Output is correct |
84 |
Correct |
19 ms |
31828 KB |
Output is correct |
85 |
Correct |
115 ms |
38808 KB |
Output is correct |
86 |
Correct |
19 ms |
31836 KB |
Output is correct |
87 |
Correct |
91 ms |
37892 KB |
Output is correct |
88 |
Correct |
18 ms |
31572 KB |
Output is correct |
89 |
Correct |
104 ms |
37680 KB |
Output is correct |
90 |
Correct |
112 ms |
38788 KB |
Output is correct |
91 |
Correct |
116 ms |
40296 KB |
Output is correct |
92 |
Correct |
76 ms |
37928 KB |
Output is correct |
93 |
Correct |
19 ms |
31868 KB |
Output is correct |
94 |
Correct |
17 ms |
31636 KB |
Output is correct |
95 |
Correct |
101 ms |
40204 KB |
Output is correct |
96 |
Correct |
122 ms |
44196 KB |
Output is correct |
97 |
Correct |
20 ms |
31868 KB |
Output is correct |
98 |
Correct |
17 ms |
31664 KB |
Output is correct |
99 |
Correct |
94 ms |
38992 KB |
Output is correct |
100 |
Correct |
19 ms |
31868 KB |
Output is correct |
101 |
Correct |
107 ms |
38280 KB |
Output is correct |
102 |
Correct |
126 ms |
42836 KB |
Output is correct |
103 |
Correct |
19 ms |
31956 KB |
Output is correct |
104 |
Correct |
19 ms |
31992 KB |
Output is correct |
105 |
Correct |
21 ms |
31868 KB |
Output is correct |
106 |
Correct |
22 ms |
31852 KB |
Output is correct |
107 |
Correct |
21 ms |
31828 KB |
Output is correct |
108 |
Correct |
652 ms |
65888 KB |
Output is correct |
109 |
Correct |
800 ms |
88096 KB |
Output is correct |
110 |
Correct |
816 ms |
95672 KB |
Output is correct |
111 |
Correct |
943 ms |
96660 KB |
Output is correct |
112 |
Correct |
874 ms |
86780 KB |
Output is correct |
113 |
Correct |
642 ms |
73504 KB |
Output is correct |
114 |
Correct |
647 ms |
61464 KB |
Output is correct |
115 |
Correct |
638 ms |
61312 KB |
Output is correct |
116 |
Correct |
813 ms |
66372 KB |
Output is correct |
117 |
Correct |
816 ms |
74156 KB |
Output is correct |
118 |
Correct |
636 ms |
61772 KB |
Output is correct |
119 |
Correct |
833 ms |
74220 KB |
Output is correct |
120 |
Correct |
855 ms |
87652 KB |
Output is correct |
121 |
Correct |
792 ms |
74208 KB |
Output is correct |
122 |
Correct |
900 ms |
90008 KB |
Output is correct |
123 |
Correct |
615 ms |
75028 KB |
Output is correct |
124 |
Correct |
874 ms |
72680 KB |
Output is correct |