#include <bits/stdc++.h>
// #pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii, int> ipii;
const int MAXN = 5e5+10;
const int MAXA = 9e3+20;
const ll INF = 1e9+10;
const int LOG = 13;
const int SQRT = 450;
const vector<int> NOL = {};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
vector <int> key = {29, 31};
vector <int> mod = {998244353, 1000000007};
void chmx(int &a, int b){ a = max(a, b); }
int n, k;
vector <int> adj[MAXN], vec[MAXN];
int a[MAXN], tot[MAXN], u[MAXN], v[MAXN];
struct dsu {
int par[MAXN], siz[MAXN];
void bd(){
for(int i=1; i<=n; i++){
par[i] = i; siz[i] = 1;
}
}
int f(int x){
if(par[x]==x) return x;
return par[x] = f(par[x]);
}
bool con(int x, int y){ return f(x) == f(y); }
void mer(int x, int y){
x = f(x); y = f(y);
if(x==y) return;
if(siz[x] > siz[y]) swap(x, y);
par[x] = y; siz[y] += siz[x];
}
} DSU;
int col;
int dfs(int nw, int par){
int sub = 0;
if(a[nw]==col) sub++;
for(auto nx : adj[nw]){
if(nx==par) continue;
sub += dfs(nx, nw);
}
if(sub!=0 && par!=0){
DSU.mer(nw, par);
// cout << nw << ' ' << par << " par\n";
}
return sub;
}
vector <int> edge[MAXN];
signed main(){
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> k; DSU.bd();
for(int i=1; i<=n-1; i++){
int x, y; cin >> x >> y; u[i] = x; v[i] = y;
adj[x].pb(y); adj[y].pb(x);
}
for(int i=1; i<=n; i++){
cin >> a[i]; tot[a[i]]++;
vec[a[i]].pb(i);
}
for(int i=1; i<=k; i++){
col = i;
dfs(vec[i][0], 0);
}
for(int i=1; i<=n-1; i++){
int x = DSU.f(u[i]), y = DSU.f(v[i]);
if(x == y) continue;
edge[x].pb(y); edge[y].pb(x);
}
int tot = 0;
for(int i=1; i<=n; i++){
if(edge[i].size() == 1) tot++;
}
cout << (tot+1)/2 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35676 KB |
Output is correct |
2 |
Correct |
15 ms |
35672 KB |
Output is correct |
3 |
Correct |
16 ms |
35676 KB |
Output is correct |
4 |
Correct |
15 ms |
35676 KB |
Output is correct |
5 |
Correct |
16 ms |
35676 KB |
Output is correct |
6 |
Correct |
15 ms |
35672 KB |
Output is correct |
7 |
Correct |
16 ms |
35676 KB |
Output is correct |
8 |
Correct |
16 ms |
35540 KB |
Output is correct |
9 |
Correct |
15 ms |
35544 KB |
Output is correct |
10 |
Correct |
20 ms |
35804 KB |
Output is correct |
11 |
Correct |
16 ms |
35672 KB |
Output is correct |
12 |
Correct |
16 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35676 KB |
Output is correct |
14 |
Correct |
16 ms |
35676 KB |
Output is correct |
15 |
Correct |
15 ms |
35676 KB |
Output is correct |
16 |
Correct |
16 ms |
35676 KB |
Output is correct |
17 |
Correct |
16 ms |
35548 KB |
Output is correct |
18 |
Correct |
16 ms |
35676 KB |
Output is correct |
19 |
Correct |
16 ms |
35716 KB |
Output is correct |
20 |
Correct |
16 ms |
35672 KB |
Output is correct |
21 |
Correct |
16 ms |
35676 KB |
Output is correct |
22 |
Correct |
16 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35588 KB |
Output is correct |
24 |
Correct |
16 ms |
35512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35676 KB |
Output is correct |
2 |
Correct |
15 ms |
35672 KB |
Output is correct |
3 |
Correct |
16 ms |
35676 KB |
Output is correct |
4 |
Correct |
15 ms |
35676 KB |
Output is correct |
5 |
Correct |
16 ms |
35676 KB |
Output is correct |
6 |
Correct |
15 ms |
35672 KB |
Output is correct |
7 |
Correct |
16 ms |
35676 KB |
Output is correct |
8 |
Correct |
16 ms |
35540 KB |
Output is correct |
9 |
Correct |
15 ms |
35544 KB |
Output is correct |
10 |
Correct |
20 ms |
35804 KB |
Output is correct |
11 |
Correct |
16 ms |
35672 KB |
Output is correct |
12 |
Correct |
16 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35676 KB |
Output is correct |
14 |
Correct |
16 ms |
35676 KB |
Output is correct |
15 |
Correct |
15 ms |
35676 KB |
Output is correct |
16 |
Correct |
16 ms |
35676 KB |
Output is correct |
17 |
Correct |
16 ms |
35548 KB |
Output is correct |
18 |
Correct |
16 ms |
35676 KB |
Output is correct |
19 |
Correct |
16 ms |
35716 KB |
Output is correct |
20 |
Correct |
16 ms |
35672 KB |
Output is correct |
21 |
Correct |
16 ms |
35676 KB |
Output is correct |
22 |
Correct |
16 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35588 KB |
Output is correct |
24 |
Correct |
16 ms |
35512 KB |
Output is correct |
25 |
Correct |
15 ms |
35672 KB |
Output is correct |
26 |
Correct |
58 ms |
35932 KB |
Output is correct |
27 |
Correct |
18 ms |
35932 KB |
Output is correct |
28 |
Correct |
82 ms |
36244 KB |
Output is correct |
29 |
Correct |
64 ms |
36088 KB |
Output is correct |
30 |
Correct |
22 ms |
35932 KB |
Output is correct |
31 |
Correct |
14 ms |
35676 KB |
Output is correct |
32 |
Correct |
152 ms |
36380 KB |
Output is correct |
33 |
Correct |
21 ms |
35492 KB |
Output is correct |
34 |
Correct |
24 ms |
35932 KB |
Output is correct |
35 |
Correct |
65 ms |
36092 KB |
Output is correct |
36 |
Correct |
17 ms |
35932 KB |
Output is correct |
37 |
Correct |
52 ms |
35932 KB |
Output is correct |
38 |
Correct |
15 ms |
35672 KB |
Output is correct |
39 |
Correct |
42 ms |
35928 KB |
Output is correct |
40 |
Correct |
17 ms |
35932 KB |
Output is correct |
41 |
Correct |
23 ms |
35944 KB |
Output is correct |
42 |
Correct |
41 ms |
35852 KB |
Output is correct |
43 |
Correct |
16 ms |
36200 KB |
Output is correct |
44 |
Correct |
16 ms |
35596 KB |
Output is correct |
45 |
Correct |
120 ms |
36140 KB |
Output is correct |
46 |
Correct |
48 ms |
36020 KB |
Output is correct |
47 |
Correct |
15 ms |
35516 KB |
Output is correct |
48 |
Correct |
33 ms |
35880 KB |
Output is correct |
49 |
Correct |
68 ms |
36080 KB |
Output is correct |
50 |
Correct |
89 ms |
36260 KB |
Output is correct |
51 |
Correct |
37 ms |
35840 KB |
Output is correct |
52 |
Correct |
18 ms |
36040 KB |
Output is correct |
53 |
Correct |
34 ms |
35932 KB |
Output is correct |
54 |
Correct |
26 ms |
35928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35676 KB |
Output is correct |
2 |
Correct |
15 ms |
35672 KB |
Output is correct |
3 |
Correct |
16 ms |
35676 KB |
Output is correct |
4 |
Correct |
15 ms |
35676 KB |
Output is correct |
5 |
Correct |
16 ms |
35676 KB |
Output is correct |
6 |
Correct |
15 ms |
35672 KB |
Output is correct |
7 |
Correct |
16 ms |
35676 KB |
Output is correct |
8 |
Correct |
16 ms |
35540 KB |
Output is correct |
9 |
Correct |
15 ms |
35544 KB |
Output is correct |
10 |
Correct |
20 ms |
35804 KB |
Output is correct |
11 |
Correct |
16 ms |
35672 KB |
Output is correct |
12 |
Correct |
16 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35676 KB |
Output is correct |
14 |
Correct |
16 ms |
35676 KB |
Output is correct |
15 |
Correct |
15 ms |
35676 KB |
Output is correct |
16 |
Correct |
16 ms |
35676 KB |
Output is correct |
17 |
Correct |
16 ms |
35548 KB |
Output is correct |
18 |
Correct |
16 ms |
35676 KB |
Output is correct |
19 |
Correct |
16 ms |
35716 KB |
Output is correct |
20 |
Correct |
16 ms |
35672 KB |
Output is correct |
21 |
Correct |
16 ms |
35676 KB |
Output is correct |
22 |
Correct |
16 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35588 KB |
Output is correct |
24 |
Correct |
16 ms |
35512 KB |
Output is correct |
25 |
Correct |
15 ms |
35640 KB |
Output is correct |
26 |
Correct |
98 ms |
46024 KB |
Output is correct |
27 |
Correct |
206 ms |
45648 KB |
Output is correct |
28 |
Correct |
18 ms |
35932 KB |
Output is correct |
29 |
Correct |
15 ms |
35676 KB |
Output is correct |
30 |
Correct |
20 ms |
35676 KB |
Output is correct |
31 |
Correct |
208 ms |
45652 KB |
Output is correct |
32 |
Correct |
18 ms |
35932 KB |
Output is correct |
33 |
Correct |
396 ms |
55888 KB |
Output is correct |
34 |
Correct |
206 ms |
45652 KB |
Output is correct |
35 |
Correct |
18 ms |
35932 KB |
Output is correct |
36 |
Correct |
301 ms |
46252 KB |
Output is correct |
37 |
Correct |
19 ms |
35928 KB |
Output is correct |
38 |
Correct |
17 ms |
35804 KB |
Output is correct |
39 |
Correct |
100 ms |
45896 KB |
Output is correct |
40 |
Correct |
17 ms |
36188 KB |
Output is correct |
41 |
Correct |
80 ms |
45516 KB |
Output is correct |
42 |
Correct |
321 ms |
51796 KB |
Output is correct |
43 |
Correct |
16 ms |
35676 KB |
Output is correct |
44 |
Correct |
83 ms |
55504 KB |
Output is correct |
45 |
Correct |
262 ms |
51536 KB |
Output is correct |
46 |
Correct |
17 ms |
35932 KB |
Output is correct |
47 |
Correct |
18 ms |
35928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
46024 KB |
Output is correct |
2 |
Execution timed out |
3079 ms |
49032 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35676 KB |
Output is correct |
2 |
Correct |
15 ms |
35672 KB |
Output is correct |
3 |
Correct |
16 ms |
35676 KB |
Output is correct |
4 |
Correct |
15 ms |
35676 KB |
Output is correct |
5 |
Correct |
16 ms |
35676 KB |
Output is correct |
6 |
Correct |
15 ms |
35672 KB |
Output is correct |
7 |
Correct |
16 ms |
35676 KB |
Output is correct |
8 |
Correct |
16 ms |
35540 KB |
Output is correct |
9 |
Correct |
15 ms |
35544 KB |
Output is correct |
10 |
Correct |
20 ms |
35804 KB |
Output is correct |
11 |
Correct |
16 ms |
35672 KB |
Output is correct |
12 |
Correct |
16 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35676 KB |
Output is correct |
14 |
Correct |
16 ms |
35676 KB |
Output is correct |
15 |
Correct |
15 ms |
35676 KB |
Output is correct |
16 |
Correct |
16 ms |
35676 KB |
Output is correct |
17 |
Correct |
16 ms |
35548 KB |
Output is correct |
18 |
Correct |
16 ms |
35676 KB |
Output is correct |
19 |
Correct |
16 ms |
35716 KB |
Output is correct |
20 |
Correct |
16 ms |
35672 KB |
Output is correct |
21 |
Correct |
16 ms |
35676 KB |
Output is correct |
22 |
Correct |
16 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35588 KB |
Output is correct |
24 |
Correct |
16 ms |
35512 KB |
Output is correct |
25 |
Correct |
15 ms |
35672 KB |
Output is correct |
26 |
Correct |
58 ms |
35932 KB |
Output is correct |
27 |
Correct |
18 ms |
35932 KB |
Output is correct |
28 |
Correct |
82 ms |
36244 KB |
Output is correct |
29 |
Correct |
64 ms |
36088 KB |
Output is correct |
30 |
Correct |
22 ms |
35932 KB |
Output is correct |
31 |
Correct |
14 ms |
35676 KB |
Output is correct |
32 |
Correct |
152 ms |
36380 KB |
Output is correct |
33 |
Correct |
21 ms |
35492 KB |
Output is correct |
34 |
Correct |
24 ms |
35932 KB |
Output is correct |
35 |
Correct |
65 ms |
36092 KB |
Output is correct |
36 |
Correct |
17 ms |
35932 KB |
Output is correct |
37 |
Correct |
52 ms |
35932 KB |
Output is correct |
38 |
Correct |
15 ms |
35672 KB |
Output is correct |
39 |
Correct |
42 ms |
35928 KB |
Output is correct |
40 |
Correct |
17 ms |
35932 KB |
Output is correct |
41 |
Correct |
23 ms |
35944 KB |
Output is correct |
42 |
Correct |
41 ms |
35852 KB |
Output is correct |
43 |
Correct |
16 ms |
36200 KB |
Output is correct |
44 |
Correct |
16 ms |
35596 KB |
Output is correct |
45 |
Correct |
120 ms |
36140 KB |
Output is correct |
46 |
Correct |
48 ms |
36020 KB |
Output is correct |
47 |
Correct |
15 ms |
35516 KB |
Output is correct |
48 |
Correct |
33 ms |
35880 KB |
Output is correct |
49 |
Correct |
68 ms |
36080 KB |
Output is correct |
50 |
Correct |
89 ms |
36260 KB |
Output is correct |
51 |
Correct |
37 ms |
35840 KB |
Output is correct |
52 |
Correct |
18 ms |
36040 KB |
Output is correct |
53 |
Correct |
34 ms |
35932 KB |
Output is correct |
54 |
Correct |
26 ms |
35928 KB |
Output is correct |
55 |
Correct |
15 ms |
35640 KB |
Output is correct |
56 |
Correct |
98 ms |
46024 KB |
Output is correct |
57 |
Correct |
206 ms |
45648 KB |
Output is correct |
58 |
Correct |
18 ms |
35932 KB |
Output is correct |
59 |
Correct |
15 ms |
35676 KB |
Output is correct |
60 |
Correct |
20 ms |
35676 KB |
Output is correct |
61 |
Correct |
208 ms |
45652 KB |
Output is correct |
62 |
Correct |
18 ms |
35932 KB |
Output is correct |
63 |
Correct |
396 ms |
55888 KB |
Output is correct |
64 |
Correct |
206 ms |
45652 KB |
Output is correct |
65 |
Correct |
18 ms |
35932 KB |
Output is correct |
66 |
Correct |
301 ms |
46252 KB |
Output is correct |
67 |
Correct |
19 ms |
35928 KB |
Output is correct |
68 |
Correct |
17 ms |
35804 KB |
Output is correct |
69 |
Correct |
100 ms |
45896 KB |
Output is correct |
70 |
Correct |
17 ms |
36188 KB |
Output is correct |
71 |
Correct |
80 ms |
45516 KB |
Output is correct |
72 |
Correct |
321 ms |
51796 KB |
Output is correct |
73 |
Correct |
16 ms |
35676 KB |
Output is correct |
74 |
Correct |
83 ms |
55504 KB |
Output is correct |
75 |
Correct |
262 ms |
51536 KB |
Output is correct |
76 |
Correct |
17 ms |
35932 KB |
Output is correct |
77 |
Correct |
18 ms |
35928 KB |
Output is correct |
78 |
Correct |
112 ms |
46024 KB |
Output is correct |
79 |
Execution timed out |
3079 ms |
49032 KB |
Time limit exceeded |
80 |
Halted |
0 ms |
0 KB |
- |