#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const int MAX_N = 1e5 + 10;
vector<int> g[MAX_N], nw[MAX_N];
int cnt[MAX_N], cntNow[MAX_N], col[MAX_N];
int n, k, ret;
int dp[MAX_N][2];
void dfsCalc(int x, int p, int delta = 1) {
cntNow[col[x]] += delta;
for(auto it : g[x]) {
if(it == p) {continue;}
dfsCalc(it, x, delta);
}
}
void dfs(int x, int p, int last) {
dfsCalc(x, p);
bool flag = false;
for(int i = 1; i <= k; i ++) {
if(cntNow[i] != 0 && cntNow[i] != cnt[i]) {
flag = true;
}
}
if(!flag && p != 0) {
nw[last].push_back(x);
last = x;
}
dfsCalc(x, p, -1);
for(auto it : g[x]) {
if(it == p) {continue;}
dfs(it, x, last);
}
}
void calc(int x, int d) {
if(x == 1 && nw[x].size() == 1) {
ret ++;
} else if(nw[x].size() == 0) {
ret ++;
}
for(auto it : nw[x]) {
calc(it, d + 1);
}
}
signed main() {
//ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> k;
for(int i = 0; i < n - 1; i ++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i = 1; i <= n; i ++) {
cin >> col[i];
cnt[col[i]] ++;
}
dfs(1, 0, 1);
calc(1, 0);
if(ret != 1) {
cout << (ret + 1) / 2 << endl;
} else {
cout << 0 << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4952 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5000 KB |
Output is correct |
14 |
Correct |
3 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5000 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
3 ms |
4940 KB |
Output is correct |
24 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4952 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5000 KB |
Output is correct |
14 |
Correct |
3 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5000 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
3 ms |
4940 KB |
Output is correct |
24 |
Correct |
3 ms |
4940 KB |
Output is correct |
25 |
Correct |
3 ms |
4940 KB |
Output is correct |
26 |
Correct |
15 ms |
5184 KB |
Output is correct |
27 |
Correct |
21 ms |
5144 KB |
Output is correct |
28 |
Correct |
93 ms |
5364 KB |
Output is correct |
29 |
Correct |
17 ms |
5196 KB |
Output is correct |
30 |
Correct |
8 ms |
5156 KB |
Output is correct |
31 |
Correct |
3 ms |
4940 KB |
Output is correct |
32 |
Correct |
140 ms |
5280 KB |
Output is correct |
33 |
Correct |
3 ms |
5000 KB |
Output is correct |
34 |
Correct |
6 ms |
5140 KB |
Output is correct |
35 |
Correct |
17 ms |
5196 KB |
Output is correct |
36 |
Correct |
10 ms |
5068 KB |
Output is correct |
37 |
Correct |
14 ms |
5148 KB |
Output is correct |
38 |
Correct |
3 ms |
4940 KB |
Output is correct |
39 |
Correct |
12 ms |
5068 KB |
Output is correct |
40 |
Correct |
7 ms |
5068 KB |
Output is correct |
41 |
Correct |
8 ms |
5068 KB |
Output is correct |
42 |
Correct |
10 ms |
5184 KB |
Output is correct |
43 |
Correct |
197 ms |
5292 KB |
Output is correct |
44 |
Correct |
3 ms |
4940 KB |
Output is correct |
45 |
Correct |
62 ms |
5196 KB |
Output is correct |
46 |
Correct |
16 ms |
5168 KB |
Output is correct |
47 |
Correct |
3 ms |
4940 KB |
Output is correct |
48 |
Correct |
12 ms |
5144 KB |
Output is correct |
49 |
Correct |
15 ms |
5208 KB |
Output is correct |
50 |
Correct |
60 ms |
5248 KB |
Output is correct |
51 |
Correct |
11 ms |
5068 KB |
Output is correct |
52 |
Correct |
6 ms |
5068 KB |
Output is correct |
53 |
Correct |
10 ms |
5148 KB |
Output is correct |
54 |
Correct |
7 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4952 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5000 KB |
Output is correct |
14 |
Correct |
3 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5000 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
3 ms |
4940 KB |
Output is correct |
24 |
Correct |
3 ms |
4940 KB |
Output is correct |
25 |
Correct |
3 ms |
4940 KB |
Output is correct |
26 |
Correct |
118 ms |
10320 KB |
Output is correct |
27 |
Correct |
300 ms |
9964 KB |
Output is correct |
28 |
Correct |
22 ms |
5164 KB |
Output is correct |
29 |
Correct |
3 ms |
5000 KB |
Output is correct |
30 |
Correct |
3 ms |
4940 KB |
Output is correct |
31 |
Correct |
831 ms |
10280 KB |
Output is correct |
32 |
Correct |
6 ms |
5068 KB |
Output is correct |
33 |
Execution timed out |
3073 ms |
14340 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
8892 KB |
Output is correct |
2 |
Execution timed out |
3054 ms |
10096 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4952 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5000 KB |
Output is correct |
14 |
Correct |
3 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5000 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
3 ms |
4940 KB |
Output is correct |
24 |
Correct |
3 ms |
4940 KB |
Output is correct |
25 |
Correct |
3 ms |
4940 KB |
Output is correct |
26 |
Correct |
15 ms |
5184 KB |
Output is correct |
27 |
Correct |
21 ms |
5144 KB |
Output is correct |
28 |
Correct |
93 ms |
5364 KB |
Output is correct |
29 |
Correct |
17 ms |
5196 KB |
Output is correct |
30 |
Correct |
8 ms |
5156 KB |
Output is correct |
31 |
Correct |
3 ms |
4940 KB |
Output is correct |
32 |
Correct |
140 ms |
5280 KB |
Output is correct |
33 |
Correct |
3 ms |
5000 KB |
Output is correct |
34 |
Correct |
6 ms |
5140 KB |
Output is correct |
35 |
Correct |
17 ms |
5196 KB |
Output is correct |
36 |
Correct |
10 ms |
5068 KB |
Output is correct |
37 |
Correct |
14 ms |
5148 KB |
Output is correct |
38 |
Correct |
3 ms |
4940 KB |
Output is correct |
39 |
Correct |
12 ms |
5068 KB |
Output is correct |
40 |
Correct |
7 ms |
5068 KB |
Output is correct |
41 |
Correct |
8 ms |
5068 KB |
Output is correct |
42 |
Correct |
10 ms |
5184 KB |
Output is correct |
43 |
Correct |
197 ms |
5292 KB |
Output is correct |
44 |
Correct |
3 ms |
4940 KB |
Output is correct |
45 |
Correct |
62 ms |
5196 KB |
Output is correct |
46 |
Correct |
16 ms |
5168 KB |
Output is correct |
47 |
Correct |
3 ms |
4940 KB |
Output is correct |
48 |
Correct |
12 ms |
5144 KB |
Output is correct |
49 |
Correct |
15 ms |
5208 KB |
Output is correct |
50 |
Correct |
60 ms |
5248 KB |
Output is correct |
51 |
Correct |
11 ms |
5068 KB |
Output is correct |
52 |
Correct |
6 ms |
5068 KB |
Output is correct |
53 |
Correct |
10 ms |
5148 KB |
Output is correct |
54 |
Correct |
7 ms |
5068 KB |
Output is correct |
55 |
Correct |
3 ms |
4940 KB |
Output is correct |
56 |
Correct |
118 ms |
10320 KB |
Output is correct |
57 |
Correct |
300 ms |
9964 KB |
Output is correct |
58 |
Correct |
22 ms |
5164 KB |
Output is correct |
59 |
Correct |
3 ms |
5000 KB |
Output is correct |
60 |
Correct |
3 ms |
4940 KB |
Output is correct |
61 |
Correct |
831 ms |
10280 KB |
Output is correct |
62 |
Correct |
6 ms |
5068 KB |
Output is correct |
63 |
Execution timed out |
3073 ms |
14340 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |