#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;
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";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
57180 KB |
Output is correct |
2 |
Correct |
11 ms |
57180 KB |
Output is correct |
3 |
Correct |
12 ms |
57180 KB |
Output is correct |
4 |
Correct |
11 ms |
57248 KB |
Output is correct |
5 |
Correct |
11 ms |
57180 KB |
Output is correct |
6 |
Correct |
12 ms |
57180 KB |
Output is correct |
7 |
Correct |
11 ms |
55136 KB |
Output is correct |
8 |
Correct |
11 ms |
57180 KB |
Output is correct |
9 |
Correct |
11 ms |
57236 KB |
Output is correct |
10 |
Correct |
11 ms |
57236 KB |
Output is correct |
11 |
Correct |
12 ms |
57180 KB |
Output is correct |
12 |
Correct |
11 ms |
57152 KB |
Output is correct |
13 |
Correct |
12 ms |
57180 KB |
Output is correct |
14 |
Correct |
12 ms |
57180 KB |
Output is correct |
15 |
Correct |
12 ms |
57176 KB |
Output is correct |
16 |
Correct |
11 ms |
55128 KB |
Output is correct |
17 |
Correct |
12 ms |
57180 KB |
Output is correct |
18 |
Correct |
12 ms |
57180 KB |
Output is correct |
19 |
Correct |
14 ms |
55132 KB |
Output is correct |
20 |
Correct |
11 ms |
55132 KB |
Output is correct |
21 |
Correct |
12 ms |
57140 KB |
Output is correct |
22 |
Correct |
11 ms |
57260 KB |
Output is correct |
23 |
Correct |
11 ms |
57180 KB |
Output is correct |
24 |
Correct |
12 ms |
57180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
57180 KB |
Output is correct |
2 |
Correct |
11 ms |
57180 KB |
Output is correct |
3 |
Correct |
12 ms |
57180 KB |
Output is correct |
4 |
Correct |
11 ms |
57248 KB |
Output is correct |
5 |
Correct |
11 ms |
57180 KB |
Output is correct |
6 |
Correct |
12 ms |
57180 KB |
Output is correct |
7 |
Correct |
11 ms |
55136 KB |
Output is correct |
8 |
Correct |
11 ms |
57180 KB |
Output is correct |
9 |
Correct |
11 ms |
57236 KB |
Output is correct |
10 |
Correct |
11 ms |
57236 KB |
Output is correct |
11 |
Correct |
12 ms |
57180 KB |
Output is correct |
12 |
Correct |
11 ms |
57152 KB |
Output is correct |
13 |
Correct |
12 ms |
57180 KB |
Output is correct |
14 |
Correct |
12 ms |
57180 KB |
Output is correct |
15 |
Correct |
12 ms |
57176 KB |
Output is correct |
16 |
Correct |
11 ms |
55128 KB |
Output is correct |
17 |
Correct |
12 ms |
57180 KB |
Output is correct |
18 |
Correct |
12 ms |
57180 KB |
Output is correct |
19 |
Correct |
14 ms |
55132 KB |
Output is correct |
20 |
Correct |
11 ms |
55132 KB |
Output is correct |
21 |
Correct |
12 ms |
57140 KB |
Output is correct |
22 |
Correct |
11 ms |
57260 KB |
Output is correct |
23 |
Correct |
11 ms |
57180 KB |
Output is correct |
24 |
Correct |
12 ms |
57180 KB |
Output is correct |
25 |
Correct |
12 ms |
57180 KB |
Output is correct |
26 |
Correct |
14 ms |
57556 KB |
Output is correct |
27 |
Correct |
12 ms |
57192 KB |
Output is correct |
28 |
Correct |
14 ms |
55728 KB |
Output is correct |
29 |
Correct |
15 ms |
55644 KB |
Output is correct |
30 |
Correct |
14 ms |
57176 KB |
Output is correct |
31 |
Correct |
12 ms |
57180 KB |
Output is correct |
32 |
Correct |
14 ms |
57972 KB |
Output is correct |
33 |
Correct |
11 ms |
55384 KB |
Output is correct |
34 |
Correct |
13 ms |
57300 KB |
Output is correct |
35 |
Correct |
14 ms |
55388 KB |
Output is correct |
36 |
Correct |
13 ms |
57180 KB |
Output is correct |
37 |
Correct |
14 ms |
57368 KB |
Output is correct |
38 |
Correct |
12 ms |
57160 KB |
Output is correct |
39 |
Correct |
15 ms |
57436 KB |
Output is correct |
40 |
Correct |
12 ms |
57180 KB |
Output is correct |
41 |
Correct |
14 ms |
57180 KB |
Output is correct |
42 |
Correct |
14 ms |
57436 KB |
Output is correct |
43 |
Correct |
13 ms |
58040 KB |
Output is correct |
44 |
Correct |
14 ms |
55132 KB |
Output is correct |
45 |
Correct |
17 ms |
57688 KB |
Output is correct |
46 |
Correct |
14 ms |
57436 KB |
Output is correct |
47 |
Correct |
11 ms |
57180 KB |
Output is correct |
48 |
Correct |
14 ms |
57336 KB |
Output is correct |
49 |
Correct |
14 ms |
55632 KB |
Output is correct |
50 |
Correct |
14 ms |
55644 KB |
Output is correct |
51 |
Correct |
13 ms |
57272 KB |
Output is correct |
52 |
Correct |
13 ms |
57180 KB |
Output is correct |
53 |
Correct |
13 ms |
57452 KB |
Output is correct |
54 |
Correct |
15 ms |
57176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
57180 KB |
Output is correct |
2 |
Correct |
11 ms |
57180 KB |
Output is correct |
3 |
Correct |
12 ms |
57180 KB |
Output is correct |
4 |
Correct |
11 ms |
57248 KB |
Output is correct |
5 |
Correct |
11 ms |
57180 KB |
Output is correct |
6 |
Correct |
12 ms |
57180 KB |
Output is correct |
7 |
Correct |
11 ms |
55136 KB |
Output is correct |
8 |
Correct |
11 ms |
57180 KB |
Output is correct |
9 |
Correct |
11 ms |
57236 KB |
Output is correct |
10 |
Correct |
11 ms |
57236 KB |
Output is correct |
11 |
Correct |
12 ms |
57180 KB |
Output is correct |
12 |
Correct |
11 ms |
57152 KB |
Output is correct |
13 |
Correct |
12 ms |
57180 KB |
Output is correct |
14 |
Correct |
12 ms |
57180 KB |
Output is correct |
15 |
Correct |
12 ms |
57176 KB |
Output is correct |
16 |
Correct |
11 ms |
55128 KB |
Output is correct |
17 |
Correct |
12 ms |
57180 KB |
Output is correct |
18 |
Correct |
12 ms |
57180 KB |
Output is correct |
19 |
Correct |
14 ms |
55132 KB |
Output is correct |
20 |
Correct |
11 ms |
55132 KB |
Output is correct |
21 |
Correct |
12 ms |
57140 KB |
Output is correct |
22 |
Correct |
11 ms |
57260 KB |
Output is correct |
23 |
Correct |
11 ms |
57180 KB |
Output is correct |
24 |
Correct |
12 ms |
57180 KB |
Output is correct |
25 |
Correct |
11 ms |
57176 KB |
Output is correct |
26 |
Correct |
49 ms |
60976 KB |
Output is correct |
27 |
Correct |
83 ms |
61012 KB |
Output is correct |
28 |
Correct |
13 ms |
57180 KB |
Output is correct |
29 |
Correct |
12 ms |
57236 KB |
Output is correct |
30 |
Correct |
13 ms |
55204 KB |
Output is correct |
31 |
Correct |
57 ms |
60756 KB |
Output is correct |
32 |
Correct |
12 ms |
57180 KB |
Output is correct |
33 |
Correct |
89 ms |
82004 KB |
Output is correct |
34 |
Correct |
69 ms |
60988 KB |
Output is correct |
35 |
Correct |
13 ms |
57176 KB |
Output is correct |
36 |
Correct |
61 ms |
62804 KB |
Output is correct |
37 |
Correct |
13 ms |
57180 KB |
Output is correct |
38 |
Correct |
13 ms |
57180 KB |
Output is correct |
39 |
Correct |
50 ms |
60916 KB |
Output is correct |
40 |
Correct |
14 ms |
57944 KB |
Output is correct |
41 |
Correct |
55 ms |
60724 KB |
Output is correct |
42 |
Correct |
77 ms |
67408 KB |
Output is correct |
43 |
Correct |
11 ms |
57180 KB |
Output is correct |
44 |
Correct |
85 ms |
83024 KB |
Output is correct |
45 |
Correct |
84 ms |
74320 KB |
Output is correct |
46 |
Correct |
12 ms |
57180 KB |
Output is correct |
47 |
Correct |
14 ms |
57180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
61136 KB |
Output is correct |
2 |
Correct |
113 ms |
69052 KB |
Output is correct |
3 |
Correct |
14 ms |
57544 KB |
Output is correct |
4 |
Correct |
13 ms |
57180 KB |
Output is correct |
5 |
Correct |
12 ms |
57176 KB |
Output is correct |
6 |
Correct |
11 ms |
55132 KB |
Output is correct |
7 |
Correct |
14 ms |
57180 KB |
Output is correct |
8 |
Correct |
135 ms |
64360 KB |
Output is correct |
9 |
Correct |
12 ms |
57176 KB |
Output is correct |
10 |
Correct |
58 ms |
61008 KB |
Output is correct |
11 |
Correct |
12 ms |
57176 KB |
Output is correct |
12 |
Correct |
57 ms |
60548 KB |
Output is correct |
13 |
Correct |
115 ms |
64592 KB |
Output is correct |
14 |
Correct |
201 ms |
69948 KB |
Output is correct |
15 |
Correct |
50 ms |
60992 KB |
Output is correct |
16 |
Correct |
14 ms |
57436 KB |
Output is correct |
17 |
Correct |
11 ms |
55132 KB |
Output is correct |
18 |
Correct |
111 ms |
68548 KB |
Output is correct |
19 |
Correct |
154 ms |
77140 KB |
Output is correct |
20 |
Correct |
14 ms |
57692 KB |
Output is correct |
21 |
Correct |
11 ms |
57180 KB |
Output is correct |
22 |
Correct |
92 ms |
64960 KB |
Output is correct |
23 |
Correct |
14 ms |
57180 KB |
Output is correct |
24 |
Correct |
69 ms |
61520 KB |
Output is correct |
25 |
Correct |
151 ms |
75796 KB |
Output is correct |
26 |
Correct |
13 ms |
55388 KB |
Output is correct |
27 |
Correct |
14 ms |
55636 KB |
Output is correct |
28 |
Correct |
13 ms |
57180 KB |
Output is correct |
29 |
Correct |
12 ms |
57176 KB |
Output is correct |
30 |
Correct |
14 ms |
57436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
57180 KB |
Output is correct |
2 |
Correct |
11 ms |
57180 KB |
Output is correct |
3 |
Correct |
12 ms |
57180 KB |
Output is correct |
4 |
Correct |
11 ms |
57248 KB |
Output is correct |
5 |
Correct |
11 ms |
57180 KB |
Output is correct |
6 |
Correct |
12 ms |
57180 KB |
Output is correct |
7 |
Correct |
11 ms |
55136 KB |
Output is correct |
8 |
Correct |
11 ms |
57180 KB |
Output is correct |
9 |
Correct |
11 ms |
57236 KB |
Output is correct |
10 |
Correct |
11 ms |
57236 KB |
Output is correct |
11 |
Correct |
12 ms |
57180 KB |
Output is correct |
12 |
Correct |
11 ms |
57152 KB |
Output is correct |
13 |
Correct |
12 ms |
57180 KB |
Output is correct |
14 |
Correct |
12 ms |
57180 KB |
Output is correct |
15 |
Correct |
12 ms |
57176 KB |
Output is correct |
16 |
Correct |
11 ms |
55128 KB |
Output is correct |
17 |
Correct |
12 ms |
57180 KB |
Output is correct |
18 |
Correct |
12 ms |
57180 KB |
Output is correct |
19 |
Correct |
14 ms |
55132 KB |
Output is correct |
20 |
Correct |
11 ms |
55132 KB |
Output is correct |
21 |
Correct |
12 ms |
57140 KB |
Output is correct |
22 |
Correct |
11 ms |
57260 KB |
Output is correct |
23 |
Correct |
11 ms |
57180 KB |
Output is correct |
24 |
Correct |
12 ms |
57180 KB |
Output is correct |
25 |
Correct |
12 ms |
57180 KB |
Output is correct |
26 |
Correct |
14 ms |
57556 KB |
Output is correct |
27 |
Correct |
12 ms |
57192 KB |
Output is correct |
28 |
Correct |
14 ms |
55728 KB |
Output is correct |
29 |
Correct |
15 ms |
55644 KB |
Output is correct |
30 |
Correct |
14 ms |
57176 KB |
Output is correct |
31 |
Correct |
12 ms |
57180 KB |
Output is correct |
32 |
Correct |
14 ms |
57972 KB |
Output is correct |
33 |
Correct |
11 ms |
55384 KB |
Output is correct |
34 |
Correct |
13 ms |
57300 KB |
Output is correct |
35 |
Correct |
14 ms |
55388 KB |
Output is correct |
36 |
Correct |
13 ms |
57180 KB |
Output is correct |
37 |
Correct |
14 ms |
57368 KB |
Output is correct |
38 |
Correct |
12 ms |
57160 KB |
Output is correct |
39 |
Correct |
15 ms |
57436 KB |
Output is correct |
40 |
Correct |
12 ms |
57180 KB |
Output is correct |
41 |
Correct |
14 ms |
57180 KB |
Output is correct |
42 |
Correct |
14 ms |
57436 KB |
Output is correct |
43 |
Correct |
13 ms |
58040 KB |
Output is correct |
44 |
Correct |
14 ms |
55132 KB |
Output is correct |
45 |
Correct |
17 ms |
57688 KB |
Output is correct |
46 |
Correct |
14 ms |
57436 KB |
Output is correct |
47 |
Correct |
11 ms |
57180 KB |
Output is correct |
48 |
Correct |
14 ms |
57336 KB |
Output is correct |
49 |
Correct |
14 ms |
55632 KB |
Output is correct |
50 |
Correct |
14 ms |
55644 KB |
Output is correct |
51 |
Correct |
13 ms |
57272 KB |
Output is correct |
52 |
Correct |
13 ms |
57180 KB |
Output is correct |
53 |
Correct |
13 ms |
57452 KB |
Output is correct |
54 |
Correct |
15 ms |
57176 KB |
Output is correct |
55 |
Correct |
11 ms |
57176 KB |
Output is correct |
56 |
Correct |
49 ms |
60976 KB |
Output is correct |
57 |
Correct |
83 ms |
61012 KB |
Output is correct |
58 |
Correct |
13 ms |
57180 KB |
Output is correct |
59 |
Correct |
12 ms |
57236 KB |
Output is correct |
60 |
Correct |
13 ms |
55204 KB |
Output is correct |
61 |
Correct |
57 ms |
60756 KB |
Output is correct |
62 |
Correct |
12 ms |
57180 KB |
Output is correct |
63 |
Correct |
89 ms |
82004 KB |
Output is correct |
64 |
Correct |
69 ms |
60988 KB |
Output is correct |
65 |
Correct |
13 ms |
57176 KB |
Output is correct |
66 |
Correct |
61 ms |
62804 KB |
Output is correct |
67 |
Correct |
13 ms |
57180 KB |
Output is correct |
68 |
Correct |
13 ms |
57180 KB |
Output is correct |
69 |
Correct |
50 ms |
60916 KB |
Output is correct |
70 |
Correct |
14 ms |
57944 KB |
Output is correct |
71 |
Correct |
55 ms |
60724 KB |
Output is correct |
72 |
Correct |
77 ms |
67408 KB |
Output is correct |
73 |
Correct |
11 ms |
57180 KB |
Output is correct |
74 |
Correct |
85 ms |
83024 KB |
Output is correct |
75 |
Correct |
84 ms |
74320 KB |
Output is correct |
76 |
Correct |
12 ms |
57180 KB |
Output is correct |
77 |
Correct |
14 ms |
57180 KB |
Output is correct |
78 |
Correct |
48 ms |
61136 KB |
Output is correct |
79 |
Correct |
113 ms |
69052 KB |
Output is correct |
80 |
Correct |
14 ms |
57544 KB |
Output is correct |
81 |
Correct |
13 ms |
57180 KB |
Output is correct |
82 |
Correct |
12 ms |
57176 KB |
Output is correct |
83 |
Correct |
11 ms |
55132 KB |
Output is correct |
84 |
Correct |
14 ms |
57180 KB |
Output is correct |
85 |
Correct |
135 ms |
64360 KB |
Output is correct |
86 |
Correct |
12 ms |
57176 KB |
Output is correct |
87 |
Correct |
58 ms |
61008 KB |
Output is correct |
88 |
Correct |
12 ms |
57176 KB |
Output is correct |
89 |
Correct |
57 ms |
60548 KB |
Output is correct |
90 |
Correct |
115 ms |
64592 KB |
Output is correct |
91 |
Correct |
201 ms |
69948 KB |
Output is correct |
92 |
Correct |
50 ms |
60992 KB |
Output is correct |
93 |
Correct |
14 ms |
57436 KB |
Output is correct |
94 |
Correct |
11 ms |
55132 KB |
Output is correct |
95 |
Correct |
111 ms |
68548 KB |
Output is correct |
96 |
Correct |
154 ms |
77140 KB |
Output is correct |
97 |
Correct |
14 ms |
57692 KB |
Output is correct |
98 |
Correct |
11 ms |
57180 KB |
Output is correct |
99 |
Correct |
92 ms |
64960 KB |
Output is correct |
100 |
Correct |
14 ms |
57180 KB |
Output is correct |
101 |
Correct |
69 ms |
61520 KB |
Output is correct |
102 |
Correct |
151 ms |
75796 KB |
Output is correct |
103 |
Correct |
13 ms |
55388 KB |
Output is correct |
104 |
Correct |
14 ms |
55636 KB |
Output is correct |
105 |
Correct |
13 ms |
57180 KB |
Output is correct |
106 |
Correct |
12 ms |
57176 KB |
Output is correct |
107 |
Correct |
14 ms |
57436 KB |
Output is correct |
108 |
Correct |
575 ms |
95016 KB |
Output is correct |
109 |
Correct |
617 ms |
130316 KB |
Output is correct |
110 |
Correct |
640 ms |
164180 KB |
Output is correct |
111 |
Correct |
1159 ms |
174636 KB |
Output is correct |
112 |
Correct |
1070 ms |
153524 KB |
Output is correct |
113 |
Correct |
763 ms |
125192 KB |
Output is correct |
114 |
Correct |
460 ms |
78072 KB |
Output is correct |
115 |
Correct |
446 ms |
77904 KB |
Output is correct |
116 |
Correct |
772 ms |
86184 KB |
Output is correct |
117 |
Correct |
1319 ms |
130116 KB |
Output is correct |
118 |
Correct |
402 ms |
77720 KB |
Output is correct |
119 |
Correct |
1273 ms |
130264 KB |
Output is correct |
120 |
Correct |
1116 ms |
163924 KB |
Output is correct |
121 |
Correct |
1246 ms |
130048 KB |
Output is correct |
122 |
Correct |
1149 ms |
102556 KB |
Output is correct |
123 |
Correct |
774 ms |
129720 KB |
Output is correct |
124 |
Correct |
1137 ms |
118260 KB |
Output is correct |