//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 500015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
struct DSU{
int boss[N],sz[N],group=0;
void init(int n){
group=n;
for (int i=0;i<=n+1;i++){
boss[i]=i;
sz[i]=1;
}
}
int find(int x){
if (boss[x]==x) return x;
return boss[x]=find(boss[x]);
}
void Merge(int a,int b){
int ra=find(a),rb=find(b);
if (ra==rb) return;
group--;
if (sz[ra]<sz[rb]) swap(ra,rb);
sz[ra]+=sz[rb];
boss[rb]=ra;
}
}d;
int n,k,fa[N],dep[N],is[N],deg[N],cnt=0;
vector<int>g[N],all[N];
void dfs(int p,int lp){
fa[p]=lp; dep[p]=dep[lp]+1;
for (auto i:g[p]){
if (i!=lp)
dfs(i,p);
}
}
void merge_path(int a,int b){
vector<int>path;
// cout<<"Go "<<a<<" "<<b<<'\n';
while (a!=b){
if (dep[a]>=dep[b]){
path.push_back(a);
d.Merge(is[a],is[fa[a]]);
a=fa[a];
}
else {
path.push_back(b);
d.Merge(is[b],is[fa[b]]);
b=fa[b];
}
}
/*
cout<<"path : ";
for (auto i:path){
cout<<i<<" ";
fa[i]=a;
}
cout<<a<<'\n';
*/
}
signed main(){
fast
cin>>n>>k;
d.init(k);
vector<pii>edge;
for (int i=1;i<n;i++){
int a,b; cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
edge.push_back({a,b});
}
for (int i=1;i<=n;i++){
cin>>is[i];
all[is[i]].push_back(i);
}
dfs(1,1);
for (int i=1;i<=k;i++){
for (int j=1;j<all[i].size();j++)
merge_path(all[i][j-1],all[i][j]);
}
for (auto i:edge){
if (d.find(is[i.x])!=d.find(is[i.y])){
deg[d.find(is[i.x])]++;
deg[d.find(is[i.y])]++;
}
}
for (int i=1;i<=k;i++)
cnt+=(deg[i]==1);
cout<<(cnt+1)/2<<'\n';
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:89:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int j=1;j<all[i].size();j++)
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23820 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
13 ms |
23748 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23832 KB |
Output is correct |
6 |
Correct |
13 ms |
23868 KB |
Output is correct |
7 |
Correct |
13 ms |
23824 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
11 ms |
23828 KB |
Output is correct |
14 |
Correct |
11 ms |
23820 KB |
Output is correct |
15 |
Correct |
12 ms |
23868 KB |
Output is correct |
16 |
Correct |
12 ms |
23824 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23748 KB |
Output is correct |
19 |
Correct |
14 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23820 KB |
Output is correct |
21 |
Correct |
13 ms |
23824 KB |
Output is correct |
22 |
Correct |
14 ms |
23824 KB |
Output is correct |
23 |
Correct |
14 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23820 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
13 ms |
23748 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23832 KB |
Output is correct |
6 |
Correct |
13 ms |
23868 KB |
Output is correct |
7 |
Correct |
13 ms |
23824 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
11 ms |
23828 KB |
Output is correct |
14 |
Correct |
11 ms |
23820 KB |
Output is correct |
15 |
Correct |
12 ms |
23868 KB |
Output is correct |
16 |
Correct |
12 ms |
23824 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23748 KB |
Output is correct |
19 |
Correct |
14 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23820 KB |
Output is correct |
21 |
Correct |
13 ms |
23824 KB |
Output is correct |
22 |
Correct |
14 ms |
23824 KB |
Output is correct |
23 |
Correct |
14 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23828 KB |
Output is correct |
25 |
Correct |
11 ms |
23816 KB |
Output is correct |
26 |
Correct |
13 ms |
24128 KB |
Output is correct |
27 |
Correct |
14 ms |
24160 KB |
Output is correct |
28 |
Correct |
13 ms |
24376 KB |
Output is correct |
29 |
Correct |
15 ms |
24264 KB |
Output is correct |
30 |
Correct |
15 ms |
24088 KB |
Output is correct |
31 |
Correct |
12 ms |
23744 KB |
Output is correct |
32 |
Correct |
17 ms |
24340 KB |
Output is correct |
33 |
Correct |
15 ms |
23764 KB |
Output is correct |
34 |
Correct |
16 ms |
24148 KB |
Output is correct |
35 |
Correct |
13 ms |
24216 KB |
Output is correct |
36 |
Correct |
13 ms |
24092 KB |
Output is correct |
37 |
Correct |
13 ms |
24204 KB |
Output is correct |
38 |
Correct |
12 ms |
23820 KB |
Output is correct |
39 |
Correct |
14 ms |
24212 KB |
Output is correct |
40 |
Correct |
15 ms |
24148 KB |
Output is correct |
41 |
Correct |
14 ms |
24148 KB |
Output is correct |
42 |
Correct |
15 ms |
24140 KB |
Output is correct |
43 |
Correct |
37 ms |
24276 KB |
Output is correct |
44 |
Correct |
13 ms |
23852 KB |
Output is correct |
45 |
Correct |
14 ms |
24276 KB |
Output is correct |
46 |
Correct |
16 ms |
24152 KB |
Output is correct |
47 |
Correct |
12 ms |
23780 KB |
Output is correct |
48 |
Correct |
14 ms |
24268 KB |
Output is correct |
49 |
Correct |
13 ms |
24276 KB |
Output is correct |
50 |
Correct |
13 ms |
24276 KB |
Output is correct |
51 |
Correct |
16 ms |
24088 KB |
Output is correct |
52 |
Correct |
13 ms |
24148 KB |
Output is correct |
53 |
Correct |
14 ms |
24148 KB |
Output is correct |
54 |
Correct |
14 ms |
24088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23820 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
13 ms |
23748 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23832 KB |
Output is correct |
6 |
Correct |
13 ms |
23868 KB |
Output is correct |
7 |
Correct |
13 ms |
23824 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
11 ms |
23828 KB |
Output is correct |
14 |
Correct |
11 ms |
23820 KB |
Output is correct |
15 |
Correct |
12 ms |
23868 KB |
Output is correct |
16 |
Correct |
12 ms |
23824 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23748 KB |
Output is correct |
19 |
Correct |
14 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23820 KB |
Output is correct |
21 |
Correct |
13 ms |
23824 KB |
Output is correct |
22 |
Correct |
14 ms |
23824 KB |
Output is correct |
23 |
Correct |
14 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23828 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
61 ms |
34108 KB |
Output is correct |
27 |
Correct |
124 ms |
33856 KB |
Output is correct |
28 |
Correct |
14 ms |
24052 KB |
Output is correct |
29 |
Correct |
12 ms |
23764 KB |
Output is correct |
30 |
Correct |
12 ms |
23764 KB |
Output is correct |
31 |
Correct |
90 ms |
33876 KB |
Output is correct |
32 |
Correct |
14 ms |
24156 KB |
Output is correct |
33 |
Execution timed out |
3061 ms |
39664 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
34184 KB |
Output is correct |
2 |
Correct |
68 ms |
38796 KB |
Output is correct |
3 |
Correct |
14 ms |
24216 KB |
Output is correct |
4 |
Correct |
17 ms |
24092 KB |
Output is correct |
5 |
Correct |
13 ms |
23816 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
24080 KB |
Output is correct |
8 |
Correct |
92 ms |
36136 KB |
Output is correct |
9 |
Correct |
14 ms |
24148 KB |
Output is correct |
10 |
Correct |
79 ms |
34172 KB |
Output is correct |
11 |
Correct |
13 ms |
23896 KB |
Output is correct |
12 |
Correct |
103 ms |
33640 KB |
Output is correct |
13 |
Correct |
91 ms |
36032 KB |
Output is correct |
14 |
Correct |
78 ms |
38708 KB |
Output is correct |
15 |
Correct |
89 ms |
34072 KB |
Output is correct |
16 |
Correct |
16 ms |
24148 KB |
Output is correct |
17 |
Correct |
12 ms |
23760 KB |
Output is correct |
18 |
Correct |
69 ms |
38268 KB |
Output is correct |
19 |
Correct |
84 ms |
40440 KB |
Output is correct |
20 |
Correct |
14 ms |
24148 KB |
Output is correct |
21 |
Correct |
15 ms |
23840 KB |
Output is correct |
22 |
Correct |
66 ms |
36316 KB |
Output is correct |
23 |
Correct |
14 ms |
24084 KB |
Output is correct |
24 |
Correct |
81 ms |
34532 KB |
Output is correct |
25 |
Correct |
116 ms |
40456 KB |
Output is correct |
26 |
Correct |
15 ms |
24192 KB |
Output is correct |
27 |
Correct |
15 ms |
24304 KB |
Output is correct |
28 |
Correct |
16 ms |
24148 KB |
Output is correct |
29 |
Correct |
14 ms |
24180 KB |
Output is correct |
30 |
Correct |
14 ms |
24148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23820 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
13 ms |
23748 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23832 KB |
Output is correct |
6 |
Correct |
13 ms |
23868 KB |
Output is correct |
7 |
Correct |
13 ms |
23824 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
11 ms |
23828 KB |
Output is correct |
14 |
Correct |
11 ms |
23820 KB |
Output is correct |
15 |
Correct |
12 ms |
23868 KB |
Output is correct |
16 |
Correct |
12 ms |
23824 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23748 KB |
Output is correct |
19 |
Correct |
14 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23820 KB |
Output is correct |
21 |
Correct |
13 ms |
23824 KB |
Output is correct |
22 |
Correct |
14 ms |
23824 KB |
Output is correct |
23 |
Correct |
14 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23828 KB |
Output is correct |
25 |
Correct |
11 ms |
23816 KB |
Output is correct |
26 |
Correct |
13 ms |
24128 KB |
Output is correct |
27 |
Correct |
14 ms |
24160 KB |
Output is correct |
28 |
Correct |
13 ms |
24376 KB |
Output is correct |
29 |
Correct |
15 ms |
24264 KB |
Output is correct |
30 |
Correct |
15 ms |
24088 KB |
Output is correct |
31 |
Correct |
12 ms |
23744 KB |
Output is correct |
32 |
Correct |
17 ms |
24340 KB |
Output is correct |
33 |
Correct |
15 ms |
23764 KB |
Output is correct |
34 |
Correct |
16 ms |
24148 KB |
Output is correct |
35 |
Correct |
13 ms |
24216 KB |
Output is correct |
36 |
Correct |
13 ms |
24092 KB |
Output is correct |
37 |
Correct |
13 ms |
24204 KB |
Output is correct |
38 |
Correct |
12 ms |
23820 KB |
Output is correct |
39 |
Correct |
14 ms |
24212 KB |
Output is correct |
40 |
Correct |
15 ms |
24148 KB |
Output is correct |
41 |
Correct |
14 ms |
24148 KB |
Output is correct |
42 |
Correct |
15 ms |
24140 KB |
Output is correct |
43 |
Correct |
37 ms |
24276 KB |
Output is correct |
44 |
Correct |
13 ms |
23852 KB |
Output is correct |
45 |
Correct |
14 ms |
24276 KB |
Output is correct |
46 |
Correct |
16 ms |
24152 KB |
Output is correct |
47 |
Correct |
12 ms |
23780 KB |
Output is correct |
48 |
Correct |
14 ms |
24268 KB |
Output is correct |
49 |
Correct |
13 ms |
24276 KB |
Output is correct |
50 |
Correct |
13 ms |
24276 KB |
Output is correct |
51 |
Correct |
16 ms |
24088 KB |
Output is correct |
52 |
Correct |
13 ms |
24148 KB |
Output is correct |
53 |
Correct |
14 ms |
24148 KB |
Output is correct |
54 |
Correct |
14 ms |
24088 KB |
Output is correct |
55 |
Correct |
12 ms |
23764 KB |
Output is correct |
56 |
Correct |
61 ms |
34108 KB |
Output is correct |
57 |
Correct |
124 ms |
33856 KB |
Output is correct |
58 |
Correct |
14 ms |
24052 KB |
Output is correct |
59 |
Correct |
12 ms |
23764 KB |
Output is correct |
60 |
Correct |
12 ms |
23764 KB |
Output is correct |
61 |
Correct |
90 ms |
33876 KB |
Output is correct |
62 |
Correct |
14 ms |
24156 KB |
Output is correct |
63 |
Execution timed out |
3061 ms |
39664 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |