#include<bits/stdc++.h>
using namespace std;
#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
const int MAXN = 500'009;
unordered_map<int, int> M[MAXN];
int goal[MAXN];
int type[MAXN];
vector<int> G[MAXN];
vector<int> G2[MAXN];
int res[MAXN];
int ans;
bool vis[MAXN];
int dp[MAXN];
int start;
void dfs(int x, int p=0) {
M[x][type[x]] = 1;
if(goal[type[x]]>1) res[x] = 1;
for(int y:G[x]) {
if(y==p) continue;
dfs(y, x);
//cerr << "mergin: "<< x << " " << y << "\n";
if(sz(M[y])>sz(M[x])) {
//cerr << "swap\n";
swap(M[y], M[x]);
swap(res[y], res[x]);
}
for(pii z:M[y]) {
//cerr << "adding: " << z.st << " " << z.nd << "\n";
//cerr << "res before: "<< res[x] << "\n";
if(M[x][z.st]==0) res[x]++;
M[x][z.st] += z.nd;
if(M[x][z.st]==goal[z.st]) res[x]--;
//cerr << "res after: " << res[x] << "\n";
}
M[y].clear();
}
//cerr << "-> " << x << " " << res[x] << "\n";
if(res[x]==0) vis[x]=true;
//cerr << x << " " << vis[x] << "\n";
}
void dfs2(int x, int h, int p=0) {
for(int y:G[x]) {
if(y==p) continue;
if(vis[y]) {
G2[y].pb(h);
G2[h].pb(y);
dfs2(y, y, x);
} else {
dfs2(y, h, x);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
for(int i=0;i<n-1;i++) {
int a, b;
cin >> a >> b;
G[a].pb(b);
G[b].pb(a);
}
for(int i=1;i<=n;i++) {
cin >> type[i];
goal[type[i]]++;
}
if(n==2) {
start = 1;
} else {
for(int i=1;i<=n;i++) {
if(sz(G[i])>1) {
start = i;
}
}
}
//cout << "start " << start << "\n";
dfs(1);
dfs2(1, 1);
int cnt = 0;
for(int i=1;i<=n;i++) {
if(sz(G2[i])==1) cnt++;
}
//cout << dp[start] << "\n";
cout << (cnt+1)/2 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
61016 KB |
Output is correct |
2 |
Correct |
15 ms |
61020 KB |
Output is correct |
3 |
Correct |
14 ms |
61020 KB |
Output is correct |
4 |
Correct |
13 ms |
61144 KB |
Output is correct |
5 |
Correct |
13 ms |
61016 KB |
Output is correct |
6 |
Correct |
13 ms |
61132 KB |
Output is correct |
7 |
Correct |
12 ms |
58968 KB |
Output is correct |
8 |
Correct |
14 ms |
61016 KB |
Output is correct |
9 |
Correct |
13 ms |
61020 KB |
Output is correct |
10 |
Correct |
15 ms |
61020 KB |
Output is correct |
11 |
Correct |
13 ms |
61020 KB |
Output is correct |
12 |
Correct |
14 ms |
61156 KB |
Output is correct |
13 |
Correct |
14 ms |
61020 KB |
Output is correct |
14 |
Correct |
13 ms |
61160 KB |
Output is correct |
15 |
Correct |
14 ms |
61016 KB |
Output is correct |
16 |
Correct |
13 ms |
58972 KB |
Output is correct |
17 |
Correct |
14 ms |
61020 KB |
Output is correct |
18 |
Correct |
16 ms |
61064 KB |
Output is correct |
19 |
Correct |
13 ms |
58972 KB |
Output is correct |
20 |
Correct |
13 ms |
58972 KB |
Output is correct |
21 |
Correct |
14 ms |
61020 KB |
Output is correct |
22 |
Correct |
13 ms |
61064 KB |
Output is correct |
23 |
Correct |
13 ms |
61016 KB |
Output is correct |
24 |
Correct |
13 ms |
61020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
61016 KB |
Output is correct |
2 |
Correct |
15 ms |
61020 KB |
Output is correct |
3 |
Correct |
14 ms |
61020 KB |
Output is correct |
4 |
Correct |
13 ms |
61144 KB |
Output is correct |
5 |
Correct |
13 ms |
61016 KB |
Output is correct |
6 |
Correct |
13 ms |
61132 KB |
Output is correct |
7 |
Correct |
12 ms |
58968 KB |
Output is correct |
8 |
Correct |
14 ms |
61016 KB |
Output is correct |
9 |
Correct |
13 ms |
61020 KB |
Output is correct |
10 |
Correct |
15 ms |
61020 KB |
Output is correct |
11 |
Correct |
13 ms |
61020 KB |
Output is correct |
12 |
Correct |
14 ms |
61156 KB |
Output is correct |
13 |
Correct |
14 ms |
61020 KB |
Output is correct |
14 |
Correct |
13 ms |
61160 KB |
Output is correct |
15 |
Correct |
14 ms |
61016 KB |
Output is correct |
16 |
Correct |
13 ms |
58972 KB |
Output is correct |
17 |
Correct |
14 ms |
61020 KB |
Output is correct |
18 |
Correct |
16 ms |
61064 KB |
Output is correct |
19 |
Correct |
13 ms |
58972 KB |
Output is correct |
20 |
Correct |
13 ms |
58972 KB |
Output is correct |
21 |
Correct |
14 ms |
61020 KB |
Output is correct |
22 |
Correct |
13 ms |
61064 KB |
Output is correct |
23 |
Correct |
13 ms |
61016 KB |
Output is correct |
24 |
Correct |
13 ms |
61020 KB |
Output is correct |
25 |
Correct |
13 ms |
61020 KB |
Output is correct |
26 |
Correct |
16 ms |
61532 KB |
Output is correct |
27 |
Correct |
15 ms |
61532 KB |
Output is correct |
28 |
Correct |
15 ms |
59996 KB |
Output is correct |
29 |
Correct |
16 ms |
59860 KB |
Output is correct |
30 |
Correct |
16 ms |
61528 KB |
Output is correct |
31 |
Correct |
13 ms |
61160 KB |
Output is correct |
32 |
Correct |
16 ms |
62000 KB |
Output is correct |
33 |
Correct |
14 ms |
59104 KB |
Output is correct |
34 |
Correct |
15 ms |
61532 KB |
Output is correct |
35 |
Correct |
16 ms |
59736 KB |
Output is correct |
36 |
Correct |
16 ms |
61388 KB |
Output is correct |
37 |
Correct |
16 ms |
61528 KB |
Output is correct |
38 |
Correct |
14 ms |
61016 KB |
Output is correct |
39 |
Correct |
16 ms |
61532 KB |
Output is correct |
40 |
Correct |
15 ms |
61528 KB |
Output is correct |
41 |
Correct |
15 ms |
61600 KB |
Output is correct |
42 |
Correct |
16 ms |
61532 KB |
Output is correct |
43 |
Correct |
15 ms |
62040 KB |
Output is correct |
44 |
Correct |
15 ms |
58972 KB |
Output is correct |
45 |
Correct |
16 ms |
61784 KB |
Output is correct |
46 |
Correct |
16 ms |
61532 KB |
Output is correct |
47 |
Correct |
15 ms |
61116 KB |
Output is correct |
48 |
Correct |
16 ms |
61532 KB |
Output is correct |
49 |
Correct |
15 ms |
59828 KB |
Output is correct |
50 |
Correct |
16 ms |
59736 KB |
Output is correct |
51 |
Correct |
16 ms |
61528 KB |
Output is correct |
52 |
Correct |
15 ms |
61528 KB |
Output is correct |
53 |
Correct |
15 ms |
61532 KB |
Output is correct |
54 |
Correct |
16 ms |
61532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
61016 KB |
Output is correct |
2 |
Correct |
15 ms |
61020 KB |
Output is correct |
3 |
Correct |
14 ms |
61020 KB |
Output is correct |
4 |
Correct |
13 ms |
61144 KB |
Output is correct |
5 |
Correct |
13 ms |
61016 KB |
Output is correct |
6 |
Correct |
13 ms |
61132 KB |
Output is correct |
7 |
Correct |
12 ms |
58968 KB |
Output is correct |
8 |
Correct |
14 ms |
61016 KB |
Output is correct |
9 |
Correct |
13 ms |
61020 KB |
Output is correct |
10 |
Correct |
15 ms |
61020 KB |
Output is correct |
11 |
Correct |
13 ms |
61020 KB |
Output is correct |
12 |
Correct |
14 ms |
61156 KB |
Output is correct |
13 |
Correct |
14 ms |
61020 KB |
Output is correct |
14 |
Correct |
13 ms |
61160 KB |
Output is correct |
15 |
Correct |
14 ms |
61016 KB |
Output is correct |
16 |
Correct |
13 ms |
58972 KB |
Output is correct |
17 |
Correct |
14 ms |
61020 KB |
Output is correct |
18 |
Correct |
16 ms |
61064 KB |
Output is correct |
19 |
Correct |
13 ms |
58972 KB |
Output is correct |
20 |
Correct |
13 ms |
58972 KB |
Output is correct |
21 |
Correct |
14 ms |
61020 KB |
Output is correct |
22 |
Correct |
13 ms |
61064 KB |
Output is correct |
23 |
Correct |
13 ms |
61016 KB |
Output is correct |
24 |
Correct |
13 ms |
61020 KB |
Output is correct |
25 |
Correct |
13 ms |
61016 KB |
Output is correct |
26 |
Correct |
77 ms |
75860 KB |
Output is correct |
27 |
Correct |
107 ms |
76372 KB |
Output is correct |
28 |
Correct |
16 ms |
61532 KB |
Output is correct |
29 |
Correct |
14 ms |
61020 KB |
Output is correct |
30 |
Correct |
14 ms |
58972 KB |
Output is correct |
31 |
Correct |
89 ms |
75664 KB |
Output is correct |
32 |
Correct |
15 ms |
61528 KB |
Output is correct |
33 |
Correct |
122 ms |
90996 KB |
Output is correct |
34 |
Correct |
112 ms |
76028 KB |
Output is correct |
35 |
Correct |
15 ms |
61528 KB |
Output is correct |
36 |
Correct |
105 ms |
76876 KB |
Output is correct |
37 |
Correct |
15 ms |
61588 KB |
Output is correct |
38 |
Correct |
15 ms |
61532 KB |
Output is correct |
39 |
Correct |
73 ms |
75812 KB |
Output is correct |
40 |
Correct |
15 ms |
62040 KB |
Output is correct |
41 |
Correct |
98 ms |
75836 KB |
Output is correct |
42 |
Correct |
121 ms |
80440 KB |
Output is correct |
43 |
Correct |
13 ms |
61016 KB |
Output is correct |
44 |
Correct |
116 ms |
91712 KB |
Output is correct |
45 |
Correct |
110 ms |
85072 KB |
Output is correct |
46 |
Correct |
15 ms |
61532 KB |
Output is correct |
47 |
Correct |
15 ms |
61528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
75808 KB |
Output is correct |
2 |
Correct |
114 ms |
82244 KB |
Output is correct |
3 |
Correct |
16 ms |
61532 KB |
Output is correct |
4 |
Correct |
15 ms |
61404 KB |
Output is correct |
5 |
Correct |
13 ms |
61020 KB |
Output is correct |
6 |
Correct |
15 ms |
59116 KB |
Output is correct |
7 |
Correct |
19 ms |
61540 KB |
Output is correct |
8 |
Correct |
127 ms |
79984 KB |
Output is correct |
9 |
Correct |
15 ms |
61604 KB |
Output is correct |
10 |
Correct |
92 ms |
75860 KB |
Output is correct |
11 |
Correct |
13 ms |
61172 KB |
Output is correct |
12 |
Correct |
92 ms |
75400 KB |
Output is correct |
13 |
Correct |
150 ms |
80244 KB |
Output is correct |
14 |
Correct |
166 ms |
86216 KB |
Output is correct |
15 |
Correct |
85 ms |
75844 KB |
Output is correct |
16 |
Correct |
15 ms |
61528 KB |
Output is correct |
17 |
Correct |
13 ms |
58972 KB |
Output is correct |
18 |
Correct |
108 ms |
82468 KB |
Output is correct |
19 |
Correct |
176 ms |
89516 KB |
Output is correct |
20 |
Correct |
18 ms |
61528 KB |
Output is correct |
21 |
Correct |
14 ms |
61020 KB |
Output is correct |
22 |
Correct |
109 ms |
78892 KB |
Output is correct |
23 |
Correct |
15 ms |
61528 KB |
Output is correct |
24 |
Correct |
119 ms |
76372 KB |
Output is correct |
25 |
Correct |
186 ms |
88264 KB |
Output is correct |
26 |
Correct |
14 ms |
59736 KB |
Output is correct |
27 |
Correct |
17 ms |
59960 KB |
Output is correct |
28 |
Correct |
16 ms |
61532 KB |
Output is correct |
29 |
Correct |
15 ms |
61376 KB |
Output is correct |
30 |
Correct |
16 ms |
61532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
61016 KB |
Output is correct |
2 |
Correct |
15 ms |
61020 KB |
Output is correct |
3 |
Correct |
14 ms |
61020 KB |
Output is correct |
4 |
Correct |
13 ms |
61144 KB |
Output is correct |
5 |
Correct |
13 ms |
61016 KB |
Output is correct |
6 |
Correct |
13 ms |
61132 KB |
Output is correct |
7 |
Correct |
12 ms |
58968 KB |
Output is correct |
8 |
Correct |
14 ms |
61016 KB |
Output is correct |
9 |
Correct |
13 ms |
61020 KB |
Output is correct |
10 |
Correct |
15 ms |
61020 KB |
Output is correct |
11 |
Correct |
13 ms |
61020 KB |
Output is correct |
12 |
Correct |
14 ms |
61156 KB |
Output is correct |
13 |
Correct |
14 ms |
61020 KB |
Output is correct |
14 |
Correct |
13 ms |
61160 KB |
Output is correct |
15 |
Correct |
14 ms |
61016 KB |
Output is correct |
16 |
Correct |
13 ms |
58972 KB |
Output is correct |
17 |
Correct |
14 ms |
61020 KB |
Output is correct |
18 |
Correct |
16 ms |
61064 KB |
Output is correct |
19 |
Correct |
13 ms |
58972 KB |
Output is correct |
20 |
Correct |
13 ms |
58972 KB |
Output is correct |
21 |
Correct |
14 ms |
61020 KB |
Output is correct |
22 |
Correct |
13 ms |
61064 KB |
Output is correct |
23 |
Correct |
13 ms |
61016 KB |
Output is correct |
24 |
Correct |
13 ms |
61020 KB |
Output is correct |
25 |
Correct |
13 ms |
61020 KB |
Output is correct |
26 |
Correct |
16 ms |
61532 KB |
Output is correct |
27 |
Correct |
15 ms |
61532 KB |
Output is correct |
28 |
Correct |
15 ms |
59996 KB |
Output is correct |
29 |
Correct |
16 ms |
59860 KB |
Output is correct |
30 |
Correct |
16 ms |
61528 KB |
Output is correct |
31 |
Correct |
13 ms |
61160 KB |
Output is correct |
32 |
Correct |
16 ms |
62000 KB |
Output is correct |
33 |
Correct |
14 ms |
59104 KB |
Output is correct |
34 |
Correct |
15 ms |
61532 KB |
Output is correct |
35 |
Correct |
16 ms |
59736 KB |
Output is correct |
36 |
Correct |
16 ms |
61388 KB |
Output is correct |
37 |
Correct |
16 ms |
61528 KB |
Output is correct |
38 |
Correct |
14 ms |
61016 KB |
Output is correct |
39 |
Correct |
16 ms |
61532 KB |
Output is correct |
40 |
Correct |
15 ms |
61528 KB |
Output is correct |
41 |
Correct |
15 ms |
61600 KB |
Output is correct |
42 |
Correct |
16 ms |
61532 KB |
Output is correct |
43 |
Correct |
15 ms |
62040 KB |
Output is correct |
44 |
Correct |
15 ms |
58972 KB |
Output is correct |
45 |
Correct |
16 ms |
61784 KB |
Output is correct |
46 |
Correct |
16 ms |
61532 KB |
Output is correct |
47 |
Correct |
15 ms |
61116 KB |
Output is correct |
48 |
Correct |
16 ms |
61532 KB |
Output is correct |
49 |
Correct |
15 ms |
59828 KB |
Output is correct |
50 |
Correct |
16 ms |
59736 KB |
Output is correct |
51 |
Correct |
16 ms |
61528 KB |
Output is correct |
52 |
Correct |
15 ms |
61528 KB |
Output is correct |
53 |
Correct |
15 ms |
61532 KB |
Output is correct |
54 |
Correct |
16 ms |
61532 KB |
Output is correct |
55 |
Correct |
13 ms |
61016 KB |
Output is correct |
56 |
Correct |
77 ms |
75860 KB |
Output is correct |
57 |
Correct |
107 ms |
76372 KB |
Output is correct |
58 |
Correct |
16 ms |
61532 KB |
Output is correct |
59 |
Correct |
14 ms |
61020 KB |
Output is correct |
60 |
Correct |
14 ms |
58972 KB |
Output is correct |
61 |
Correct |
89 ms |
75664 KB |
Output is correct |
62 |
Correct |
15 ms |
61528 KB |
Output is correct |
63 |
Correct |
122 ms |
90996 KB |
Output is correct |
64 |
Correct |
112 ms |
76028 KB |
Output is correct |
65 |
Correct |
15 ms |
61528 KB |
Output is correct |
66 |
Correct |
105 ms |
76876 KB |
Output is correct |
67 |
Correct |
15 ms |
61588 KB |
Output is correct |
68 |
Correct |
15 ms |
61532 KB |
Output is correct |
69 |
Correct |
73 ms |
75812 KB |
Output is correct |
70 |
Correct |
15 ms |
62040 KB |
Output is correct |
71 |
Correct |
98 ms |
75836 KB |
Output is correct |
72 |
Correct |
121 ms |
80440 KB |
Output is correct |
73 |
Correct |
13 ms |
61016 KB |
Output is correct |
74 |
Correct |
116 ms |
91712 KB |
Output is correct |
75 |
Correct |
110 ms |
85072 KB |
Output is correct |
76 |
Correct |
15 ms |
61532 KB |
Output is correct |
77 |
Correct |
15 ms |
61528 KB |
Output is correct |
78 |
Correct |
77 ms |
75808 KB |
Output is correct |
79 |
Correct |
114 ms |
82244 KB |
Output is correct |
80 |
Correct |
16 ms |
61532 KB |
Output is correct |
81 |
Correct |
15 ms |
61404 KB |
Output is correct |
82 |
Correct |
13 ms |
61020 KB |
Output is correct |
83 |
Correct |
15 ms |
59116 KB |
Output is correct |
84 |
Correct |
19 ms |
61540 KB |
Output is correct |
85 |
Correct |
127 ms |
79984 KB |
Output is correct |
86 |
Correct |
15 ms |
61604 KB |
Output is correct |
87 |
Correct |
92 ms |
75860 KB |
Output is correct |
88 |
Correct |
13 ms |
61172 KB |
Output is correct |
89 |
Correct |
92 ms |
75400 KB |
Output is correct |
90 |
Correct |
150 ms |
80244 KB |
Output is correct |
91 |
Correct |
166 ms |
86216 KB |
Output is correct |
92 |
Correct |
85 ms |
75844 KB |
Output is correct |
93 |
Correct |
15 ms |
61528 KB |
Output is correct |
94 |
Correct |
13 ms |
58972 KB |
Output is correct |
95 |
Correct |
108 ms |
82468 KB |
Output is correct |
96 |
Correct |
176 ms |
89516 KB |
Output is correct |
97 |
Correct |
18 ms |
61528 KB |
Output is correct |
98 |
Correct |
14 ms |
61020 KB |
Output is correct |
99 |
Correct |
109 ms |
78892 KB |
Output is correct |
100 |
Correct |
15 ms |
61528 KB |
Output is correct |
101 |
Correct |
119 ms |
76372 KB |
Output is correct |
102 |
Correct |
186 ms |
88264 KB |
Output is correct |
103 |
Correct |
14 ms |
59736 KB |
Output is correct |
104 |
Correct |
17 ms |
59960 KB |
Output is correct |
105 |
Correct |
16 ms |
61532 KB |
Output is correct |
106 |
Correct |
15 ms |
61376 KB |
Output is correct |
107 |
Correct |
16 ms |
61532 KB |
Output is correct |
108 |
Correct |
604 ms |
150200 KB |
Output is correct |
109 |
Correct |
819 ms |
174416 KB |
Output is correct |
110 |
Correct |
817 ms |
199120 KB |
Output is correct |
111 |
Correct |
1226 ms |
216592 KB |
Output is correct |
112 |
Correct |
1178 ms |
197612 KB |
Output is correct |
113 |
Correct |
829 ms |
175116 KB |
Output is correct |
114 |
Correct |
643 ms |
136820 KB |
Output is correct |
115 |
Correct |
690 ms |
136696 KB |
Output is correct |
116 |
Correct |
894 ms |
152256 KB |
Output is correct |
117 |
Correct |
1280 ms |
201964 KB |
Output is correct |
118 |
Correct |
674 ms |
135580 KB |
Output is correct |
119 |
Correct |
1314 ms |
200628 KB |
Output is correct |
120 |
Correct |
1348 ms |
208344 KB |
Output is correct |
121 |
Correct |
1233 ms |
199944 KB |
Output is correct |
122 |
Correct |
1090 ms |
175364 KB |
Output is correct |
123 |
Correct |
722 ms |
178420 KB |
Output is correct |
124 |
Correct |
1220 ms |
181720 KB |
Output is correct |