#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=5e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
ll P[maxN][21],h[maxN],in[maxN],out[maxN],tg=0;
vector<ll>g[maxN];
void dfs(ll u=1,ll p=1)
{
P[u][0]=p;
in[u]=++tg;
for(int i=1;i<=20;i++) P[u][i]=P[P[u][i-1]][i-1];
for(int v:g[u])
{
if(v!=p)
{
h[v]=h[u]+1;
dfs(v,u);
}
}
}
ll cnt[maxN];
vector<ll>adj[maxN];
void dfs1(ll u=1,ll p=0)
{
for(int v:g[u])
{
if(v!=p)
{
dfs1(v,u);
cnt[u]+=cnt[v];
}
}
}
void dfs2(ll u=1,ll p=0,ll last=-1)
{
if(cnt[u]==0&&last!=-1)
{
adj[u].pb(last);
adj[last].pb(u);
}
if(cnt[u]==0) last=u;
for(int v:g[u])
{
if(v!=p)
{
dfs2(v,u,last);
}
}
}
ll n,k;
vector<ll>vec[maxN];
bool cmp(ll x,ll y)
{
return in[x]<in[y];
}
ll lca(ll u,ll v)
{
if(h[u]<h[v]) swap(u,v);
for(int i=20;i>=0;i--) if(h[u]-(1<<i)>=h[v]) u=P[u][i];
if(u==v) return u;
for(int i=20;i>=0;i--) if(P[u][i]!=P[v][i]) u=P[u][i],v=P[v][i];
return P[u][0];
}
void solve()
{
cin >> n >> k;
for(int i=1;i<n;i++)
{
ll u,v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs();
for(int i=1;i<=n;i++)
{
ll x;
cin >> x;
vec[x].pb(i);
}
for(int i=1;i<=k;i++)
{
sort(vec[i].begin(),vec[i].end(),cmp);
for(int j=0;j<vec[i].size();j++)
{
cnt[vec[i][j]]++;
cnt[lca(vec[i][j],vec[i][(j+1)%vec[i].size()])]--;
}
}
dfs1();
dfs2();
ll ct=0;
for(int i=1;i<=n;i++)
{
if(adj[i].size()==1) ct++;
}
cout <<(ct+1)/2;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
mergers.cpp: In function 'void solve()':
mergers.cpp:92:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int j=0;j<vec[i].size();j++)
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35560 KB |
Output is correct |
3 |
Correct |
18 ms |
35560 KB |
Output is correct |
4 |
Correct |
19 ms |
35524 KB |
Output is correct |
5 |
Correct |
18 ms |
35528 KB |
Output is correct |
6 |
Correct |
18 ms |
35552 KB |
Output is correct |
7 |
Correct |
20 ms |
35560 KB |
Output is correct |
8 |
Correct |
20 ms |
35584 KB |
Output is correct |
9 |
Correct |
19 ms |
35508 KB |
Output is correct |
10 |
Correct |
17 ms |
35472 KB |
Output is correct |
11 |
Correct |
17 ms |
35616 KB |
Output is correct |
12 |
Correct |
19 ms |
35500 KB |
Output is correct |
13 |
Correct |
23 ms |
35480 KB |
Output is correct |
14 |
Correct |
18 ms |
35508 KB |
Output is correct |
15 |
Correct |
19 ms |
35540 KB |
Output is correct |
16 |
Correct |
18 ms |
35540 KB |
Output is correct |
17 |
Correct |
20 ms |
35540 KB |
Output is correct |
18 |
Correct |
18 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35540 KB |
Output is correct |
20 |
Correct |
18 ms |
35556 KB |
Output is correct |
21 |
Correct |
18 ms |
35528 KB |
Output is correct |
22 |
Correct |
18 ms |
35512 KB |
Output is correct |
23 |
Correct |
18 ms |
35540 KB |
Output is correct |
24 |
Correct |
17 ms |
35540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35560 KB |
Output is correct |
3 |
Correct |
18 ms |
35560 KB |
Output is correct |
4 |
Correct |
19 ms |
35524 KB |
Output is correct |
5 |
Correct |
18 ms |
35528 KB |
Output is correct |
6 |
Correct |
18 ms |
35552 KB |
Output is correct |
7 |
Correct |
20 ms |
35560 KB |
Output is correct |
8 |
Correct |
20 ms |
35584 KB |
Output is correct |
9 |
Correct |
19 ms |
35508 KB |
Output is correct |
10 |
Correct |
17 ms |
35472 KB |
Output is correct |
11 |
Correct |
17 ms |
35616 KB |
Output is correct |
12 |
Correct |
19 ms |
35500 KB |
Output is correct |
13 |
Correct |
23 ms |
35480 KB |
Output is correct |
14 |
Correct |
18 ms |
35508 KB |
Output is correct |
15 |
Correct |
19 ms |
35540 KB |
Output is correct |
16 |
Correct |
18 ms |
35540 KB |
Output is correct |
17 |
Correct |
20 ms |
35540 KB |
Output is correct |
18 |
Correct |
18 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35540 KB |
Output is correct |
20 |
Correct |
18 ms |
35556 KB |
Output is correct |
21 |
Correct |
18 ms |
35528 KB |
Output is correct |
22 |
Correct |
18 ms |
35512 KB |
Output is correct |
23 |
Correct |
18 ms |
35540 KB |
Output is correct |
24 |
Correct |
17 ms |
35540 KB |
Output is correct |
25 |
Correct |
17 ms |
35496 KB |
Output is correct |
26 |
Correct |
21 ms |
36392 KB |
Output is correct |
27 |
Correct |
19 ms |
36284 KB |
Output is correct |
28 |
Correct |
21 ms |
36564 KB |
Output is correct |
29 |
Correct |
20 ms |
36460 KB |
Output is correct |
30 |
Correct |
19 ms |
36320 KB |
Output is correct |
31 |
Correct |
18 ms |
35560 KB |
Output is correct |
32 |
Correct |
20 ms |
36408 KB |
Output is correct |
33 |
Correct |
18 ms |
35540 KB |
Output is correct |
34 |
Correct |
21 ms |
36284 KB |
Output is correct |
35 |
Correct |
20 ms |
36460 KB |
Output is correct |
36 |
Correct |
18 ms |
36308 KB |
Output is correct |
37 |
Correct |
20 ms |
36300 KB |
Output is correct |
38 |
Correct |
19 ms |
35548 KB |
Output is correct |
39 |
Correct |
21 ms |
36340 KB |
Output is correct |
40 |
Correct |
21 ms |
36336 KB |
Output is correct |
41 |
Correct |
20 ms |
36304 KB |
Output is correct |
42 |
Correct |
20 ms |
36308 KB |
Output is correct |
43 |
Correct |
19 ms |
36464 KB |
Output is correct |
44 |
Correct |
18 ms |
35540 KB |
Output is correct |
45 |
Correct |
21 ms |
36284 KB |
Output is correct |
46 |
Correct |
20 ms |
36356 KB |
Output is correct |
47 |
Correct |
18 ms |
35556 KB |
Output is correct |
48 |
Correct |
20 ms |
36304 KB |
Output is correct |
49 |
Correct |
19 ms |
36436 KB |
Output is correct |
50 |
Correct |
20 ms |
36564 KB |
Output is correct |
51 |
Correct |
20 ms |
36336 KB |
Output is correct |
52 |
Correct |
21 ms |
36340 KB |
Output is correct |
53 |
Correct |
21 ms |
36324 KB |
Output is correct |
54 |
Correct |
20 ms |
36344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35560 KB |
Output is correct |
3 |
Correct |
18 ms |
35560 KB |
Output is correct |
4 |
Correct |
19 ms |
35524 KB |
Output is correct |
5 |
Correct |
18 ms |
35528 KB |
Output is correct |
6 |
Correct |
18 ms |
35552 KB |
Output is correct |
7 |
Correct |
20 ms |
35560 KB |
Output is correct |
8 |
Correct |
20 ms |
35584 KB |
Output is correct |
9 |
Correct |
19 ms |
35508 KB |
Output is correct |
10 |
Correct |
17 ms |
35472 KB |
Output is correct |
11 |
Correct |
17 ms |
35616 KB |
Output is correct |
12 |
Correct |
19 ms |
35500 KB |
Output is correct |
13 |
Correct |
23 ms |
35480 KB |
Output is correct |
14 |
Correct |
18 ms |
35508 KB |
Output is correct |
15 |
Correct |
19 ms |
35540 KB |
Output is correct |
16 |
Correct |
18 ms |
35540 KB |
Output is correct |
17 |
Correct |
20 ms |
35540 KB |
Output is correct |
18 |
Correct |
18 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35540 KB |
Output is correct |
20 |
Correct |
18 ms |
35556 KB |
Output is correct |
21 |
Correct |
18 ms |
35528 KB |
Output is correct |
22 |
Correct |
18 ms |
35512 KB |
Output is correct |
23 |
Correct |
18 ms |
35540 KB |
Output is correct |
24 |
Correct |
17 ms |
35540 KB |
Output is correct |
25 |
Correct |
19 ms |
35540 KB |
Output is correct |
26 |
Correct |
118 ms |
60832 KB |
Output is correct |
27 |
Correct |
157 ms |
60508 KB |
Output is correct |
28 |
Correct |
21 ms |
36336 KB |
Output is correct |
29 |
Correct |
20 ms |
35496 KB |
Output is correct |
30 |
Correct |
19 ms |
35540 KB |
Output is correct |
31 |
Correct |
133 ms |
60456 KB |
Output is correct |
32 |
Correct |
20 ms |
36308 KB |
Output is correct |
33 |
Correct |
190 ms |
65740 KB |
Output is correct |
34 |
Correct |
152 ms |
60684 KB |
Output is correct |
35 |
Correct |
20 ms |
36308 KB |
Output is correct |
36 |
Correct |
163 ms |
60512 KB |
Output is correct |
37 |
Correct |
21 ms |
36308 KB |
Output is correct |
38 |
Correct |
20 ms |
36352 KB |
Output is correct |
39 |
Correct |
115 ms |
60696 KB |
Output is correct |
40 |
Correct |
21 ms |
36412 KB |
Output is correct |
41 |
Correct |
139 ms |
60308 KB |
Output is correct |
42 |
Correct |
173 ms |
61932 KB |
Output is correct |
43 |
Correct |
18 ms |
35540 KB |
Output is correct |
44 |
Correct |
170 ms |
65724 KB |
Output is correct |
45 |
Correct |
183 ms |
64208 KB |
Output is correct |
46 |
Correct |
20 ms |
36264 KB |
Output is correct |
47 |
Correct |
21 ms |
36316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
60896 KB |
Output is correct |
2 |
Correct |
113 ms |
67000 KB |
Output is correct |
3 |
Correct |
20 ms |
36340 KB |
Output is correct |
4 |
Correct |
19 ms |
36308 KB |
Output is correct |
5 |
Correct |
20 ms |
35684 KB |
Output is correct |
6 |
Correct |
18 ms |
35524 KB |
Output is correct |
7 |
Correct |
19 ms |
36296 KB |
Output is correct |
8 |
Correct |
159 ms |
62048 KB |
Output is correct |
9 |
Correct |
22 ms |
36308 KB |
Output is correct |
10 |
Correct |
132 ms |
60772 KB |
Output is correct |
11 |
Correct |
18 ms |
35580 KB |
Output is correct |
12 |
Correct |
153 ms |
60352 KB |
Output is correct |
13 |
Correct |
147 ms |
62268 KB |
Output is correct |
14 |
Correct |
142 ms |
66660 KB |
Output is correct |
15 |
Correct |
115 ms |
60728 KB |
Output is correct |
16 |
Correct |
21 ms |
36340 KB |
Output is correct |
17 |
Correct |
19 ms |
35556 KB |
Output is correct |
18 |
Correct |
121 ms |
66136 KB |
Output is correct |
19 |
Correct |
171 ms |
68696 KB |
Output is correct |
20 |
Correct |
21 ms |
36328 KB |
Output is correct |
21 |
Correct |
18 ms |
35604 KB |
Output is correct |
22 |
Correct |
116 ms |
62400 KB |
Output is correct |
23 |
Correct |
20 ms |
36308 KB |
Output is correct |
24 |
Correct |
147 ms |
61196 KB |
Output is correct |
25 |
Correct |
172 ms |
69160 KB |
Output is correct |
26 |
Correct |
20 ms |
36436 KB |
Output is correct |
27 |
Correct |
21 ms |
36564 KB |
Output is correct |
28 |
Correct |
19 ms |
36308 KB |
Output is correct |
29 |
Correct |
21 ms |
36272 KB |
Output is correct |
30 |
Correct |
20 ms |
36320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35560 KB |
Output is correct |
3 |
Correct |
18 ms |
35560 KB |
Output is correct |
4 |
Correct |
19 ms |
35524 KB |
Output is correct |
5 |
Correct |
18 ms |
35528 KB |
Output is correct |
6 |
Correct |
18 ms |
35552 KB |
Output is correct |
7 |
Correct |
20 ms |
35560 KB |
Output is correct |
8 |
Correct |
20 ms |
35584 KB |
Output is correct |
9 |
Correct |
19 ms |
35508 KB |
Output is correct |
10 |
Correct |
17 ms |
35472 KB |
Output is correct |
11 |
Correct |
17 ms |
35616 KB |
Output is correct |
12 |
Correct |
19 ms |
35500 KB |
Output is correct |
13 |
Correct |
23 ms |
35480 KB |
Output is correct |
14 |
Correct |
18 ms |
35508 KB |
Output is correct |
15 |
Correct |
19 ms |
35540 KB |
Output is correct |
16 |
Correct |
18 ms |
35540 KB |
Output is correct |
17 |
Correct |
20 ms |
35540 KB |
Output is correct |
18 |
Correct |
18 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35540 KB |
Output is correct |
20 |
Correct |
18 ms |
35556 KB |
Output is correct |
21 |
Correct |
18 ms |
35528 KB |
Output is correct |
22 |
Correct |
18 ms |
35512 KB |
Output is correct |
23 |
Correct |
18 ms |
35540 KB |
Output is correct |
24 |
Correct |
17 ms |
35540 KB |
Output is correct |
25 |
Correct |
17 ms |
35496 KB |
Output is correct |
26 |
Correct |
21 ms |
36392 KB |
Output is correct |
27 |
Correct |
19 ms |
36284 KB |
Output is correct |
28 |
Correct |
21 ms |
36564 KB |
Output is correct |
29 |
Correct |
20 ms |
36460 KB |
Output is correct |
30 |
Correct |
19 ms |
36320 KB |
Output is correct |
31 |
Correct |
18 ms |
35560 KB |
Output is correct |
32 |
Correct |
20 ms |
36408 KB |
Output is correct |
33 |
Correct |
18 ms |
35540 KB |
Output is correct |
34 |
Correct |
21 ms |
36284 KB |
Output is correct |
35 |
Correct |
20 ms |
36460 KB |
Output is correct |
36 |
Correct |
18 ms |
36308 KB |
Output is correct |
37 |
Correct |
20 ms |
36300 KB |
Output is correct |
38 |
Correct |
19 ms |
35548 KB |
Output is correct |
39 |
Correct |
21 ms |
36340 KB |
Output is correct |
40 |
Correct |
21 ms |
36336 KB |
Output is correct |
41 |
Correct |
20 ms |
36304 KB |
Output is correct |
42 |
Correct |
20 ms |
36308 KB |
Output is correct |
43 |
Correct |
19 ms |
36464 KB |
Output is correct |
44 |
Correct |
18 ms |
35540 KB |
Output is correct |
45 |
Correct |
21 ms |
36284 KB |
Output is correct |
46 |
Correct |
20 ms |
36356 KB |
Output is correct |
47 |
Correct |
18 ms |
35556 KB |
Output is correct |
48 |
Correct |
20 ms |
36304 KB |
Output is correct |
49 |
Correct |
19 ms |
36436 KB |
Output is correct |
50 |
Correct |
20 ms |
36564 KB |
Output is correct |
51 |
Correct |
20 ms |
36336 KB |
Output is correct |
52 |
Correct |
21 ms |
36340 KB |
Output is correct |
53 |
Correct |
21 ms |
36324 KB |
Output is correct |
54 |
Correct |
20 ms |
36344 KB |
Output is correct |
55 |
Correct |
19 ms |
35540 KB |
Output is correct |
56 |
Correct |
118 ms |
60832 KB |
Output is correct |
57 |
Correct |
157 ms |
60508 KB |
Output is correct |
58 |
Correct |
21 ms |
36336 KB |
Output is correct |
59 |
Correct |
20 ms |
35496 KB |
Output is correct |
60 |
Correct |
19 ms |
35540 KB |
Output is correct |
61 |
Correct |
133 ms |
60456 KB |
Output is correct |
62 |
Correct |
20 ms |
36308 KB |
Output is correct |
63 |
Correct |
190 ms |
65740 KB |
Output is correct |
64 |
Correct |
152 ms |
60684 KB |
Output is correct |
65 |
Correct |
20 ms |
36308 KB |
Output is correct |
66 |
Correct |
163 ms |
60512 KB |
Output is correct |
67 |
Correct |
21 ms |
36308 KB |
Output is correct |
68 |
Correct |
20 ms |
36352 KB |
Output is correct |
69 |
Correct |
115 ms |
60696 KB |
Output is correct |
70 |
Correct |
21 ms |
36412 KB |
Output is correct |
71 |
Correct |
139 ms |
60308 KB |
Output is correct |
72 |
Correct |
173 ms |
61932 KB |
Output is correct |
73 |
Correct |
18 ms |
35540 KB |
Output is correct |
74 |
Correct |
170 ms |
65724 KB |
Output is correct |
75 |
Correct |
183 ms |
64208 KB |
Output is correct |
76 |
Correct |
20 ms |
36264 KB |
Output is correct |
77 |
Correct |
21 ms |
36316 KB |
Output is correct |
78 |
Correct |
113 ms |
60896 KB |
Output is correct |
79 |
Correct |
113 ms |
67000 KB |
Output is correct |
80 |
Correct |
20 ms |
36340 KB |
Output is correct |
81 |
Correct |
19 ms |
36308 KB |
Output is correct |
82 |
Correct |
20 ms |
35684 KB |
Output is correct |
83 |
Correct |
18 ms |
35524 KB |
Output is correct |
84 |
Correct |
19 ms |
36296 KB |
Output is correct |
85 |
Correct |
159 ms |
62048 KB |
Output is correct |
86 |
Correct |
22 ms |
36308 KB |
Output is correct |
87 |
Correct |
132 ms |
60772 KB |
Output is correct |
88 |
Correct |
18 ms |
35580 KB |
Output is correct |
89 |
Correct |
153 ms |
60352 KB |
Output is correct |
90 |
Correct |
147 ms |
62268 KB |
Output is correct |
91 |
Correct |
142 ms |
66660 KB |
Output is correct |
92 |
Correct |
115 ms |
60728 KB |
Output is correct |
93 |
Correct |
21 ms |
36340 KB |
Output is correct |
94 |
Correct |
19 ms |
35556 KB |
Output is correct |
95 |
Correct |
121 ms |
66136 KB |
Output is correct |
96 |
Correct |
171 ms |
68696 KB |
Output is correct |
97 |
Correct |
21 ms |
36328 KB |
Output is correct |
98 |
Correct |
18 ms |
35604 KB |
Output is correct |
99 |
Correct |
116 ms |
62400 KB |
Output is correct |
100 |
Correct |
20 ms |
36308 KB |
Output is correct |
101 |
Correct |
147 ms |
61196 KB |
Output is correct |
102 |
Correct |
172 ms |
69160 KB |
Output is correct |
103 |
Correct |
20 ms |
36436 KB |
Output is correct |
104 |
Correct |
21 ms |
36564 KB |
Output is correct |
105 |
Correct |
19 ms |
36308 KB |
Output is correct |
106 |
Correct |
21 ms |
36272 KB |
Output is correct |
107 |
Correct |
20 ms |
36320 KB |
Output is correct |
108 |
Correct |
686 ms |
168708 KB |
Output is correct |
109 |
Correct |
1179 ms |
171696 KB |
Output is correct |
110 |
Correct |
1090 ms |
180540 KB |
Output is correct |
111 |
Correct |
1250 ms |
203944 KB |
Output is correct |
112 |
Correct |
1162 ms |
196448 KB |
Output is correct |
113 |
Correct |
745 ms |
189012 KB |
Output is correct |
114 |
Correct |
865 ms |
159988 KB |
Output is correct |
115 |
Correct |
876 ms |
159732 KB |
Output is correct |
116 |
Correct |
1042 ms |
165280 KB |
Output is correct |
117 |
Correct |
1022 ms |
192496 KB |
Output is correct |
118 |
Correct |
838 ms |
160648 KB |
Output is correct |
119 |
Correct |
1020 ms |
192504 KB |
Output is correct |
120 |
Correct |
1228 ms |
206200 KB |
Output is correct |
121 |
Correct |
1087 ms |
192460 KB |
Output is correct |
122 |
Correct |
1006 ms |
171616 KB |
Output is correct |
123 |
Correct |
737 ms |
194664 KB |
Output is correct |
124 |
Correct |
1121 ms |
178412 KB |
Output is correct |