#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=500001;
const int LOG=20;
int n,k;
int s[N];
VI e[N];
int up[LOG][N],dep[N],node[N],bad[N];
VI t[N];
void dfs(int u,int par=0) {
dep[u]=dep[par]+1;
up[0][u]=par;
for (auto v:e[u]) {
if (v != par) dfs(v, u);
}
}
int lca(int a,int b) {
if (dep[a]<dep[b]) swap(a,b);
per(i,0,LOG) {
if (dep[a]-(1<<i)>=dep[b]) a=up[i][a];
}
if (a==b) return a;
per(i,0,LOG) if (up[i][a]!=up[i][b]) a=up[i][a],b=up[i][b];
return up[0][a];
}
int dfs2(int u,int par=0) {
int mnlvl=dep[node[s[u]]];
for (auto v:e[u]) {
if (v!=par) mnlvl=min(mnlvl,dfs2(v,u));
}
if(mnlvl>=dep[u]) bad[u]=par;
return mnlvl;
}
int x=0;
int id[N];
void dfs3(int u,int par=0) {
if (bad[u]==par) {
id[u]=++x;
} else {
id[u]=id[par];
}
for (auto v:e[u]) {
if (v != par) dfs3(v, u);
}
}
void dfs4(int u,int par=0) {
for (auto v:e[u]) {
if (v!=par) {
if (id[u]!=id[v]) {
t[id[u]].push_back(id[v]);
t[id[v]].push_back(id[u]);
}
dfs4(v,u);
}
}
}
int main() {
scanf("%d%d",&n,&k);
rep(i,1,n) {
int u,v;
scanf("%d%d",&u,&v);
e[u].pb(v);
e[v].pb(u);
}
rep(i,1,n+1) scanf("%d",&s[i]);
dfs(1);
rep(i,1,LOG) rep(j,1,n+1) up[i][j]=up[i-1][up[i-1][j]];
rep(i,1,n+1) {
if (node[s[i]]==0) node[s[i]]=i;
else node[s[i]]=lca(node[s[i]],i);
}
dfs2(1);
dfs3(1);
dfs4(1);
int leaves=0;
rep(i,1,x+1) {
sort(t[i].begin(),t[i].end());
t[i].resize(unique(t[i].begin(),t[i].end())-t[i].begin());
if (t[i].size()==1) leaves++;
}
printf("%d\n",(leaves+1)/2);
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
mergers.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
mergers.cpp:93:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | rep(i,1,n+1) scanf("%d",&s[i]);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
67932 KB |
Output is correct |
2 |
Correct |
10 ms |
68188 KB |
Output is correct |
3 |
Correct |
10 ms |
68188 KB |
Output is correct |
4 |
Correct |
10 ms |
68132 KB |
Output is correct |
5 |
Correct |
10 ms |
68072 KB |
Output is correct |
6 |
Correct |
10 ms |
68184 KB |
Output is correct |
7 |
Correct |
10 ms |
67932 KB |
Output is correct |
8 |
Correct |
10 ms |
68188 KB |
Output is correct |
9 |
Correct |
10 ms |
67932 KB |
Output is correct |
10 |
Correct |
10 ms |
68064 KB |
Output is correct |
11 |
Correct |
10 ms |
68188 KB |
Output is correct |
12 |
Correct |
10 ms |
68192 KB |
Output is correct |
13 |
Correct |
10 ms |
68184 KB |
Output is correct |
14 |
Correct |
10 ms |
68188 KB |
Output is correct |
15 |
Correct |
10 ms |
68156 KB |
Output is correct |
16 |
Correct |
10 ms |
68184 KB |
Output is correct |
17 |
Correct |
10 ms |
67928 KB |
Output is correct |
18 |
Correct |
10 ms |
68188 KB |
Output is correct |
19 |
Correct |
10 ms |
67932 KB |
Output is correct |
20 |
Correct |
10 ms |
67928 KB |
Output is correct |
21 |
Correct |
10 ms |
68188 KB |
Output is correct |
22 |
Correct |
11 ms |
68076 KB |
Output is correct |
23 |
Correct |
10 ms |
68032 KB |
Output is correct |
24 |
Correct |
10 ms |
67928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
67932 KB |
Output is correct |
2 |
Correct |
10 ms |
68188 KB |
Output is correct |
3 |
Correct |
10 ms |
68188 KB |
Output is correct |
4 |
Correct |
10 ms |
68132 KB |
Output is correct |
5 |
Correct |
10 ms |
68072 KB |
Output is correct |
6 |
Correct |
10 ms |
68184 KB |
Output is correct |
7 |
Correct |
10 ms |
67932 KB |
Output is correct |
8 |
Correct |
10 ms |
68188 KB |
Output is correct |
9 |
Correct |
10 ms |
67932 KB |
Output is correct |
10 |
Correct |
10 ms |
68064 KB |
Output is correct |
11 |
Correct |
10 ms |
68188 KB |
Output is correct |
12 |
Correct |
10 ms |
68192 KB |
Output is correct |
13 |
Correct |
10 ms |
68184 KB |
Output is correct |
14 |
Correct |
10 ms |
68188 KB |
Output is correct |
15 |
Correct |
10 ms |
68156 KB |
Output is correct |
16 |
Correct |
10 ms |
68184 KB |
Output is correct |
17 |
Correct |
10 ms |
67928 KB |
Output is correct |
18 |
Correct |
10 ms |
68188 KB |
Output is correct |
19 |
Correct |
10 ms |
67932 KB |
Output is correct |
20 |
Correct |
10 ms |
67928 KB |
Output is correct |
21 |
Correct |
10 ms |
68188 KB |
Output is correct |
22 |
Correct |
11 ms |
68076 KB |
Output is correct |
23 |
Correct |
10 ms |
68032 KB |
Output is correct |
24 |
Correct |
10 ms |
67928 KB |
Output is correct |
25 |
Correct |
10 ms |
68188 KB |
Output is correct |
26 |
Correct |
11 ms |
68184 KB |
Output is correct |
27 |
Correct |
11 ms |
68320 KB |
Output is correct |
28 |
Correct |
11 ms |
68268 KB |
Output is correct |
29 |
Correct |
11 ms |
68184 KB |
Output is correct |
30 |
Correct |
11 ms |
68188 KB |
Output is correct |
31 |
Correct |
10 ms |
68188 KB |
Output is correct |
32 |
Correct |
11 ms |
68348 KB |
Output is correct |
33 |
Correct |
10 ms |
68188 KB |
Output is correct |
34 |
Correct |
11 ms |
68288 KB |
Output is correct |
35 |
Correct |
12 ms |
68188 KB |
Output is correct |
36 |
Correct |
11 ms |
68188 KB |
Output is correct |
37 |
Correct |
11 ms |
68188 KB |
Output is correct |
38 |
Correct |
10 ms |
68128 KB |
Output is correct |
39 |
Correct |
11 ms |
68188 KB |
Output is correct |
40 |
Correct |
12 ms |
68188 KB |
Output is correct |
41 |
Correct |
11 ms |
68188 KB |
Output is correct |
42 |
Correct |
11 ms |
68188 KB |
Output is correct |
43 |
Correct |
11 ms |
68444 KB |
Output is correct |
44 |
Correct |
10 ms |
68056 KB |
Output is correct |
45 |
Correct |
11 ms |
68188 KB |
Output is correct |
46 |
Correct |
11 ms |
68188 KB |
Output is correct |
47 |
Correct |
10 ms |
68120 KB |
Output is correct |
48 |
Correct |
11 ms |
68224 KB |
Output is correct |
49 |
Correct |
11 ms |
68440 KB |
Output is correct |
50 |
Correct |
12 ms |
68444 KB |
Output is correct |
51 |
Correct |
12 ms |
68320 KB |
Output is correct |
52 |
Correct |
11 ms |
68188 KB |
Output is correct |
53 |
Correct |
11 ms |
68188 KB |
Output is correct |
54 |
Correct |
12 ms |
68300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
67932 KB |
Output is correct |
2 |
Correct |
10 ms |
68188 KB |
Output is correct |
3 |
Correct |
10 ms |
68188 KB |
Output is correct |
4 |
Correct |
10 ms |
68132 KB |
Output is correct |
5 |
Correct |
10 ms |
68072 KB |
Output is correct |
6 |
Correct |
10 ms |
68184 KB |
Output is correct |
7 |
Correct |
10 ms |
67932 KB |
Output is correct |
8 |
Correct |
10 ms |
68188 KB |
Output is correct |
9 |
Correct |
10 ms |
67932 KB |
Output is correct |
10 |
Correct |
10 ms |
68064 KB |
Output is correct |
11 |
Correct |
10 ms |
68188 KB |
Output is correct |
12 |
Correct |
10 ms |
68192 KB |
Output is correct |
13 |
Correct |
10 ms |
68184 KB |
Output is correct |
14 |
Correct |
10 ms |
68188 KB |
Output is correct |
15 |
Correct |
10 ms |
68156 KB |
Output is correct |
16 |
Correct |
10 ms |
68184 KB |
Output is correct |
17 |
Correct |
10 ms |
67928 KB |
Output is correct |
18 |
Correct |
10 ms |
68188 KB |
Output is correct |
19 |
Correct |
10 ms |
67932 KB |
Output is correct |
20 |
Correct |
10 ms |
67928 KB |
Output is correct |
21 |
Correct |
10 ms |
68188 KB |
Output is correct |
22 |
Correct |
11 ms |
68076 KB |
Output is correct |
23 |
Correct |
10 ms |
68032 KB |
Output is correct |
24 |
Correct |
10 ms |
67928 KB |
Output is correct |
25 |
Correct |
10 ms |
68440 KB |
Output is correct |
26 |
Correct |
51 ms |
72140 KB |
Output is correct |
27 |
Correct |
63 ms |
71732 KB |
Output is correct |
28 |
Correct |
11 ms |
68184 KB |
Output is correct |
29 |
Correct |
10 ms |
68188 KB |
Output is correct |
30 |
Correct |
10 ms |
68064 KB |
Output is correct |
31 |
Correct |
62 ms |
71884 KB |
Output is correct |
32 |
Correct |
11 ms |
68188 KB |
Output is correct |
33 |
Correct |
95 ms |
80388 KB |
Output is correct |
34 |
Correct |
63 ms |
71764 KB |
Output is correct |
35 |
Correct |
11 ms |
68564 KB |
Output is correct |
36 |
Correct |
81 ms |
72756 KB |
Output is correct |
37 |
Correct |
11 ms |
68188 KB |
Output is correct |
38 |
Correct |
11 ms |
68056 KB |
Output is correct |
39 |
Correct |
51 ms |
72264 KB |
Output is correct |
40 |
Correct |
11 ms |
68440 KB |
Output is correct |
41 |
Correct |
70 ms |
71964 KB |
Output is correct |
42 |
Correct |
83 ms |
74576 KB |
Output is correct |
43 |
Correct |
10 ms |
68188 KB |
Output is correct |
44 |
Correct |
88 ms |
80724 KB |
Output is correct |
45 |
Correct |
81 ms |
76236 KB |
Output is correct |
46 |
Correct |
11 ms |
68188 KB |
Output is correct |
47 |
Correct |
11 ms |
68188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
72248 KB |
Output is correct |
2 |
Correct |
57 ms |
76184 KB |
Output is correct |
3 |
Correct |
13 ms |
68184 KB |
Output is correct |
4 |
Correct |
12 ms |
68188 KB |
Output is correct |
5 |
Correct |
10 ms |
68188 KB |
Output is correct |
6 |
Correct |
10 ms |
67928 KB |
Output is correct |
7 |
Correct |
11 ms |
68320 KB |
Output is correct |
8 |
Correct |
67 ms |
72528 KB |
Output is correct |
9 |
Correct |
11 ms |
68184 KB |
Output is correct |
10 |
Correct |
68 ms |
71784 KB |
Output is correct |
11 |
Correct |
10 ms |
68188 KB |
Output is correct |
12 |
Correct |
72 ms |
71948 KB |
Output is correct |
13 |
Correct |
76 ms |
72524 KB |
Output is correct |
14 |
Correct |
71 ms |
75580 KB |
Output is correct |
15 |
Correct |
48 ms |
72008 KB |
Output is correct |
16 |
Correct |
12 ms |
68188 KB |
Output is correct |
17 |
Correct |
10 ms |
68188 KB |
Output is correct |
18 |
Correct |
55 ms |
75284 KB |
Output is correct |
19 |
Correct |
76 ms |
80020 KB |
Output is correct |
20 |
Correct |
11 ms |
68184 KB |
Output is correct |
21 |
Correct |
10 ms |
68188 KB |
Output is correct |
22 |
Correct |
54 ms |
73072 KB |
Output is correct |
23 |
Correct |
13 ms |
68444 KB |
Output is correct |
24 |
Correct |
71 ms |
72020 KB |
Output is correct |
25 |
Correct |
72 ms |
78376 KB |
Output is correct |
26 |
Correct |
11 ms |
68188 KB |
Output is correct |
27 |
Correct |
12 ms |
68444 KB |
Output is correct |
28 |
Correct |
11 ms |
68284 KB |
Output is correct |
29 |
Correct |
11 ms |
68188 KB |
Output is correct |
30 |
Correct |
11 ms |
68344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
67932 KB |
Output is correct |
2 |
Correct |
10 ms |
68188 KB |
Output is correct |
3 |
Correct |
10 ms |
68188 KB |
Output is correct |
4 |
Correct |
10 ms |
68132 KB |
Output is correct |
5 |
Correct |
10 ms |
68072 KB |
Output is correct |
6 |
Correct |
10 ms |
68184 KB |
Output is correct |
7 |
Correct |
10 ms |
67932 KB |
Output is correct |
8 |
Correct |
10 ms |
68188 KB |
Output is correct |
9 |
Correct |
10 ms |
67932 KB |
Output is correct |
10 |
Correct |
10 ms |
68064 KB |
Output is correct |
11 |
Correct |
10 ms |
68188 KB |
Output is correct |
12 |
Correct |
10 ms |
68192 KB |
Output is correct |
13 |
Correct |
10 ms |
68184 KB |
Output is correct |
14 |
Correct |
10 ms |
68188 KB |
Output is correct |
15 |
Correct |
10 ms |
68156 KB |
Output is correct |
16 |
Correct |
10 ms |
68184 KB |
Output is correct |
17 |
Correct |
10 ms |
67928 KB |
Output is correct |
18 |
Correct |
10 ms |
68188 KB |
Output is correct |
19 |
Correct |
10 ms |
67932 KB |
Output is correct |
20 |
Correct |
10 ms |
67928 KB |
Output is correct |
21 |
Correct |
10 ms |
68188 KB |
Output is correct |
22 |
Correct |
11 ms |
68076 KB |
Output is correct |
23 |
Correct |
10 ms |
68032 KB |
Output is correct |
24 |
Correct |
10 ms |
67928 KB |
Output is correct |
25 |
Correct |
10 ms |
68188 KB |
Output is correct |
26 |
Correct |
11 ms |
68184 KB |
Output is correct |
27 |
Correct |
11 ms |
68320 KB |
Output is correct |
28 |
Correct |
11 ms |
68268 KB |
Output is correct |
29 |
Correct |
11 ms |
68184 KB |
Output is correct |
30 |
Correct |
11 ms |
68188 KB |
Output is correct |
31 |
Correct |
10 ms |
68188 KB |
Output is correct |
32 |
Correct |
11 ms |
68348 KB |
Output is correct |
33 |
Correct |
10 ms |
68188 KB |
Output is correct |
34 |
Correct |
11 ms |
68288 KB |
Output is correct |
35 |
Correct |
12 ms |
68188 KB |
Output is correct |
36 |
Correct |
11 ms |
68188 KB |
Output is correct |
37 |
Correct |
11 ms |
68188 KB |
Output is correct |
38 |
Correct |
10 ms |
68128 KB |
Output is correct |
39 |
Correct |
11 ms |
68188 KB |
Output is correct |
40 |
Correct |
12 ms |
68188 KB |
Output is correct |
41 |
Correct |
11 ms |
68188 KB |
Output is correct |
42 |
Correct |
11 ms |
68188 KB |
Output is correct |
43 |
Correct |
11 ms |
68444 KB |
Output is correct |
44 |
Correct |
10 ms |
68056 KB |
Output is correct |
45 |
Correct |
11 ms |
68188 KB |
Output is correct |
46 |
Correct |
11 ms |
68188 KB |
Output is correct |
47 |
Correct |
10 ms |
68120 KB |
Output is correct |
48 |
Correct |
11 ms |
68224 KB |
Output is correct |
49 |
Correct |
11 ms |
68440 KB |
Output is correct |
50 |
Correct |
12 ms |
68444 KB |
Output is correct |
51 |
Correct |
12 ms |
68320 KB |
Output is correct |
52 |
Correct |
11 ms |
68188 KB |
Output is correct |
53 |
Correct |
11 ms |
68188 KB |
Output is correct |
54 |
Correct |
12 ms |
68300 KB |
Output is correct |
55 |
Correct |
10 ms |
68440 KB |
Output is correct |
56 |
Correct |
51 ms |
72140 KB |
Output is correct |
57 |
Correct |
63 ms |
71732 KB |
Output is correct |
58 |
Correct |
11 ms |
68184 KB |
Output is correct |
59 |
Correct |
10 ms |
68188 KB |
Output is correct |
60 |
Correct |
10 ms |
68064 KB |
Output is correct |
61 |
Correct |
62 ms |
71884 KB |
Output is correct |
62 |
Correct |
11 ms |
68188 KB |
Output is correct |
63 |
Correct |
95 ms |
80388 KB |
Output is correct |
64 |
Correct |
63 ms |
71764 KB |
Output is correct |
65 |
Correct |
11 ms |
68564 KB |
Output is correct |
66 |
Correct |
81 ms |
72756 KB |
Output is correct |
67 |
Correct |
11 ms |
68188 KB |
Output is correct |
68 |
Correct |
11 ms |
68056 KB |
Output is correct |
69 |
Correct |
51 ms |
72264 KB |
Output is correct |
70 |
Correct |
11 ms |
68440 KB |
Output is correct |
71 |
Correct |
70 ms |
71964 KB |
Output is correct |
72 |
Correct |
83 ms |
74576 KB |
Output is correct |
73 |
Correct |
10 ms |
68188 KB |
Output is correct |
74 |
Correct |
88 ms |
80724 KB |
Output is correct |
75 |
Correct |
81 ms |
76236 KB |
Output is correct |
76 |
Correct |
11 ms |
68188 KB |
Output is correct |
77 |
Correct |
11 ms |
68188 KB |
Output is correct |
78 |
Correct |
48 ms |
72248 KB |
Output is correct |
79 |
Correct |
57 ms |
76184 KB |
Output is correct |
80 |
Correct |
13 ms |
68184 KB |
Output is correct |
81 |
Correct |
12 ms |
68188 KB |
Output is correct |
82 |
Correct |
10 ms |
68188 KB |
Output is correct |
83 |
Correct |
10 ms |
67928 KB |
Output is correct |
84 |
Correct |
11 ms |
68320 KB |
Output is correct |
85 |
Correct |
67 ms |
72528 KB |
Output is correct |
86 |
Correct |
11 ms |
68184 KB |
Output is correct |
87 |
Correct |
68 ms |
71784 KB |
Output is correct |
88 |
Correct |
10 ms |
68188 KB |
Output is correct |
89 |
Correct |
72 ms |
71948 KB |
Output is correct |
90 |
Correct |
76 ms |
72524 KB |
Output is correct |
91 |
Correct |
71 ms |
75580 KB |
Output is correct |
92 |
Correct |
48 ms |
72008 KB |
Output is correct |
93 |
Correct |
12 ms |
68188 KB |
Output is correct |
94 |
Correct |
10 ms |
68188 KB |
Output is correct |
95 |
Correct |
55 ms |
75284 KB |
Output is correct |
96 |
Correct |
76 ms |
80020 KB |
Output is correct |
97 |
Correct |
11 ms |
68184 KB |
Output is correct |
98 |
Correct |
10 ms |
68188 KB |
Output is correct |
99 |
Correct |
54 ms |
73072 KB |
Output is correct |
100 |
Correct |
13 ms |
68444 KB |
Output is correct |
101 |
Correct |
71 ms |
72020 KB |
Output is correct |
102 |
Correct |
72 ms |
78376 KB |
Output is correct |
103 |
Correct |
11 ms |
68188 KB |
Output is correct |
104 |
Correct |
12 ms |
68444 KB |
Output is correct |
105 |
Correct |
11 ms |
68284 KB |
Output is correct |
106 |
Correct |
11 ms |
68188 KB |
Output is correct |
107 |
Correct |
11 ms |
68344 KB |
Output is correct |
108 |
Correct |
348 ms |
90816 KB |
Output is correct |
109 |
Correct |
862 ms |
115264 KB |
Output is correct |
110 |
Correct |
882 ms |
128624 KB |
Output is correct |
111 |
Correct |
778 ms |
141140 KB |
Output is correct |
112 |
Correct |
615 ms |
124104 KB |
Output is correct |
113 |
Correct |
373 ms |
114428 KB |
Output is correct |
114 |
Correct |
481 ms |
93524 KB |
Output is correct |
115 |
Correct |
473 ms |
93508 KB |
Output is correct |
116 |
Correct |
540 ms |
96992 KB |
Output is correct |
117 |
Correct |
556 ms |
115024 KB |
Output is correct |
118 |
Correct |
476 ms |
94916 KB |
Output is correct |
119 |
Correct |
577 ms |
115152 KB |
Output is correct |
120 |
Correct |
647 ms |
131340 KB |
Output is correct |
121 |
Correct |
555 ms |
114904 KB |
Output is correct |
122 |
Correct |
546 ms |
101292 KB |
Output is correct |
123 |
Correct |
325 ms |
117956 KB |
Output is correct |
124 |
Correct |
711 ms |
108292 KB |
Output is correct |