#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
//#pragma GCC optimize("O2,unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,sse,sse2,sse3,ssse3,sse4,popcnt")
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=5e5+5;
const ll offset=1e18;
const ll block_sz=317;
const ll inf=1e18;
const ll mod=1e9+7;
int n,k,in[maxn],par[maxn],up[maxn],Time,deg[maxn];
vector<int> adj[maxn],L[maxn];
set<pii> S;
set<pii>::iterator x;
//bool vis[maxn];
int Find(int u)
{
return par[u]<0?u:par[u]=Find(par[u]);
}
bool Union(int u,int v)
{
if ((u=Find(u))==(v=Find(v)))
{
return 0;
}
//if (par[u]>par[v]) swap(u,v);
par[u]+=par[v];
par[v]=u;
return 1;
}
void dfs(int u,int pre)
{
up[u]=pre;
in[u]=in[pre]+1;
for(int v:adj[u])
{
if (v==pre) continue;
dfs(v,u);
}
}
void sol()
{
cin >> n>> k;
for1(i,1,n-1)
{
int u,v;cin >>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1,1);
for1(i,1,n)
{
par[i]=-1;
int g;cin >> g;L[g].pb(i);
}
for1(i,1,k)
{
S.clear();
for (int v:L[i])
{
S.insert({in[Find(v)],Find(v)});
}
while(S.size()>1)
{
x=(--S.end());
S.erase(x);
Union(up[x->se],x-> se);
S.insert({in[Find(x->se)],Find(x->se)});
}
}
// for1(i,1,n) cout << par[i]<<' ';
// cout << '\n';
int cnt=0;
for1(u,1,n)
{
int x=0;
for(int v: adj[u])
{
if (Find(u)!=Find(v))
{
//if (u==3) cout << v<<'\n';
// x++;
deg[Find(u)]++;
}
}
}
for1(i,1,n) if (deg[i]==1) cnt++;
cout << (cnt+1)/2;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("GROUPDIV.inp","r",stdin);
// freopen("GROUPDIV.out","w",stdout);
int t=1;//cin >> t;
while (t--)
{
sol();
}
}
/*
3 1
12345678
?11
*/
Compilation message
mergers.cpp: In function 'void sol()':
mergers.cpp:90:13: warning: unused variable 'x' [-Wunused-variable]
90 | int x=0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31324 KB |
Output is correct |
2 |
Correct |
7 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31348 KB |
Output is correct |
5 |
Correct |
6 ms |
31324 KB |
Output is correct |
6 |
Correct |
6 ms |
31324 KB |
Output is correct |
7 |
Correct |
7 ms |
31320 KB |
Output is correct |
8 |
Correct |
6 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
31412 KB |
Output is correct |
10 |
Correct |
6 ms |
31140 KB |
Output is correct |
11 |
Correct |
6 ms |
31320 KB |
Output is correct |
12 |
Correct |
6 ms |
31324 KB |
Output is correct |
13 |
Correct |
6 ms |
31324 KB |
Output is correct |
14 |
Correct |
6 ms |
31324 KB |
Output is correct |
15 |
Correct |
6 ms |
31328 KB |
Output is correct |
16 |
Correct |
6 ms |
31404 KB |
Output is correct |
17 |
Correct |
6 ms |
31412 KB |
Output is correct |
18 |
Correct |
6 ms |
31324 KB |
Output is correct |
19 |
Correct |
6 ms |
31324 KB |
Output is correct |
20 |
Correct |
6 ms |
31324 KB |
Output is correct |
21 |
Correct |
7 ms |
31404 KB |
Output is correct |
22 |
Correct |
6 ms |
31408 KB |
Output is correct |
23 |
Correct |
6 ms |
31520 KB |
Output is correct |
24 |
Correct |
7 ms |
31404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31324 KB |
Output is correct |
2 |
Correct |
7 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31348 KB |
Output is correct |
5 |
Correct |
6 ms |
31324 KB |
Output is correct |
6 |
Correct |
6 ms |
31324 KB |
Output is correct |
7 |
Correct |
7 ms |
31320 KB |
Output is correct |
8 |
Correct |
6 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
31412 KB |
Output is correct |
10 |
Correct |
6 ms |
31140 KB |
Output is correct |
11 |
Correct |
6 ms |
31320 KB |
Output is correct |
12 |
Correct |
6 ms |
31324 KB |
Output is correct |
13 |
Correct |
6 ms |
31324 KB |
Output is correct |
14 |
Correct |
6 ms |
31324 KB |
Output is correct |
15 |
Correct |
6 ms |
31328 KB |
Output is correct |
16 |
Correct |
6 ms |
31404 KB |
Output is correct |
17 |
Correct |
6 ms |
31412 KB |
Output is correct |
18 |
Correct |
6 ms |
31324 KB |
Output is correct |
19 |
Correct |
6 ms |
31324 KB |
Output is correct |
20 |
Correct |
6 ms |
31324 KB |
Output is correct |
21 |
Correct |
7 ms |
31404 KB |
Output is correct |
22 |
Correct |
6 ms |
31408 KB |
Output is correct |
23 |
Correct |
6 ms |
31520 KB |
Output is correct |
24 |
Correct |
7 ms |
31404 KB |
Output is correct |
25 |
Correct |
6 ms |
31172 KB |
Output is correct |
26 |
Correct |
8 ms |
31580 KB |
Output is correct |
27 |
Correct |
8 ms |
31508 KB |
Output is correct |
28 |
Correct |
8 ms |
31580 KB |
Output is correct |
29 |
Correct |
8 ms |
31832 KB |
Output is correct |
30 |
Correct |
8 ms |
31408 KB |
Output is correct |
31 |
Correct |
6 ms |
31324 KB |
Output is correct |
32 |
Correct |
7 ms |
31668 KB |
Output is correct |
33 |
Correct |
6 ms |
31324 KB |
Output is correct |
34 |
Correct |
7 ms |
31580 KB |
Output is correct |
35 |
Correct |
7 ms |
31580 KB |
Output is correct |
36 |
Correct |
8 ms |
31580 KB |
Output is correct |
37 |
Correct |
8 ms |
31580 KB |
Output is correct |
38 |
Correct |
6 ms |
31324 KB |
Output is correct |
39 |
Correct |
9 ms |
31416 KB |
Output is correct |
40 |
Correct |
8 ms |
31580 KB |
Output is correct |
41 |
Correct |
8 ms |
31640 KB |
Output is correct |
42 |
Correct |
7 ms |
31580 KB |
Output is correct |
43 |
Correct |
8 ms |
31896 KB |
Output is correct |
44 |
Correct |
7 ms |
31324 KB |
Output is correct |
45 |
Correct |
9 ms |
31580 KB |
Output is correct |
46 |
Correct |
8 ms |
31580 KB |
Output is correct |
47 |
Correct |
7 ms |
31324 KB |
Output is correct |
48 |
Correct |
8 ms |
31420 KB |
Output is correct |
49 |
Correct |
7 ms |
31580 KB |
Output is correct |
50 |
Correct |
7 ms |
31580 KB |
Output is correct |
51 |
Correct |
7 ms |
31580 KB |
Output is correct |
52 |
Correct |
8 ms |
31440 KB |
Output is correct |
53 |
Correct |
7 ms |
31580 KB |
Output is correct |
54 |
Correct |
7 ms |
31832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31324 KB |
Output is correct |
2 |
Correct |
7 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31348 KB |
Output is correct |
5 |
Correct |
6 ms |
31324 KB |
Output is correct |
6 |
Correct |
6 ms |
31324 KB |
Output is correct |
7 |
Correct |
7 ms |
31320 KB |
Output is correct |
8 |
Correct |
6 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
31412 KB |
Output is correct |
10 |
Correct |
6 ms |
31140 KB |
Output is correct |
11 |
Correct |
6 ms |
31320 KB |
Output is correct |
12 |
Correct |
6 ms |
31324 KB |
Output is correct |
13 |
Correct |
6 ms |
31324 KB |
Output is correct |
14 |
Correct |
6 ms |
31324 KB |
Output is correct |
15 |
Correct |
6 ms |
31328 KB |
Output is correct |
16 |
Correct |
6 ms |
31404 KB |
Output is correct |
17 |
Correct |
6 ms |
31412 KB |
Output is correct |
18 |
Correct |
6 ms |
31324 KB |
Output is correct |
19 |
Correct |
6 ms |
31324 KB |
Output is correct |
20 |
Correct |
6 ms |
31324 KB |
Output is correct |
21 |
Correct |
7 ms |
31404 KB |
Output is correct |
22 |
Correct |
6 ms |
31408 KB |
Output is correct |
23 |
Correct |
6 ms |
31520 KB |
Output is correct |
24 |
Correct |
7 ms |
31404 KB |
Output is correct |
25 |
Correct |
6 ms |
31320 KB |
Output is correct |
26 |
Correct |
42 ms |
37788 KB |
Output is correct |
27 |
Correct |
58 ms |
37456 KB |
Output is correct |
28 |
Correct |
7 ms |
31600 KB |
Output is correct |
29 |
Correct |
7 ms |
31332 KB |
Output is correct |
30 |
Correct |
6 ms |
31324 KB |
Output is correct |
31 |
Correct |
61 ms |
37548 KB |
Output is correct |
32 |
Correct |
7 ms |
31580 KB |
Output is correct |
33 |
Correct |
50 ms |
41300 KB |
Output is correct |
34 |
Correct |
61 ms |
37644 KB |
Output is correct |
35 |
Correct |
8 ms |
31580 KB |
Output is correct |
36 |
Correct |
65 ms |
37664 KB |
Output is correct |
37 |
Correct |
8 ms |
31632 KB |
Output is correct |
38 |
Correct |
7 ms |
31584 KB |
Output is correct |
39 |
Correct |
45 ms |
37708 KB |
Output is correct |
40 |
Correct |
9 ms |
31668 KB |
Output is correct |
41 |
Correct |
84 ms |
43492 KB |
Output is correct |
42 |
Correct |
49 ms |
38228 KB |
Output is correct |
43 |
Correct |
6 ms |
31324 KB |
Output is correct |
44 |
Correct |
76 ms |
47416 KB |
Output is correct |
45 |
Correct |
61 ms |
40532 KB |
Output is correct |
46 |
Correct |
7 ms |
31580 KB |
Output is correct |
47 |
Correct |
7 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
36368 KB |
Output is correct |
2 |
Correct |
55 ms |
38352 KB |
Output is correct |
3 |
Correct |
7 ms |
31580 KB |
Output is correct |
4 |
Correct |
7 ms |
31512 KB |
Output is correct |
5 |
Correct |
6 ms |
31320 KB |
Output is correct |
6 |
Correct |
8 ms |
31320 KB |
Output is correct |
7 |
Correct |
7 ms |
31576 KB |
Output is correct |
8 |
Correct |
61 ms |
38636 KB |
Output is correct |
9 |
Correct |
7 ms |
31580 KB |
Output is correct |
10 |
Correct |
59 ms |
37716 KB |
Output is correct |
11 |
Correct |
6 ms |
31320 KB |
Output is correct |
12 |
Correct |
67 ms |
37260 KB |
Output is correct |
13 |
Correct |
65 ms |
38568 KB |
Output is correct |
14 |
Correct |
53 ms |
39764 KB |
Output is correct |
15 |
Correct |
45 ms |
37700 KB |
Output is correct |
16 |
Correct |
8 ms |
31832 KB |
Output is correct |
17 |
Correct |
6 ms |
31368 KB |
Output is correct |
18 |
Correct |
59 ms |
39680 KB |
Output is correct |
19 |
Correct |
56 ms |
41572 KB |
Output is correct |
20 |
Correct |
7 ms |
31580 KB |
Output is correct |
21 |
Correct |
6 ms |
31324 KB |
Output is correct |
22 |
Correct |
47 ms |
38676 KB |
Output is correct |
23 |
Correct |
8 ms |
31580 KB |
Output is correct |
24 |
Correct |
57 ms |
37980 KB |
Output is correct |
25 |
Correct |
62 ms |
41808 KB |
Output is correct |
26 |
Correct |
8 ms |
31580 KB |
Output is correct |
27 |
Correct |
7 ms |
31580 KB |
Output is correct |
28 |
Correct |
7 ms |
31580 KB |
Output is correct |
29 |
Correct |
7 ms |
31576 KB |
Output is correct |
30 |
Correct |
8 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31324 KB |
Output is correct |
2 |
Correct |
7 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31348 KB |
Output is correct |
5 |
Correct |
6 ms |
31324 KB |
Output is correct |
6 |
Correct |
6 ms |
31324 KB |
Output is correct |
7 |
Correct |
7 ms |
31320 KB |
Output is correct |
8 |
Correct |
6 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
31412 KB |
Output is correct |
10 |
Correct |
6 ms |
31140 KB |
Output is correct |
11 |
Correct |
6 ms |
31320 KB |
Output is correct |
12 |
Correct |
6 ms |
31324 KB |
Output is correct |
13 |
Correct |
6 ms |
31324 KB |
Output is correct |
14 |
Correct |
6 ms |
31324 KB |
Output is correct |
15 |
Correct |
6 ms |
31328 KB |
Output is correct |
16 |
Correct |
6 ms |
31404 KB |
Output is correct |
17 |
Correct |
6 ms |
31412 KB |
Output is correct |
18 |
Correct |
6 ms |
31324 KB |
Output is correct |
19 |
Correct |
6 ms |
31324 KB |
Output is correct |
20 |
Correct |
6 ms |
31324 KB |
Output is correct |
21 |
Correct |
7 ms |
31404 KB |
Output is correct |
22 |
Correct |
6 ms |
31408 KB |
Output is correct |
23 |
Correct |
6 ms |
31520 KB |
Output is correct |
24 |
Correct |
7 ms |
31404 KB |
Output is correct |
25 |
Correct |
6 ms |
31172 KB |
Output is correct |
26 |
Correct |
8 ms |
31580 KB |
Output is correct |
27 |
Correct |
8 ms |
31508 KB |
Output is correct |
28 |
Correct |
8 ms |
31580 KB |
Output is correct |
29 |
Correct |
8 ms |
31832 KB |
Output is correct |
30 |
Correct |
8 ms |
31408 KB |
Output is correct |
31 |
Correct |
6 ms |
31324 KB |
Output is correct |
32 |
Correct |
7 ms |
31668 KB |
Output is correct |
33 |
Correct |
6 ms |
31324 KB |
Output is correct |
34 |
Correct |
7 ms |
31580 KB |
Output is correct |
35 |
Correct |
7 ms |
31580 KB |
Output is correct |
36 |
Correct |
8 ms |
31580 KB |
Output is correct |
37 |
Correct |
8 ms |
31580 KB |
Output is correct |
38 |
Correct |
6 ms |
31324 KB |
Output is correct |
39 |
Correct |
9 ms |
31416 KB |
Output is correct |
40 |
Correct |
8 ms |
31580 KB |
Output is correct |
41 |
Correct |
8 ms |
31640 KB |
Output is correct |
42 |
Correct |
7 ms |
31580 KB |
Output is correct |
43 |
Correct |
8 ms |
31896 KB |
Output is correct |
44 |
Correct |
7 ms |
31324 KB |
Output is correct |
45 |
Correct |
9 ms |
31580 KB |
Output is correct |
46 |
Correct |
8 ms |
31580 KB |
Output is correct |
47 |
Correct |
7 ms |
31324 KB |
Output is correct |
48 |
Correct |
8 ms |
31420 KB |
Output is correct |
49 |
Correct |
7 ms |
31580 KB |
Output is correct |
50 |
Correct |
7 ms |
31580 KB |
Output is correct |
51 |
Correct |
7 ms |
31580 KB |
Output is correct |
52 |
Correct |
8 ms |
31440 KB |
Output is correct |
53 |
Correct |
7 ms |
31580 KB |
Output is correct |
54 |
Correct |
7 ms |
31832 KB |
Output is correct |
55 |
Correct |
6 ms |
31320 KB |
Output is correct |
56 |
Correct |
42 ms |
37788 KB |
Output is correct |
57 |
Correct |
58 ms |
37456 KB |
Output is correct |
58 |
Correct |
7 ms |
31600 KB |
Output is correct |
59 |
Correct |
7 ms |
31332 KB |
Output is correct |
60 |
Correct |
6 ms |
31324 KB |
Output is correct |
61 |
Correct |
61 ms |
37548 KB |
Output is correct |
62 |
Correct |
7 ms |
31580 KB |
Output is correct |
63 |
Correct |
50 ms |
41300 KB |
Output is correct |
64 |
Correct |
61 ms |
37644 KB |
Output is correct |
65 |
Correct |
8 ms |
31580 KB |
Output is correct |
66 |
Correct |
65 ms |
37664 KB |
Output is correct |
67 |
Correct |
8 ms |
31632 KB |
Output is correct |
68 |
Correct |
7 ms |
31584 KB |
Output is correct |
69 |
Correct |
45 ms |
37708 KB |
Output is correct |
70 |
Correct |
9 ms |
31668 KB |
Output is correct |
71 |
Correct |
84 ms |
43492 KB |
Output is correct |
72 |
Correct |
49 ms |
38228 KB |
Output is correct |
73 |
Correct |
6 ms |
31324 KB |
Output is correct |
74 |
Correct |
76 ms |
47416 KB |
Output is correct |
75 |
Correct |
61 ms |
40532 KB |
Output is correct |
76 |
Correct |
7 ms |
31580 KB |
Output is correct |
77 |
Correct |
7 ms |
31580 KB |
Output is correct |
78 |
Correct |
41 ms |
36368 KB |
Output is correct |
79 |
Correct |
55 ms |
38352 KB |
Output is correct |
80 |
Correct |
7 ms |
31580 KB |
Output is correct |
81 |
Correct |
7 ms |
31512 KB |
Output is correct |
82 |
Correct |
6 ms |
31320 KB |
Output is correct |
83 |
Correct |
8 ms |
31320 KB |
Output is correct |
84 |
Correct |
7 ms |
31576 KB |
Output is correct |
85 |
Correct |
61 ms |
38636 KB |
Output is correct |
86 |
Correct |
7 ms |
31580 KB |
Output is correct |
87 |
Correct |
59 ms |
37716 KB |
Output is correct |
88 |
Correct |
6 ms |
31320 KB |
Output is correct |
89 |
Correct |
67 ms |
37260 KB |
Output is correct |
90 |
Correct |
65 ms |
38568 KB |
Output is correct |
91 |
Correct |
53 ms |
39764 KB |
Output is correct |
92 |
Correct |
45 ms |
37700 KB |
Output is correct |
93 |
Correct |
8 ms |
31832 KB |
Output is correct |
94 |
Correct |
6 ms |
31368 KB |
Output is correct |
95 |
Correct |
59 ms |
39680 KB |
Output is correct |
96 |
Correct |
56 ms |
41572 KB |
Output is correct |
97 |
Correct |
7 ms |
31580 KB |
Output is correct |
98 |
Correct |
6 ms |
31324 KB |
Output is correct |
99 |
Correct |
47 ms |
38676 KB |
Output is correct |
100 |
Correct |
8 ms |
31580 KB |
Output is correct |
101 |
Correct |
57 ms |
37980 KB |
Output is correct |
102 |
Correct |
62 ms |
41808 KB |
Output is correct |
103 |
Correct |
8 ms |
31580 KB |
Output is correct |
104 |
Correct |
7 ms |
31580 KB |
Output is correct |
105 |
Correct |
7 ms |
31580 KB |
Output is correct |
106 |
Correct |
7 ms |
31576 KB |
Output is correct |
107 |
Correct |
8 ms |
31580 KB |
Output is correct |
108 |
Correct |
332 ms |
77716 KB |
Output is correct |
109 |
Correct |
649 ms |
91328 KB |
Output is correct |
110 |
Correct |
723 ms |
114032 KB |
Output is correct |
111 |
Correct |
505 ms |
94844 KB |
Output is correct |
112 |
Correct |
571 ms |
93008 KB |
Output is correct |
113 |
Correct |
344 ms |
83268 KB |
Output is correct |
114 |
Correct |
580 ms |
85600 KB |
Output is correct |
115 |
Correct |
659 ms |
99088 KB |
Output is correct |
116 |
Correct |
413 ms |
75088 KB |
Output is correct |
117 |
Correct |
528 ms |
83928 KB |
Output is correct |
118 |
Correct |
413 ms |
72480 KB |
Output is correct |
119 |
Correct |
501 ms |
83796 KB |
Output is correct |
120 |
Correct |
537 ms |
93580 KB |
Output is correct |
121 |
Correct |
524 ms |
84048 KB |
Output is correct |
122 |
Correct |
521 ms |
78280 KB |
Output is correct |
123 |
Correct |
365 ms |
84916 KB |
Output is correct |
124 |
Correct |
576 ms |
80176 KB |
Output is correct |