/* In the name of God, aka Allah */
// let this be mytemp.cpp
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 5e5 + 50;
int n, k, cnt[N], ans, par[N];
int pr[N], sz[N];
int d[N];
bool mark[N];
vector<int> adj[N];
map<int, int> mp[N];
int getroot(int u) {
if (pr[u] == u) return u;
return pr[u] = getroot(pr[u]);
}
void uniset(int u, int v) {
u = getroot(u);
v = getroot(v);
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
pr[v] = u;
sz[u] += sz[v];
}
void dfs(int u, int p) {
par[u] = p;
for (int v:adj[u]) {
if (v == p) continue;
dfs(v, u);
if (!mp[v].empty()) uniset(u, v);
else mark[v] = 1;
if (mp[u].size() < mp[v].size()) swap(mp[u], mp[v]);
for (pii x:mp[v]) {
mp[u][x.F] += x.S;
if (mp[u][x.F] == cnt[x.F]) mp[u].erase(x.F);
}
mp[v].clear();
}
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n-1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int u = 1; u <= n; u++) {
int c;
cin >> c;
mp[u][c]++;
cnt[c]++;
pr[u] = u;
sz[u] = 1;
}
for (int u = 1; u <= n; u++) {
if (mp[u].begin()->S == cnt[mp[u].begin()->F]) mp[u].clear();
}
dfs(1, 0);
for (int u = 1; u <= n; u++) {
if (mark[u]) {
d[getroot(u)]++;
d[getroot(par[u])]++;
}
}
set<int> st;
for (int u = 1; u <= n; u++) {
if (d[u] == 1) st.insert(u);
}
cout << (st.size()+1)/2 << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35592 KB |
Output is correct |
2 |
Correct |
20 ms |
35560 KB |
Output is correct |
3 |
Correct |
19 ms |
35588 KB |
Output is correct |
4 |
Correct |
17 ms |
35564 KB |
Output is correct |
5 |
Correct |
16 ms |
35472 KB |
Output is correct |
6 |
Correct |
17 ms |
35548 KB |
Output is correct |
7 |
Correct |
18 ms |
35540 KB |
Output is correct |
8 |
Correct |
17 ms |
35504 KB |
Output is correct |
9 |
Correct |
21 ms |
35548 KB |
Output is correct |
10 |
Correct |
16 ms |
35540 KB |
Output is correct |
11 |
Correct |
16 ms |
35520 KB |
Output is correct |
12 |
Correct |
17 ms |
35552 KB |
Output is correct |
13 |
Correct |
21 ms |
35592 KB |
Output is correct |
14 |
Correct |
18 ms |
35540 KB |
Output is correct |
15 |
Correct |
19 ms |
35564 KB |
Output is correct |
16 |
Correct |
17 ms |
35564 KB |
Output is correct |
17 |
Correct |
17 ms |
35524 KB |
Output is correct |
18 |
Correct |
17 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35492 KB |
Output is correct |
20 |
Correct |
17 ms |
35576 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
19 ms |
35584 KB |
Output is correct |
23 |
Correct |
17 ms |
35540 KB |
Output is correct |
24 |
Correct |
19 ms |
35540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35592 KB |
Output is correct |
2 |
Correct |
20 ms |
35560 KB |
Output is correct |
3 |
Correct |
19 ms |
35588 KB |
Output is correct |
4 |
Correct |
17 ms |
35564 KB |
Output is correct |
5 |
Correct |
16 ms |
35472 KB |
Output is correct |
6 |
Correct |
17 ms |
35548 KB |
Output is correct |
7 |
Correct |
18 ms |
35540 KB |
Output is correct |
8 |
Correct |
17 ms |
35504 KB |
Output is correct |
9 |
Correct |
21 ms |
35548 KB |
Output is correct |
10 |
Correct |
16 ms |
35540 KB |
Output is correct |
11 |
Correct |
16 ms |
35520 KB |
Output is correct |
12 |
Correct |
17 ms |
35552 KB |
Output is correct |
13 |
Correct |
21 ms |
35592 KB |
Output is correct |
14 |
Correct |
18 ms |
35540 KB |
Output is correct |
15 |
Correct |
19 ms |
35564 KB |
Output is correct |
16 |
Correct |
17 ms |
35564 KB |
Output is correct |
17 |
Correct |
17 ms |
35524 KB |
Output is correct |
18 |
Correct |
17 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35492 KB |
Output is correct |
20 |
Correct |
17 ms |
35576 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
19 ms |
35584 KB |
Output is correct |
23 |
Correct |
17 ms |
35540 KB |
Output is correct |
24 |
Correct |
19 ms |
35540 KB |
Output is correct |
25 |
Correct |
17 ms |
35540 KB |
Output is correct |
26 |
Correct |
18 ms |
35796 KB |
Output is correct |
27 |
Correct |
19 ms |
35916 KB |
Output is correct |
28 |
Correct |
23 ms |
36056 KB |
Output is correct |
29 |
Correct |
19 ms |
35796 KB |
Output is correct |
30 |
Correct |
20 ms |
35860 KB |
Output is correct |
31 |
Correct |
17 ms |
35600 KB |
Output is correct |
32 |
Correct |
20 ms |
36180 KB |
Output is correct |
33 |
Correct |
21 ms |
35564 KB |
Output is correct |
34 |
Correct |
17 ms |
35836 KB |
Output is correct |
35 |
Correct |
19 ms |
35772 KB |
Output is correct |
36 |
Correct |
18 ms |
35812 KB |
Output is correct |
37 |
Correct |
18 ms |
35792 KB |
Output is correct |
38 |
Correct |
17 ms |
35540 KB |
Output is correct |
39 |
Correct |
20 ms |
35776 KB |
Output is correct |
40 |
Correct |
20 ms |
35852 KB |
Output is correct |
41 |
Correct |
19 ms |
35796 KB |
Output is correct |
42 |
Correct |
19 ms |
35796 KB |
Output is correct |
43 |
Correct |
18 ms |
36284 KB |
Output is correct |
44 |
Correct |
18 ms |
35540 KB |
Output is correct |
45 |
Correct |
18 ms |
35944 KB |
Output is correct |
46 |
Correct |
19 ms |
35796 KB |
Output is correct |
47 |
Correct |
17 ms |
35540 KB |
Output is correct |
48 |
Correct |
19 ms |
35796 KB |
Output is correct |
49 |
Correct |
24 ms |
35832 KB |
Output is correct |
50 |
Correct |
19 ms |
35900 KB |
Output is correct |
51 |
Correct |
21 ms |
35816 KB |
Output is correct |
52 |
Correct |
19 ms |
35768 KB |
Output is correct |
53 |
Correct |
19 ms |
35836 KB |
Output is correct |
54 |
Correct |
20 ms |
35764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35592 KB |
Output is correct |
2 |
Correct |
20 ms |
35560 KB |
Output is correct |
3 |
Correct |
19 ms |
35588 KB |
Output is correct |
4 |
Correct |
17 ms |
35564 KB |
Output is correct |
5 |
Correct |
16 ms |
35472 KB |
Output is correct |
6 |
Correct |
17 ms |
35548 KB |
Output is correct |
7 |
Correct |
18 ms |
35540 KB |
Output is correct |
8 |
Correct |
17 ms |
35504 KB |
Output is correct |
9 |
Correct |
21 ms |
35548 KB |
Output is correct |
10 |
Correct |
16 ms |
35540 KB |
Output is correct |
11 |
Correct |
16 ms |
35520 KB |
Output is correct |
12 |
Correct |
17 ms |
35552 KB |
Output is correct |
13 |
Correct |
21 ms |
35592 KB |
Output is correct |
14 |
Correct |
18 ms |
35540 KB |
Output is correct |
15 |
Correct |
19 ms |
35564 KB |
Output is correct |
16 |
Correct |
17 ms |
35564 KB |
Output is correct |
17 |
Correct |
17 ms |
35524 KB |
Output is correct |
18 |
Correct |
17 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35492 KB |
Output is correct |
20 |
Correct |
17 ms |
35576 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
19 ms |
35584 KB |
Output is correct |
23 |
Correct |
17 ms |
35540 KB |
Output is correct |
24 |
Correct |
19 ms |
35540 KB |
Output is correct |
25 |
Correct |
17 ms |
35596 KB |
Output is correct |
26 |
Correct |
81 ms |
44916 KB |
Output is correct |
27 |
Correct |
120 ms |
44792 KB |
Output is correct |
28 |
Correct |
20 ms |
35864 KB |
Output is correct |
29 |
Correct |
17 ms |
35516 KB |
Output is correct |
30 |
Correct |
18 ms |
35472 KB |
Output is correct |
31 |
Correct |
86 ms |
45028 KB |
Output is correct |
32 |
Correct |
24 ms |
35788 KB |
Output is correct |
33 |
Correct |
112 ms |
59124 KB |
Output is correct |
34 |
Correct |
105 ms |
44648 KB |
Output is correct |
35 |
Correct |
20 ms |
35852 KB |
Output is correct |
36 |
Correct |
95 ms |
46408 KB |
Output is correct |
37 |
Correct |
18 ms |
35916 KB |
Output is correct |
38 |
Correct |
19 ms |
35876 KB |
Output is correct |
39 |
Correct |
76 ms |
44948 KB |
Output is correct |
40 |
Correct |
21 ms |
36328 KB |
Output is correct |
41 |
Correct |
84 ms |
44684 KB |
Output is correct |
42 |
Correct |
106 ms |
49416 KB |
Output is correct |
43 |
Correct |
17 ms |
35508 KB |
Output is correct |
44 |
Correct |
96 ms |
59720 KB |
Output is correct |
45 |
Correct |
102 ms |
52212 KB |
Output is correct |
46 |
Correct |
23 ms |
35788 KB |
Output is correct |
47 |
Correct |
19 ms |
35744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
44908 KB |
Output is correct |
2 |
Correct |
80 ms |
45796 KB |
Output is correct |
3 |
Correct |
19 ms |
35780 KB |
Output is correct |
4 |
Correct |
20 ms |
35796 KB |
Output is correct |
5 |
Correct |
18 ms |
35536 KB |
Output is correct |
6 |
Correct |
17 ms |
35476 KB |
Output is correct |
7 |
Correct |
19 ms |
35768 KB |
Output is correct |
8 |
Correct |
109 ms |
45372 KB |
Output is correct |
9 |
Correct |
19 ms |
35796 KB |
Output is correct |
10 |
Correct |
84 ms |
45104 KB |
Output is correct |
11 |
Correct |
17 ms |
35504 KB |
Output is correct |
12 |
Correct |
94 ms |
45164 KB |
Output is correct |
13 |
Correct |
96 ms |
45388 KB |
Output is correct |
14 |
Correct |
82 ms |
45568 KB |
Output is correct |
15 |
Correct |
78 ms |
45044 KB |
Output is correct |
16 |
Correct |
23 ms |
35956 KB |
Output is correct |
17 |
Correct |
19 ms |
35548 KB |
Output is correct |
18 |
Correct |
89 ms |
45728 KB |
Output is correct |
19 |
Correct |
82 ms |
53432 KB |
Output is correct |
20 |
Correct |
20 ms |
35784 KB |
Output is correct |
21 |
Correct |
20 ms |
35596 KB |
Output is correct |
22 |
Correct |
110 ms |
45564 KB |
Output is correct |
23 |
Correct |
19 ms |
35888 KB |
Output is correct |
24 |
Correct |
109 ms |
45104 KB |
Output is correct |
25 |
Correct |
87 ms |
50844 KB |
Output is correct |
26 |
Correct |
20 ms |
35796 KB |
Output is correct |
27 |
Correct |
19 ms |
36052 KB |
Output is correct |
28 |
Correct |
20 ms |
35876 KB |
Output is correct |
29 |
Correct |
20 ms |
35784 KB |
Output is correct |
30 |
Correct |
20 ms |
35896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35592 KB |
Output is correct |
2 |
Correct |
20 ms |
35560 KB |
Output is correct |
3 |
Correct |
19 ms |
35588 KB |
Output is correct |
4 |
Correct |
17 ms |
35564 KB |
Output is correct |
5 |
Correct |
16 ms |
35472 KB |
Output is correct |
6 |
Correct |
17 ms |
35548 KB |
Output is correct |
7 |
Correct |
18 ms |
35540 KB |
Output is correct |
8 |
Correct |
17 ms |
35504 KB |
Output is correct |
9 |
Correct |
21 ms |
35548 KB |
Output is correct |
10 |
Correct |
16 ms |
35540 KB |
Output is correct |
11 |
Correct |
16 ms |
35520 KB |
Output is correct |
12 |
Correct |
17 ms |
35552 KB |
Output is correct |
13 |
Correct |
21 ms |
35592 KB |
Output is correct |
14 |
Correct |
18 ms |
35540 KB |
Output is correct |
15 |
Correct |
19 ms |
35564 KB |
Output is correct |
16 |
Correct |
17 ms |
35564 KB |
Output is correct |
17 |
Correct |
17 ms |
35524 KB |
Output is correct |
18 |
Correct |
17 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35492 KB |
Output is correct |
20 |
Correct |
17 ms |
35576 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
19 ms |
35584 KB |
Output is correct |
23 |
Correct |
17 ms |
35540 KB |
Output is correct |
24 |
Correct |
19 ms |
35540 KB |
Output is correct |
25 |
Correct |
17 ms |
35540 KB |
Output is correct |
26 |
Correct |
18 ms |
35796 KB |
Output is correct |
27 |
Correct |
19 ms |
35916 KB |
Output is correct |
28 |
Correct |
23 ms |
36056 KB |
Output is correct |
29 |
Correct |
19 ms |
35796 KB |
Output is correct |
30 |
Correct |
20 ms |
35860 KB |
Output is correct |
31 |
Correct |
17 ms |
35600 KB |
Output is correct |
32 |
Correct |
20 ms |
36180 KB |
Output is correct |
33 |
Correct |
21 ms |
35564 KB |
Output is correct |
34 |
Correct |
17 ms |
35836 KB |
Output is correct |
35 |
Correct |
19 ms |
35772 KB |
Output is correct |
36 |
Correct |
18 ms |
35812 KB |
Output is correct |
37 |
Correct |
18 ms |
35792 KB |
Output is correct |
38 |
Correct |
17 ms |
35540 KB |
Output is correct |
39 |
Correct |
20 ms |
35776 KB |
Output is correct |
40 |
Correct |
20 ms |
35852 KB |
Output is correct |
41 |
Correct |
19 ms |
35796 KB |
Output is correct |
42 |
Correct |
19 ms |
35796 KB |
Output is correct |
43 |
Correct |
18 ms |
36284 KB |
Output is correct |
44 |
Correct |
18 ms |
35540 KB |
Output is correct |
45 |
Correct |
18 ms |
35944 KB |
Output is correct |
46 |
Correct |
19 ms |
35796 KB |
Output is correct |
47 |
Correct |
17 ms |
35540 KB |
Output is correct |
48 |
Correct |
19 ms |
35796 KB |
Output is correct |
49 |
Correct |
24 ms |
35832 KB |
Output is correct |
50 |
Correct |
19 ms |
35900 KB |
Output is correct |
51 |
Correct |
21 ms |
35816 KB |
Output is correct |
52 |
Correct |
19 ms |
35768 KB |
Output is correct |
53 |
Correct |
19 ms |
35836 KB |
Output is correct |
54 |
Correct |
20 ms |
35764 KB |
Output is correct |
55 |
Correct |
17 ms |
35596 KB |
Output is correct |
56 |
Correct |
81 ms |
44916 KB |
Output is correct |
57 |
Correct |
120 ms |
44792 KB |
Output is correct |
58 |
Correct |
20 ms |
35864 KB |
Output is correct |
59 |
Correct |
17 ms |
35516 KB |
Output is correct |
60 |
Correct |
18 ms |
35472 KB |
Output is correct |
61 |
Correct |
86 ms |
45028 KB |
Output is correct |
62 |
Correct |
24 ms |
35788 KB |
Output is correct |
63 |
Correct |
112 ms |
59124 KB |
Output is correct |
64 |
Correct |
105 ms |
44648 KB |
Output is correct |
65 |
Correct |
20 ms |
35852 KB |
Output is correct |
66 |
Correct |
95 ms |
46408 KB |
Output is correct |
67 |
Correct |
18 ms |
35916 KB |
Output is correct |
68 |
Correct |
19 ms |
35876 KB |
Output is correct |
69 |
Correct |
76 ms |
44948 KB |
Output is correct |
70 |
Correct |
21 ms |
36328 KB |
Output is correct |
71 |
Correct |
84 ms |
44684 KB |
Output is correct |
72 |
Correct |
106 ms |
49416 KB |
Output is correct |
73 |
Correct |
17 ms |
35508 KB |
Output is correct |
74 |
Correct |
96 ms |
59720 KB |
Output is correct |
75 |
Correct |
102 ms |
52212 KB |
Output is correct |
76 |
Correct |
23 ms |
35788 KB |
Output is correct |
77 |
Correct |
19 ms |
35744 KB |
Output is correct |
78 |
Correct |
75 ms |
44908 KB |
Output is correct |
79 |
Correct |
80 ms |
45796 KB |
Output is correct |
80 |
Correct |
19 ms |
35780 KB |
Output is correct |
81 |
Correct |
20 ms |
35796 KB |
Output is correct |
82 |
Correct |
18 ms |
35536 KB |
Output is correct |
83 |
Correct |
17 ms |
35476 KB |
Output is correct |
84 |
Correct |
19 ms |
35768 KB |
Output is correct |
85 |
Correct |
109 ms |
45372 KB |
Output is correct |
86 |
Correct |
19 ms |
35796 KB |
Output is correct |
87 |
Correct |
84 ms |
45104 KB |
Output is correct |
88 |
Correct |
17 ms |
35504 KB |
Output is correct |
89 |
Correct |
94 ms |
45164 KB |
Output is correct |
90 |
Correct |
96 ms |
45388 KB |
Output is correct |
91 |
Correct |
82 ms |
45568 KB |
Output is correct |
92 |
Correct |
78 ms |
45044 KB |
Output is correct |
93 |
Correct |
23 ms |
35956 KB |
Output is correct |
94 |
Correct |
19 ms |
35548 KB |
Output is correct |
95 |
Correct |
89 ms |
45728 KB |
Output is correct |
96 |
Correct |
82 ms |
53432 KB |
Output is correct |
97 |
Correct |
20 ms |
35784 KB |
Output is correct |
98 |
Correct |
20 ms |
35596 KB |
Output is correct |
99 |
Correct |
110 ms |
45564 KB |
Output is correct |
100 |
Correct |
19 ms |
35888 KB |
Output is correct |
101 |
Correct |
109 ms |
45104 KB |
Output is correct |
102 |
Correct |
87 ms |
50844 KB |
Output is correct |
103 |
Correct |
20 ms |
35796 KB |
Output is correct |
104 |
Correct |
19 ms |
36052 KB |
Output is correct |
105 |
Correct |
20 ms |
35876 KB |
Output is correct |
106 |
Correct |
20 ms |
35784 KB |
Output is correct |
107 |
Correct |
20 ms |
35896 KB |
Output is correct |
108 |
Correct |
800 ms |
85732 KB |
Output is correct |
109 |
Correct |
636 ms |
117600 KB |
Output is correct |
110 |
Correct |
641 ms |
140128 KB |
Output is correct |
111 |
Correct |
585 ms |
132092 KB |
Output is correct |
112 |
Correct |
636 ms |
112508 KB |
Output is correct |
113 |
Correct |
485 ms |
86612 KB |
Output is correct |
114 |
Correct |
554 ms |
81116 KB |
Output is correct |
115 |
Correct |
524 ms |
81112 KB |
Output is correct |
116 |
Correct |
844 ms |
83800 KB |
Output is correct |
117 |
Correct |
507 ms |
85508 KB |
Output is correct |
118 |
Correct |
520 ms |
82208 KB |
Output is correct |
119 |
Correct |
538 ms |
85532 KB |
Output is correct |
120 |
Correct |
564 ms |
113996 KB |
Output is correct |
121 |
Correct |
539 ms |
85472 KB |
Output is correct |
122 |
Correct |
1081 ms |
84692 KB |
Output is correct |
123 |
Correct |
442 ms |
86852 KB |
Output is correct |
124 |
Correct |
509 ms |
84828 KB |
Output is correct |