#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less_equal<>,
rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
#define ll long long
#define ii pair<ll,ll>
#define iii tuple<ll,ll,ll>
#ifndef DEBUG
#define cerr if (0) cerr
#define endl '\n'
#endif
const ll inf=1e15;
const ll maxn=5e5+5;
const ll mod=1e9+7;
mt19937 rng(69);
vector<ll> adj[maxn];
vector<ll> type[maxn];
ll h[maxn];
ll dp[maxn];
ll pre[maxn],post[maxn],pref[maxn];
ll TIME,cnt;
vector<ll> check;
void dfs(ll x, ll p=0){
pre[x]=++TIME;
dp[x]=h[x];
for (auto u:adj[x]){
if (u==p) continue;
dfs(u,x);
dp[x]^=dp[u];
}
post[x]=TIME;
cerr<<dp[x]<<' '<<x<<endl;
if (x!=1 && dp[x]==0){
pref[pre[x]]++;
check.emplace_back(x);
cnt++;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,k,a,b;
cin>>n>>k;
for (int i=1;i<n;i++){
cin>>a>>b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
for (int i=1;i<=n;i++){
cin>>a;
type[a].emplace_back(i);
}
for (int i=1;i<=k;i++){
ll tot=0;
for (int j=0;j<type[i].size()-1;j++){
h[type[i][j]]=rng();
tot^=h[type[i][j]];
}
h[type[i].back()]=tot;
}
dfs(1);
for (int i=1;i<maxn;i++) pref[i]+=pref[i-1];
ll ans=0;
for (auto u:check){
ll res=pref[post[u]]-pref[pre[u]-1]; //number of bad edges in subtree
cerr<<u<<' '<<res<<endl;
if (res==1 || res==cnt) ans++;
}
cout<<(ans+1)/2;
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:65:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int j=0;j<type[i].size()-1;j++){
| ~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
35164 KB |
Output is correct |
2 |
Correct |
9 ms |
35164 KB |
Output is correct |
3 |
Correct |
9 ms |
35076 KB |
Output is correct |
4 |
Correct |
9 ms |
35164 KB |
Output is correct |
5 |
Correct |
10 ms |
35080 KB |
Output is correct |
6 |
Correct |
9 ms |
35164 KB |
Output is correct |
7 |
Correct |
9 ms |
35164 KB |
Output is correct |
8 |
Correct |
9 ms |
35320 KB |
Output is correct |
9 |
Correct |
9 ms |
35160 KB |
Output is correct |
10 |
Correct |
9 ms |
35160 KB |
Output is correct |
11 |
Correct |
9 ms |
35308 KB |
Output is correct |
12 |
Correct |
10 ms |
35164 KB |
Output is correct |
13 |
Correct |
9 ms |
35268 KB |
Output is correct |
14 |
Correct |
9 ms |
35164 KB |
Output is correct |
15 |
Correct |
9 ms |
35164 KB |
Output is correct |
16 |
Correct |
10 ms |
35108 KB |
Output is correct |
17 |
Correct |
9 ms |
35164 KB |
Output is correct |
18 |
Correct |
9 ms |
35164 KB |
Output is correct |
19 |
Correct |
10 ms |
35164 KB |
Output is correct |
20 |
Correct |
10 ms |
35164 KB |
Output is correct |
21 |
Correct |
9 ms |
35164 KB |
Output is correct |
22 |
Correct |
9 ms |
35164 KB |
Output is correct |
23 |
Correct |
10 ms |
35164 KB |
Output is correct |
24 |
Correct |
10 ms |
35164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
35164 KB |
Output is correct |
2 |
Correct |
9 ms |
35164 KB |
Output is correct |
3 |
Correct |
9 ms |
35076 KB |
Output is correct |
4 |
Correct |
9 ms |
35164 KB |
Output is correct |
5 |
Correct |
10 ms |
35080 KB |
Output is correct |
6 |
Correct |
9 ms |
35164 KB |
Output is correct |
7 |
Correct |
9 ms |
35164 KB |
Output is correct |
8 |
Correct |
9 ms |
35320 KB |
Output is correct |
9 |
Correct |
9 ms |
35160 KB |
Output is correct |
10 |
Correct |
9 ms |
35160 KB |
Output is correct |
11 |
Correct |
9 ms |
35308 KB |
Output is correct |
12 |
Correct |
10 ms |
35164 KB |
Output is correct |
13 |
Correct |
9 ms |
35268 KB |
Output is correct |
14 |
Correct |
9 ms |
35164 KB |
Output is correct |
15 |
Correct |
9 ms |
35164 KB |
Output is correct |
16 |
Correct |
10 ms |
35108 KB |
Output is correct |
17 |
Correct |
9 ms |
35164 KB |
Output is correct |
18 |
Correct |
9 ms |
35164 KB |
Output is correct |
19 |
Correct |
10 ms |
35164 KB |
Output is correct |
20 |
Correct |
10 ms |
35164 KB |
Output is correct |
21 |
Correct |
9 ms |
35164 KB |
Output is correct |
22 |
Correct |
9 ms |
35164 KB |
Output is correct |
23 |
Correct |
10 ms |
35164 KB |
Output is correct |
24 |
Correct |
10 ms |
35164 KB |
Output is correct |
25 |
Correct |
10 ms |
35164 KB |
Output is correct |
26 |
Correct |
11 ms |
35420 KB |
Output is correct |
27 |
Correct |
11 ms |
35500 KB |
Output is correct |
28 |
Correct |
11 ms |
35676 KB |
Output is correct |
29 |
Correct |
12 ms |
35420 KB |
Output is correct |
30 |
Correct |
11 ms |
35420 KB |
Output is correct |
31 |
Correct |
10 ms |
35164 KB |
Output is correct |
32 |
Correct |
11 ms |
35676 KB |
Output is correct |
33 |
Correct |
9 ms |
35264 KB |
Output is correct |
34 |
Correct |
10 ms |
35420 KB |
Output is correct |
35 |
Correct |
10 ms |
35420 KB |
Output is correct |
36 |
Correct |
11 ms |
35320 KB |
Output is correct |
37 |
Correct |
10 ms |
35420 KB |
Output is correct |
38 |
Correct |
9 ms |
35164 KB |
Output is correct |
39 |
Correct |
10 ms |
35524 KB |
Output is correct |
40 |
Correct |
10 ms |
35420 KB |
Output is correct |
41 |
Correct |
12 ms |
35324 KB |
Output is correct |
42 |
Correct |
10 ms |
35420 KB |
Output is correct |
43 |
Correct |
10 ms |
35676 KB |
Output is correct |
44 |
Correct |
10 ms |
35160 KB |
Output is correct |
45 |
Correct |
11 ms |
35420 KB |
Output is correct |
46 |
Correct |
10 ms |
35420 KB |
Output is correct |
47 |
Correct |
10 ms |
35160 KB |
Output is correct |
48 |
Correct |
10 ms |
35396 KB |
Output is correct |
49 |
Correct |
12 ms |
35480 KB |
Output is correct |
50 |
Correct |
11 ms |
35676 KB |
Output is correct |
51 |
Correct |
10 ms |
35480 KB |
Output is correct |
52 |
Correct |
12 ms |
35416 KB |
Output is correct |
53 |
Correct |
10 ms |
35416 KB |
Output is correct |
54 |
Correct |
12 ms |
35420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
35164 KB |
Output is correct |
2 |
Correct |
9 ms |
35164 KB |
Output is correct |
3 |
Correct |
9 ms |
35076 KB |
Output is correct |
4 |
Correct |
9 ms |
35164 KB |
Output is correct |
5 |
Correct |
10 ms |
35080 KB |
Output is correct |
6 |
Correct |
9 ms |
35164 KB |
Output is correct |
7 |
Correct |
9 ms |
35164 KB |
Output is correct |
8 |
Correct |
9 ms |
35320 KB |
Output is correct |
9 |
Correct |
9 ms |
35160 KB |
Output is correct |
10 |
Correct |
9 ms |
35160 KB |
Output is correct |
11 |
Correct |
9 ms |
35308 KB |
Output is correct |
12 |
Correct |
10 ms |
35164 KB |
Output is correct |
13 |
Correct |
9 ms |
35268 KB |
Output is correct |
14 |
Correct |
9 ms |
35164 KB |
Output is correct |
15 |
Correct |
9 ms |
35164 KB |
Output is correct |
16 |
Correct |
10 ms |
35108 KB |
Output is correct |
17 |
Correct |
9 ms |
35164 KB |
Output is correct |
18 |
Correct |
9 ms |
35164 KB |
Output is correct |
19 |
Correct |
10 ms |
35164 KB |
Output is correct |
20 |
Correct |
10 ms |
35164 KB |
Output is correct |
21 |
Correct |
9 ms |
35164 KB |
Output is correct |
22 |
Correct |
9 ms |
35164 KB |
Output is correct |
23 |
Correct |
10 ms |
35164 KB |
Output is correct |
24 |
Correct |
10 ms |
35164 KB |
Output is correct |
25 |
Correct |
9 ms |
35160 KB |
Output is correct |
26 |
Correct |
43 ms |
47816 KB |
Output is correct |
27 |
Correct |
54 ms |
47500 KB |
Output is correct |
28 |
Correct |
10 ms |
35420 KB |
Output is correct |
29 |
Correct |
9 ms |
35164 KB |
Output is correct |
30 |
Correct |
9 ms |
35164 KB |
Output is correct |
31 |
Correct |
50 ms |
47360 KB |
Output is correct |
32 |
Correct |
10 ms |
35416 KB |
Output is correct |
33 |
Correct |
55 ms |
54104 KB |
Output is correct |
34 |
Correct |
60 ms |
47660 KB |
Output is correct |
35 |
Correct |
12 ms |
35420 KB |
Output is correct |
36 |
Correct |
62 ms |
47720 KB |
Output is correct |
37 |
Correct |
11 ms |
35420 KB |
Output is correct |
38 |
Correct |
11 ms |
35420 KB |
Output is correct |
39 |
Correct |
42 ms |
47656 KB |
Output is correct |
40 |
Correct |
11 ms |
35672 KB |
Output is correct |
41 |
Correct |
61 ms |
47300 KB |
Output is correct |
42 |
Correct |
57 ms |
49216 KB |
Output is correct |
43 |
Correct |
10 ms |
35160 KB |
Output is correct |
44 |
Correct |
53 ms |
54216 KB |
Output is correct |
45 |
Correct |
58 ms |
52096 KB |
Output is correct |
46 |
Correct |
11 ms |
35540 KB |
Output is correct |
47 |
Correct |
10 ms |
35420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
46620 KB |
Output is correct |
2 |
Correct |
49 ms |
49996 KB |
Output is correct |
3 |
Correct |
11 ms |
35420 KB |
Output is correct |
4 |
Correct |
10 ms |
35420 KB |
Output is correct |
5 |
Correct |
10 ms |
35164 KB |
Output is correct |
6 |
Correct |
9 ms |
35164 KB |
Output is correct |
7 |
Correct |
10 ms |
35380 KB |
Output is correct |
8 |
Correct |
59 ms |
48904 KB |
Output is correct |
9 |
Correct |
11 ms |
35416 KB |
Output is correct |
10 |
Correct |
53 ms |
47852 KB |
Output is correct |
11 |
Correct |
9 ms |
35164 KB |
Output is correct |
12 |
Correct |
52 ms |
47164 KB |
Output is correct |
13 |
Correct |
70 ms |
48884 KB |
Output is correct |
14 |
Correct |
58 ms |
51152 KB |
Output is correct |
15 |
Correct |
42 ms |
47680 KB |
Output is correct |
16 |
Correct |
11 ms |
35420 KB |
Output is correct |
17 |
Correct |
10 ms |
35164 KB |
Output is correct |
18 |
Correct |
50 ms |
50972 KB |
Output is correct |
19 |
Correct |
69 ms |
54644 KB |
Output is correct |
20 |
Correct |
12 ms |
35672 KB |
Output is correct |
21 |
Correct |
11 ms |
35160 KB |
Output is correct |
22 |
Correct |
54 ms |
49152 KB |
Output is correct |
23 |
Correct |
10 ms |
35416 KB |
Output is correct |
24 |
Correct |
57 ms |
48208 KB |
Output is correct |
25 |
Correct |
64 ms |
54004 KB |
Output is correct |
26 |
Correct |
11 ms |
35376 KB |
Output is correct |
27 |
Correct |
11 ms |
35676 KB |
Output is correct |
28 |
Correct |
10 ms |
35420 KB |
Output is correct |
29 |
Correct |
11 ms |
35524 KB |
Output is correct |
30 |
Correct |
10 ms |
35420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
35164 KB |
Output is correct |
2 |
Correct |
9 ms |
35164 KB |
Output is correct |
3 |
Correct |
9 ms |
35076 KB |
Output is correct |
4 |
Correct |
9 ms |
35164 KB |
Output is correct |
5 |
Correct |
10 ms |
35080 KB |
Output is correct |
6 |
Correct |
9 ms |
35164 KB |
Output is correct |
7 |
Correct |
9 ms |
35164 KB |
Output is correct |
8 |
Correct |
9 ms |
35320 KB |
Output is correct |
9 |
Correct |
9 ms |
35160 KB |
Output is correct |
10 |
Correct |
9 ms |
35160 KB |
Output is correct |
11 |
Correct |
9 ms |
35308 KB |
Output is correct |
12 |
Correct |
10 ms |
35164 KB |
Output is correct |
13 |
Correct |
9 ms |
35268 KB |
Output is correct |
14 |
Correct |
9 ms |
35164 KB |
Output is correct |
15 |
Correct |
9 ms |
35164 KB |
Output is correct |
16 |
Correct |
10 ms |
35108 KB |
Output is correct |
17 |
Correct |
9 ms |
35164 KB |
Output is correct |
18 |
Correct |
9 ms |
35164 KB |
Output is correct |
19 |
Correct |
10 ms |
35164 KB |
Output is correct |
20 |
Correct |
10 ms |
35164 KB |
Output is correct |
21 |
Correct |
9 ms |
35164 KB |
Output is correct |
22 |
Correct |
9 ms |
35164 KB |
Output is correct |
23 |
Correct |
10 ms |
35164 KB |
Output is correct |
24 |
Correct |
10 ms |
35164 KB |
Output is correct |
25 |
Correct |
10 ms |
35164 KB |
Output is correct |
26 |
Correct |
11 ms |
35420 KB |
Output is correct |
27 |
Correct |
11 ms |
35500 KB |
Output is correct |
28 |
Correct |
11 ms |
35676 KB |
Output is correct |
29 |
Correct |
12 ms |
35420 KB |
Output is correct |
30 |
Correct |
11 ms |
35420 KB |
Output is correct |
31 |
Correct |
10 ms |
35164 KB |
Output is correct |
32 |
Correct |
11 ms |
35676 KB |
Output is correct |
33 |
Correct |
9 ms |
35264 KB |
Output is correct |
34 |
Correct |
10 ms |
35420 KB |
Output is correct |
35 |
Correct |
10 ms |
35420 KB |
Output is correct |
36 |
Correct |
11 ms |
35320 KB |
Output is correct |
37 |
Correct |
10 ms |
35420 KB |
Output is correct |
38 |
Correct |
9 ms |
35164 KB |
Output is correct |
39 |
Correct |
10 ms |
35524 KB |
Output is correct |
40 |
Correct |
10 ms |
35420 KB |
Output is correct |
41 |
Correct |
12 ms |
35324 KB |
Output is correct |
42 |
Correct |
10 ms |
35420 KB |
Output is correct |
43 |
Correct |
10 ms |
35676 KB |
Output is correct |
44 |
Correct |
10 ms |
35160 KB |
Output is correct |
45 |
Correct |
11 ms |
35420 KB |
Output is correct |
46 |
Correct |
10 ms |
35420 KB |
Output is correct |
47 |
Correct |
10 ms |
35160 KB |
Output is correct |
48 |
Correct |
10 ms |
35396 KB |
Output is correct |
49 |
Correct |
12 ms |
35480 KB |
Output is correct |
50 |
Correct |
11 ms |
35676 KB |
Output is correct |
51 |
Correct |
10 ms |
35480 KB |
Output is correct |
52 |
Correct |
12 ms |
35416 KB |
Output is correct |
53 |
Correct |
10 ms |
35416 KB |
Output is correct |
54 |
Correct |
12 ms |
35420 KB |
Output is correct |
55 |
Correct |
9 ms |
35160 KB |
Output is correct |
56 |
Correct |
43 ms |
47816 KB |
Output is correct |
57 |
Correct |
54 ms |
47500 KB |
Output is correct |
58 |
Correct |
10 ms |
35420 KB |
Output is correct |
59 |
Correct |
9 ms |
35164 KB |
Output is correct |
60 |
Correct |
9 ms |
35164 KB |
Output is correct |
61 |
Correct |
50 ms |
47360 KB |
Output is correct |
62 |
Correct |
10 ms |
35416 KB |
Output is correct |
63 |
Correct |
55 ms |
54104 KB |
Output is correct |
64 |
Correct |
60 ms |
47660 KB |
Output is correct |
65 |
Correct |
12 ms |
35420 KB |
Output is correct |
66 |
Correct |
62 ms |
47720 KB |
Output is correct |
67 |
Correct |
11 ms |
35420 KB |
Output is correct |
68 |
Correct |
11 ms |
35420 KB |
Output is correct |
69 |
Correct |
42 ms |
47656 KB |
Output is correct |
70 |
Correct |
11 ms |
35672 KB |
Output is correct |
71 |
Correct |
61 ms |
47300 KB |
Output is correct |
72 |
Correct |
57 ms |
49216 KB |
Output is correct |
73 |
Correct |
10 ms |
35160 KB |
Output is correct |
74 |
Correct |
53 ms |
54216 KB |
Output is correct |
75 |
Correct |
58 ms |
52096 KB |
Output is correct |
76 |
Correct |
11 ms |
35540 KB |
Output is correct |
77 |
Correct |
10 ms |
35420 KB |
Output is correct |
78 |
Correct |
43 ms |
46620 KB |
Output is correct |
79 |
Correct |
49 ms |
49996 KB |
Output is correct |
80 |
Correct |
11 ms |
35420 KB |
Output is correct |
81 |
Correct |
10 ms |
35420 KB |
Output is correct |
82 |
Correct |
10 ms |
35164 KB |
Output is correct |
83 |
Correct |
9 ms |
35164 KB |
Output is correct |
84 |
Correct |
10 ms |
35380 KB |
Output is correct |
85 |
Correct |
59 ms |
48904 KB |
Output is correct |
86 |
Correct |
11 ms |
35416 KB |
Output is correct |
87 |
Correct |
53 ms |
47852 KB |
Output is correct |
88 |
Correct |
9 ms |
35164 KB |
Output is correct |
89 |
Correct |
52 ms |
47164 KB |
Output is correct |
90 |
Correct |
70 ms |
48884 KB |
Output is correct |
91 |
Correct |
58 ms |
51152 KB |
Output is correct |
92 |
Correct |
42 ms |
47680 KB |
Output is correct |
93 |
Correct |
11 ms |
35420 KB |
Output is correct |
94 |
Correct |
10 ms |
35164 KB |
Output is correct |
95 |
Correct |
50 ms |
50972 KB |
Output is correct |
96 |
Correct |
69 ms |
54644 KB |
Output is correct |
97 |
Correct |
12 ms |
35672 KB |
Output is correct |
98 |
Correct |
11 ms |
35160 KB |
Output is correct |
99 |
Correct |
54 ms |
49152 KB |
Output is correct |
100 |
Correct |
10 ms |
35416 KB |
Output is correct |
101 |
Correct |
57 ms |
48208 KB |
Output is correct |
102 |
Correct |
64 ms |
54004 KB |
Output is correct |
103 |
Correct |
11 ms |
35376 KB |
Output is correct |
104 |
Correct |
11 ms |
35676 KB |
Output is correct |
105 |
Correct |
10 ms |
35420 KB |
Output is correct |
106 |
Correct |
11 ms |
35524 KB |
Output is correct |
107 |
Correct |
10 ms |
35420 KB |
Output is correct |
108 |
Correct |
265 ms |
82428 KB |
Output is correct |
109 |
Correct |
450 ms |
89184 KB |
Output is correct |
110 |
Correct |
474 ms |
100540 KB |
Output is correct |
111 |
Correct |
569 ms |
112660 KB |
Output is correct |
112 |
Correct |
513 ms |
107040 KB |
Output is correct |
113 |
Correct |
300 ms |
93108 KB |
Output is correct |
114 |
Correct |
363 ms |
73800 KB |
Output is correct |
115 |
Correct |
418 ms |
73704 KB |
Output is correct |
116 |
Correct |
423 ms |
79176 KB |
Output is correct |
117 |
Correct |
474 ms |
93468 KB |
Output is correct |
118 |
Correct |
386 ms |
76412 KB |
Output is correct |
119 |
Correct |
518 ms |
92412 KB |
Output is correct |
120 |
Correct |
528 ms |
107584 KB |
Output is correct |
121 |
Correct |
480 ms |
93880 KB |
Output is correct |
122 |
Correct |
424 ms |
83316 KB |
Output is correct |
123 |
Correct |
315 ms |
96544 KB |
Output is correct |
124 |
Correct |
532 ms |
90892 KB |
Output is correct |