#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];
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(x!=start&&res[x]==0) vis[x]=true;
//cerr << x << " " << vis[x] << "\n";
}
void dfs2(int x, int p=0) {
for(int y:G[x]) {
if(y==p) continue;
dfs2(y, x);
dp[x] += dp[y];
}
if(dp[x]==0) dp[x] = vis[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(start);
dfs2(start);
//cout << dp[start] << "\n";
cout << (dp[start]+1)/2 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
47192 KB |
Output is correct |
2 |
Correct |
11 ms |
49244 KB |
Output is correct |
3 |
Correct |
12 ms |
49352 KB |
Output is correct |
4 |
Correct |
13 ms |
49244 KB |
Output is correct |
5 |
Correct |
12 ms |
47196 KB |
Output is correct |
6 |
Correct |
11 ms |
47192 KB |
Output is correct |
7 |
Correct |
10 ms |
45144 KB |
Output is correct |
8 |
Correct |
11 ms |
47196 KB |
Output is correct |
9 |
Correct |
11 ms |
49244 KB |
Output is correct |
10 |
Correct |
11 ms |
49240 KB |
Output is correct |
11 |
Correct |
11 ms |
49244 KB |
Output is correct |
12 |
Correct |
11 ms |
49244 KB |
Output is correct |
13 |
Correct |
11 ms |
49244 KB |
Output is correct |
14 |
Correct |
11 ms |
49240 KB |
Output is correct |
15 |
Correct |
11 ms |
49244 KB |
Output is correct |
16 |
Correct |
11 ms |
47196 KB |
Output is correct |
17 |
Correct |
10 ms |
47328 KB |
Output is correct |
18 |
Correct |
11 ms |
47164 KB |
Output is correct |
19 |
Correct |
13 ms |
47196 KB |
Output is correct |
20 |
Correct |
12 ms |
47364 KB |
Output is correct |
21 |
Correct |
11 ms |
47316 KB |
Output is correct |
22 |
Correct |
11 ms |
47192 KB |
Output is correct |
23 |
Correct |
11 ms |
47196 KB |
Output is correct |
24 |
Correct |
11 ms |
49244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
47192 KB |
Output is correct |
2 |
Correct |
11 ms |
49244 KB |
Output is correct |
3 |
Correct |
12 ms |
49352 KB |
Output is correct |
4 |
Correct |
13 ms |
49244 KB |
Output is correct |
5 |
Correct |
12 ms |
47196 KB |
Output is correct |
6 |
Correct |
11 ms |
47192 KB |
Output is correct |
7 |
Correct |
10 ms |
45144 KB |
Output is correct |
8 |
Correct |
11 ms |
47196 KB |
Output is correct |
9 |
Correct |
11 ms |
49244 KB |
Output is correct |
10 |
Correct |
11 ms |
49240 KB |
Output is correct |
11 |
Correct |
11 ms |
49244 KB |
Output is correct |
12 |
Correct |
11 ms |
49244 KB |
Output is correct |
13 |
Correct |
11 ms |
49244 KB |
Output is correct |
14 |
Correct |
11 ms |
49240 KB |
Output is correct |
15 |
Correct |
11 ms |
49244 KB |
Output is correct |
16 |
Correct |
11 ms |
47196 KB |
Output is correct |
17 |
Correct |
10 ms |
47328 KB |
Output is correct |
18 |
Correct |
11 ms |
47164 KB |
Output is correct |
19 |
Correct |
13 ms |
47196 KB |
Output is correct |
20 |
Correct |
12 ms |
47364 KB |
Output is correct |
21 |
Correct |
11 ms |
47316 KB |
Output is correct |
22 |
Correct |
11 ms |
47192 KB |
Output is correct |
23 |
Correct |
11 ms |
47196 KB |
Output is correct |
24 |
Correct |
11 ms |
49244 KB |
Output is correct |
25 |
Correct |
11 ms |
47192 KB |
Output is correct |
26 |
Correct |
13 ms |
50012 KB |
Output is correct |
27 |
Correct |
12 ms |
49888 KB |
Output is correct |
28 |
Correct |
13 ms |
47964 KB |
Output is correct |
29 |
Correct |
16 ms |
47960 KB |
Output is correct |
30 |
Correct |
13 ms |
49752 KB |
Output is correct |
31 |
Correct |
12 ms |
47196 KB |
Output is correct |
32 |
Correct |
13 ms |
50268 KB |
Output is correct |
33 |
Correct |
11 ms |
49440 KB |
Output is correct |
34 |
Correct |
14 ms |
47836 KB |
Output is correct |
35 |
Correct |
15 ms |
47960 KB |
Output is correct |
36 |
Incorrect |
13 ms |
49876 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
47192 KB |
Output is correct |
2 |
Correct |
11 ms |
49244 KB |
Output is correct |
3 |
Correct |
12 ms |
49352 KB |
Output is correct |
4 |
Correct |
13 ms |
49244 KB |
Output is correct |
5 |
Correct |
12 ms |
47196 KB |
Output is correct |
6 |
Correct |
11 ms |
47192 KB |
Output is correct |
7 |
Correct |
10 ms |
45144 KB |
Output is correct |
8 |
Correct |
11 ms |
47196 KB |
Output is correct |
9 |
Correct |
11 ms |
49244 KB |
Output is correct |
10 |
Correct |
11 ms |
49240 KB |
Output is correct |
11 |
Correct |
11 ms |
49244 KB |
Output is correct |
12 |
Correct |
11 ms |
49244 KB |
Output is correct |
13 |
Correct |
11 ms |
49244 KB |
Output is correct |
14 |
Correct |
11 ms |
49240 KB |
Output is correct |
15 |
Correct |
11 ms |
49244 KB |
Output is correct |
16 |
Correct |
11 ms |
47196 KB |
Output is correct |
17 |
Correct |
10 ms |
47328 KB |
Output is correct |
18 |
Correct |
11 ms |
47164 KB |
Output is correct |
19 |
Correct |
13 ms |
47196 KB |
Output is correct |
20 |
Correct |
12 ms |
47364 KB |
Output is correct |
21 |
Correct |
11 ms |
47316 KB |
Output is correct |
22 |
Correct |
11 ms |
47192 KB |
Output is correct |
23 |
Correct |
11 ms |
47196 KB |
Output is correct |
24 |
Correct |
11 ms |
49244 KB |
Output is correct |
25 |
Correct |
10 ms |
47196 KB |
Output is correct |
26 |
Correct |
67 ms |
64964 KB |
Output is correct |
27 |
Correct |
99 ms |
66896 KB |
Output is correct |
28 |
Correct |
13 ms |
49752 KB |
Output is correct |
29 |
Correct |
12 ms |
47200 KB |
Output is correct |
30 |
Correct |
11 ms |
49492 KB |
Output is correct |
31 |
Incorrect |
96 ms |
66772 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
64964 KB |
Output is correct |
2 |
Correct |
113 ms |
69544 KB |
Output is correct |
3 |
Correct |
14 ms |
50008 KB |
Output is correct |
4 |
Correct |
13 ms |
49756 KB |
Output is correct |
5 |
Correct |
11 ms |
47308 KB |
Output is correct |
6 |
Correct |
11 ms |
49244 KB |
Output is correct |
7 |
Correct |
13 ms |
47828 KB |
Output is correct |
8 |
Correct |
137 ms |
70728 KB |
Output is correct |
9 |
Incorrect |
14 ms |
49752 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
47192 KB |
Output is correct |
2 |
Correct |
11 ms |
49244 KB |
Output is correct |
3 |
Correct |
12 ms |
49352 KB |
Output is correct |
4 |
Correct |
13 ms |
49244 KB |
Output is correct |
5 |
Correct |
12 ms |
47196 KB |
Output is correct |
6 |
Correct |
11 ms |
47192 KB |
Output is correct |
7 |
Correct |
10 ms |
45144 KB |
Output is correct |
8 |
Correct |
11 ms |
47196 KB |
Output is correct |
9 |
Correct |
11 ms |
49244 KB |
Output is correct |
10 |
Correct |
11 ms |
49240 KB |
Output is correct |
11 |
Correct |
11 ms |
49244 KB |
Output is correct |
12 |
Correct |
11 ms |
49244 KB |
Output is correct |
13 |
Correct |
11 ms |
49244 KB |
Output is correct |
14 |
Correct |
11 ms |
49240 KB |
Output is correct |
15 |
Correct |
11 ms |
49244 KB |
Output is correct |
16 |
Correct |
11 ms |
47196 KB |
Output is correct |
17 |
Correct |
10 ms |
47328 KB |
Output is correct |
18 |
Correct |
11 ms |
47164 KB |
Output is correct |
19 |
Correct |
13 ms |
47196 KB |
Output is correct |
20 |
Correct |
12 ms |
47364 KB |
Output is correct |
21 |
Correct |
11 ms |
47316 KB |
Output is correct |
22 |
Correct |
11 ms |
47192 KB |
Output is correct |
23 |
Correct |
11 ms |
47196 KB |
Output is correct |
24 |
Correct |
11 ms |
49244 KB |
Output is correct |
25 |
Correct |
11 ms |
47192 KB |
Output is correct |
26 |
Correct |
13 ms |
50012 KB |
Output is correct |
27 |
Correct |
12 ms |
49888 KB |
Output is correct |
28 |
Correct |
13 ms |
47964 KB |
Output is correct |
29 |
Correct |
16 ms |
47960 KB |
Output is correct |
30 |
Correct |
13 ms |
49752 KB |
Output is correct |
31 |
Correct |
12 ms |
47196 KB |
Output is correct |
32 |
Correct |
13 ms |
50268 KB |
Output is correct |
33 |
Correct |
11 ms |
49440 KB |
Output is correct |
34 |
Correct |
14 ms |
47836 KB |
Output is correct |
35 |
Correct |
15 ms |
47960 KB |
Output is correct |
36 |
Incorrect |
13 ms |
49876 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |