#include <bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i = a;i<=b;i++)
#define ll long long int
#define pb push_back
#define vi vector<int>
#define ff first
#define ss second
#define vv vector
#define ii pair<int,int>
using namespace std;
const int MAXN = 5e5 + 1;
const int LOGN = 20;
int n,k;
vi g[MAXN];
int color[MAXN];
vi verticesOfColor[MAXN];
int sparseTable[LOGN][MAXN];
int depth[MAXN];
int degree[MAXN];
void dfs_setup(int node,int p = -1){
sparseTable[0][node] = p;
if(p == -1)depth[node] = -1;else depth[node] = depth[p] + 1;
for(auto e : g[node]){
if(e != p)dfs_setup(e,node);
}
}
struct DSU{
int parent[MAXN];
int ID[MAXN];
void init(){
FOR(i,n) parent[i] = i;
FOR(i,n) ID[i] = i;
}
inline int find(int a){
if(parent[a] != a)parent[a] = find(parent[a]);
return parent[a];
}
inline void merge(int a,int b){
int pa = find(a);
int pb = find(b);
if(depth[pa] > depth[pb])swap(pa,pb);
parent[pb] = pa;
//parent[pa] = pb;
//if(depth[pa] < depth[pb])ID[pb] = pa;else ID[pb] = pa;
}
inline int id(int a){
return find(a);
//return ID[find(a)];
}
inline bool isSame(int a,int b){
return find(a) == find(b);
}
} dsu;
bool done[MAXN];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
vv<ii> allE;
FOR(i,n-1){
int a,b;cin >> a >> b;a--;b--;
g[a].pb(b);g[b].pb(a);
allE.pb({a,b});
}
dfs_setup(0);
FOR(i,n){
int a;cin >> a;a--;
color[i] = a;
verticesOfColor[a].pb(i);
}
dsu.init();
FOR(i,k){
if(verticesOfColor[i].size() == 1)continue;
FORE(j,1,(int)verticesOfColor[i].size()-1){
int node1 = dsu.id(verticesOfColor[i][j]);
int node2 = dsu.id(verticesOfColor[i][j-1]);
while(node1 != node2){
if(depth[node1] > depth[node2])swap(node1,node2);
int p = dsu.id(sparseTable[0][node2]);
if(p == -1)p = 0;
dsu.merge(p,node2);
// cout << node2 << " " << node1 << " " << p << endl;
node2 = p;
}
}
}
//FOR(i,n)cout << dsu.find(i) << endl;
for(auto e : allE)if(!dsu.isSame(e.ff,e.ss))degree[dsu.find(e.ff)]++,degree[dsu.find(e.ss)]++;
int cnt = 1;
FOR(i,n)if(degree[i] == 1)cnt++;
cout << cnt/2 << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23800 KB |
Output is correct |
5 |
Correct |
29 ms |
23928 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23900 KB |
Output is correct |
8 |
Correct |
24 ms |
23928 KB |
Output is correct |
9 |
Correct |
24 ms |
23928 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
11 |
Correct |
24 ms |
23928 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
25 ms |
23928 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
24 ms |
23900 KB |
Output is correct |
17 |
Correct |
24 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
28 ms |
23928 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
29 ms |
23928 KB |
Output is correct |
22 |
Correct |
24 ms |
24028 KB |
Output is correct |
23 |
Correct |
24 ms |
23932 KB |
Output is correct |
24 |
Correct |
28 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23800 KB |
Output is correct |
5 |
Correct |
29 ms |
23928 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23900 KB |
Output is correct |
8 |
Correct |
24 ms |
23928 KB |
Output is correct |
9 |
Correct |
24 ms |
23928 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
11 |
Correct |
24 ms |
23928 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
25 ms |
23928 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
24 ms |
23900 KB |
Output is correct |
17 |
Correct |
24 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
28 ms |
23928 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
29 ms |
23928 KB |
Output is correct |
22 |
Correct |
24 ms |
24028 KB |
Output is correct |
23 |
Correct |
24 ms |
23932 KB |
Output is correct |
24 |
Correct |
28 ms |
23928 KB |
Output is correct |
25 |
Correct |
23 ms |
23900 KB |
Output is correct |
26 |
Correct |
26 ms |
24184 KB |
Output is correct |
27 |
Correct |
26 ms |
24056 KB |
Output is correct |
28 |
Correct |
26 ms |
24184 KB |
Output is correct |
29 |
Correct |
26 ms |
24184 KB |
Output is correct |
30 |
Correct |
26 ms |
24148 KB |
Output is correct |
31 |
Correct |
23 ms |
23928 KB |
Output is correct |
32 |
Correct |
26 ms |
24312 KB |
Output is correct |
33 |
Correct |
24 ms |
23928 KB |
Output is correct |
34 |
Correct |
26 ms |
24056 KB |
Output is correct |
35 |
Correct |
26 ms |
24184 KB |
Output is correct |
36 |
Correct |
26 ms |
24056 KB |
Output is correct |
37 |
Correct |
26 ms |
24184 KB |
Output is correct |
38 |
Correct |
24 ms |
23932 KB |
Output is correct |
39 |
Correct |
26 ms |
24156 KB |
Output is correct |
40 |
Correct |
26 ms |
24184 KB |
Output is correct |
41 |
Correct |
26 ms |
24056 KB |
Output is correct |
42 |
Correct |
26 ms |
24184 KB |
Output is correct |
43 |
Correct |
26 ms |
24312 KB |
Output is correct |
44 |
Correct |
24 ms |
23928 KB |
Output is correct |
45 |
Correct |
26 ms |
24184 KB |
Output is correct |
46 |
Correct |
26 ms |
24184 KB |
Output is correct |
47 |
Correct |
24 ms |
23800 KB |
Output is correct |
48 |
Correct |
26 ms |
24184 KB |
Output is correct |
49 |
Correct |
26 ms |
24184 KB |
Output is correct |
50 |
Correct |
26 ms |
24312 KB |
Output is correct |
51 |
Correct |
26 ms |
24184 KB |
Output is correct |
52 |
Correct |
26 ms |
24184 KB |
Output is correct |
53 |
Correct |
31 ms |
24188 KB |
Output is correct |
54 |
Correct |
25 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23800 KB |
Output is correct |
5 |
Correct |
29 ms |
23928 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23900 KB |
Output is correct |
8 |
Correct |
24 ms |
23928 KB |
Output is correct |
9 |
Correct |
24 ms |
23928 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
11 |
Correct |
24 ms |
23928 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
25 ms |
23928 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
24 ms |
23900 KB |
Output is correct |
17 |
Correct |
24 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
28 ms |
23928 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
29 ms |
23928 KB |
Output is correct |
22 |
Correct |
24 ms |
24028 KB |
Output is correct |
23 |
Correct |
24 ms |
23932 KB |
Output is correct |
24 |
Correct |
28 ms |
23928 KB |
Output is correct |
25 |
Correct |
28 ms |
23800 KB |
Output is correct |
26 |
Correct |
85 ms |
31296 KB |
Output is correct |
27 |
Correct |
105 ms |
30920 KB |
Output is correct |
28 |
Correct |
26 ms |
24056 KB |
Output is correct |
29 |
Correct |
23 ms |
23800 KB |
Output is correct |
30 |
Correct |
24 ms |
23928 KB |
Output is correct |
31 |
Correct |
106 ms |
31088 KB |
Output is correct |
32 |
Correct |
25 ms |
24184 KB |
Output is correct |
33 |
Correct |
151 ms |
35180 KB |
Output is correct |
34 |
Correct |
103 ms |
30872 KB |
Output is correct |
35 |
Correct |
26 ms |
24184 KB |
Output is correct |
36 |
Correct |
111 ms |
31336 KB |
Output is correct |
37 |
Correct |
30 ms |
24056 KB |
Output is correct |
38 |
Correct |
26 ms |
24056 KB |
Output is correct |
39 |
Correct |
90 ms |
31316 KB |
Output is correct |
40 |
Correct |
25 ms |
24288 KB |
Output is correct |
41 |
Correct |
116 ms |
31340 KB |
Output is correct |
42 |
Correct |
113 ms |
32232 KB |
Output is correct |
43 |
Correct |
24 ms |
23928 KB |
Output is correct |
44 |
Correct |
122 ms |
35704 KB |
Output is correct |
45 |
Correct |
113 ms |
33108 KB |
Output is correct |
46 |
Correct |
25 ms |
24056 KB |
Output is correct |
47 |
Correct |
25 ms |
24184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
31052 KB |
Output is correct |
2 |
Correct |
100 ms |
34140 KB |
Output is correct |
3 |
Correct |
26 ms |
24184 KB |
Output is correct |
4 |
Correct |
26 ms |
24156 KB |
Output is correct |
5 |
Correct |
27 ms |
23860 KB |
Output is correct |
6 |
Correct |
27 ms |
23928 KB |
Output is correct |
7 |
Correct |
25 ms |
24184 KB |
Output is correct |
8 |
Correct |
124 ms |
32360 KB |
Output is correct |
9 |
Correct |
26 ms |
24184 KB |
Output is correct |
10 |
Correct |
108 ms |
31292 KB |
Output is correct |
11 |
Correct |
24 ms |
23928 KB |
Output is correct |
12 |
Correct |
120 ms |
31208 KB |
Output is correct |
13 |
Correct |
133 ms |
32364 KB |
Output is correct |
14 |
Correct |
125 ms |
33940 KB |
Output is correct |
15 |
Correct |
91 ms |
31208 KB |
Output is correct |
16 |
Correct |
26 ms |
24184 KB |
Output is correct |
17 |
Correct |
24 ms |
23920 KB |
Output is correct |
18 |
Correct |
113 ms |
33900 KB |
Output is correct |
19 |
Correct |
186 ms |
36268 KB |
Output is correct |
20 |
Correct |
26 ms |
24184 KB |
Output is correct |
21 |
Correct |
24 ms |
24056 KB |
Output is correct |
22 |
Correct |
100 ms |
32616 KB |
Output is correct |
23 |
Correct |
26 ms |
24184 KB |
Output is correct |
24 |
Correct |
117 ms |
31704 KB |
Output is correct |
25 |
Correct |
112 ms |
35532 KB |
Output is correct |
26 |
Correct |
26 ms |
24184 KB |
Output is correct |
27 |
Correct |
26 ms |
24312 KB |
Output is correct |
28 |
Correct |
26 ms |
24184 KB |
Output is correct |
29 |
Correct |
25 ms |
24184 KB |
Output is correct |
30 |
Correct |
30 ms |
24088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23800 KB |
Output is correct |
5 |
Correct |
29 ms |
23928 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23900 KB |
Output is correct |
8 |
Correct |
24 ms |
23928 KB |
Output is correct |
9 |
Correct |
24 ms |
23928 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
11 |
Correct |
24 ms |
23928 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
25 ms |
23928 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
24 ms |
23900 KB |
Output is correct |
17 |
Correct |
24 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
28 ms |
23928 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
29 ms |
23928 KB |
Output is correct |
22 |
Correct |
24 ms |
24028 KB |
Output is correct |
23 |
Correct |
24 ms |
23932 KB |
Output is correct |
24 |
Correct |
28 ms |
23928 KB |
Output is correct |
25 |
Correct |
23 ms |
23900 KB |
Output is correct |
26 |
Correct |
26 ms |
24184 KB |
Output is correct |
27 |
Correct |
26 ms |
24056 KB |
Output is correct |
28 |
Correct |
26 ms |
24184 KB |
Output is correct |
29 |
Correct |
26 ms |
24184 KB |
Output is correct |
30 |
Correct |
26 ms |
24148 KB |
Output is correct |
31 |
Correct |
23 ms |
23928 KB |
Output is correct |
32 |
Correct |
26 ms |
24312 KB |
Output is correct |
33 |
Correct |
24 ms |
23928 KB |
Output is correct |
34 |
Correct |
26 ms |
24056 KB |
Output is correct |
35 |
Correct |
26 ms |
24184 KB |
Output is correct |
36 |
Correct |
26 ms |
24056 KB |
Output is correct |
37 |
Correct |
26 ms |
24184 KB |
Output is correct |
38 |
Correct |
24 ms |
23932 KB |
Output is correct |
39 |
Correct |
26 ms |
24156 KB |
Output is correct |
40 |
Correct |
26 ms |
24184 KB |
Output is correct |
41 |
Correct |
26 ms |
24056 KB |
Output is correct |
42 |
Correct |
26 ms |
24184 KB |
Output is correct |
43 |
Correct |
26 ms |
24312 KB |
Output is correct |
44 |
Correct |
24 ms |
23928 KB |
Output is correct |
45 |
Correct |
26 ms |
24184 KB |
Output is correct |
46 |
Correct |
26 ms |
24184 KB |
Output is correct |
47 |
Correct |
24 ms |
23800 KB |
Output is correct |
48 |
Correct |
26 ms |
24184 KB |
Output is correct |
49 |
Correct |
26 ms |
24184 KB |
Output is correct |
50 |
Correct |
26 ms |
24312 KB |
Output is correct |
51 |
Correct |
26 ms |
24184 KB |
Output is correct |
52 |
Correct |
26 ms |
24184 KB |
Output is correct |
53 |
Correct |
31 ms |
24188 KB |
Output is correct |
54 |
Correct |
25 ms |
24056 KB |
Output is correct |
55 |
Correct |
28 ms |
23800 KB |
Output is correct |
56 |
Correct |
85 ms |
31296 KB |
Output is correct |
57 |
Correct |
105 ms |
30920 KB |
Output is correct |
58 |
Correct |
26 ms |
24056 KB |
Output is correct |
59 |
Correct |
23 ms |
23800 KB |
Output is correct |
60 |
Correct |
24 ms |
23928 KB |
Output is correct |
61 |
Correct |
106 ms |
31088 KB |
Output is correct |
62 |
Correct |
25 ms |
24184 KB |
Output is correct |
63 |
Correct |
151 ms |
35180 KB |
Output is correct |
64 |
Correct |
103 ms |
30872 KB |
Output is correct |
65 |
Correct |
26 ms |
24184 KB |
Output is correct |
66 |
Correct |
111 ms |
31336 KB |
Output is correct |
67 |
Correct |
30 ms |
24056 KB |
Output is correct |
68 |
Correct |
26 ms |
24056 KB |
Output is correct |
69 |
Correct |
90 ms |
31316 KB |
Output is correct |
70 |
Correct |
25 ms |
24288 KB |
Output is correct |
71 |
Correct |
116 ms |
31340 KB |
Output is correct |
72 |
Correct |
113 ms |
32232 KB |
Output is correct |
73 |
Correct |
24 ms |
23928 KB |
Output is correct |
74 |
Correct |
122 ms |
35704 KB |
Output is correct |
75 |
Correct |
113 ms |
33108 KB |
Output is correct |
76 |
Correct |
25 ms |
24056 KB |
Output is correct |
77 |
Correct |
25 ms |
24184 KB |
Output is correct |
78 |
Correct |
87 ms |
31052 KB |
Output is correct |
79 |
Correct |
100 ms |
34140 KB |
Output is correct |
80 |
Correct |
26 ms |
24184 KB |
Output is correct |
81 |
Correct |
26 ms |
24156 KB |
Output is correct |
82 |
Correct |
27 ms |
23860 KB |
Output is correct |
83 |
Correct |
27 ms |
23928 KB |
Output is correct |
84 |
Correct |
25 ms |
24184 KB |
Output is correct |
85 |
Correct |
124 ms |
32360 KB |
Output is correct |
86 |
Correct |
26 ms |
24184 KB |
Output is correct |
87 |
Correct |
108 ms |
31292 KB |
Output is correct |
88 |
Correct |
24 ms |
23928 KB |
Output is correct |
89 |
Correct |
120 ms |
31208 KB |
Output is correct |
90 |
Correct |
133 ms |
32364 KB |
Output is correct |
91 |
Correct |
125 ms |
33940 KB |
Output is correct |
92 |
Correct |
91 ms |
31208 KB |
Output is correct |
93 |
Correct |
26 ms |
24184 KB |
Output is correct |
94 |
Correct |
24 ms |
23920 KB |
Output is correct |
95 |
Correct |
113 ms |
33900 KB |
Output is correct |
96 |
Correct |
186 ms |
36268 KB |
Output is correct |
97 |
Correct |
26 ms |
24184 KB |
Output is correct |
98 |
Correct |
24 ms |
24056 KB |
Output is correct |
99 |
Correct |
100 ms |
32616 KB |
Output is correct |
100 |
Correct |
26 ms |
24184 KB |
Output is correct |
101 |
Correct |
117 ms |
31704 KB |
Output is correct |
102 |
Correct |
112 ms |
35532 KB |
Output is correct |
103 |
Correct |
26 ms |
24184 KB |
Output is correct |
104 |
Correct |
26 ms |
24312 KB |
Output is correct |
105 |
Correct |
26 ms |
24184 KB |
Output is correct |
106 |
Correct |
25 ms |
24184 KB |
Output is correct |
107 |
Correct |
30 ms |
24088 KB |
Output is correct |
108 |
Correct |
564 ms |
64264 KB |
Output is correct |
109 |
Correct |
688 ms |
68588 KB |
Output is correct |
110 |
Correct |
698 ms |
75744 KB |
Output is correct |
111 |
Correct |
826 ms |
85592 KB |
Output is correct |
112 |
Correct |
831 ms |
79580 KB |
Output is correct |
113 |
Correct |
724 ms |
71860 KB |
Output is correct |
114 |
Correct |
586 ms |
57564 KB |
Output is correct |
115 |
Correct |
574 ms |
58332 KB |
Output is correct |
116 |
Correct |
739 ms |
60776 KB |
Output is correct |
117 |
Correct |
723 ms |
71772 KB |
Output is correct |
118 |
Correct |
609 ms |
58164 KB |
Output is correct |
119 |
Correct |
743 ms |
71964 KB |
Output is correct |
120 |
Correct |
752 ms |
79840 KB |
Output is correct |
121 |
Correct |
737 ms |
71748 KB |
Output is correct |
122 |
Correct |
817 ms |
65372 KB |
Output is correct |
123 |
Correct |
531 ms |
73176 KB |
Output is correct |
124 |
Correct |
784 ms |
70636 KB |
Output is correct |