import java.io.*; import java.util.*;
public class mergers{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
}
static int MOD=998244353;
static int[] par,dep;
static int[] skip;
public static void main(String[] args){
FastReader br=new FastReader();
int N=br.nextInt(); int M=br.nextInt();
ArrayList<Integer> states[]=new ArrayList[M]; for(int i=0;i<M;i++){states[i]=new ArrayList<>();}
ArrayList<Integer> adj[]=new ArrayList[N]; for(int i=0;i<N;i++){adj[i]=new ArrayList<>();}
int a,b;
for (int i = 0; i < N-1; i++) {
a=br.nextInt()-1; b=br.nextInt()-1; adj[a].add(b); adj[b].add(a);
}
for (int i = 0; i < N; i++) {
a=br.nextInt()-1; states[a].add(i);
}
par=new int[N]; dep=new int[N]; dfs(adj,-1,0);
skip=new int[N];//Skipping: avoiding repeated merging
for (int i = 0; i < N; i++) {
skip[i]=i;
}
int u;
for (int i = 0; i < M; i++) {
if(states[i].isEmpty())continue;
u=states[i].get(0);
for (int j = 1; j < states[i].size(); j++) {
a=u; b=states[i].get(j);
while(a!=b){
if(dep[a]>=dep[b]){
skip[a]=find(par[a]); a=skip[a];
}else{
skip[b]=find(par[b]); b=skip[b];
}
}
//For each a, we have merged a with its 1st, .., kth ancestor.
//They belong to the same state
}
}
//In the end, all vertices of the same state have the same skip
//Therefore, after merging, if two vertices don't have some skip, they are not in the same state
int[] deg=new int[N];
for (int i = 0; i < N; i++) {
a=find(i);
for (int j : adj[i]) {
if(a!=find(j)){
//Notice in the compressed form, there is only one leader for each state, namely j
//If i is adjacent to two vertices with same find, i must have that find too
deg[a]++;
}
}
}
//System.out.println(Arrays.toString(deg));
int ans=0;
for (int i = 0; i < N; i++) {
if(deg[i]==1)ans++;
}
System.out.println((ans+1)/2);
//If a state is a leaf, there exist exactly one
}
public static int find(int x){
if(skip[x]==x)return x;
skip[x]=find(skip[x]); return skip[x];
}
public static void dfs(ArrayList<Integer>[] adj, int p, int v){
for (int c : adj[v]) {
if(c==p)continue;
dep[c]=dep[v]+1; par[c]=v; dfs(adj,v,c);
}
}
}
//Debugging:
//Are you sure your algorithm is correct?
//Bounds: long
//Special cases: n=0,1?
//Make sure you remove your debugging code before you submit!
Compilation message
Note: mergers.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
8428 KB |
Output is correct |
2 |
Correct |
77 ms |
8540 KB |
Output is correct |
3 |
Correct |
76 ms |
8556 KB |
Output is correct |
4 |
Correct |
74 ms |
8428 KB |
Output is correct |
5 |
Correct |
70 ms |
8556 KB |
Output is correct |
6 |
Correct |
78 ms |
8656 KB |
Output is correct |
7 |
Correct |
71 ms |
8556 KB |
Output is correct |
8 |
Correct |
77 ms |
8668 KB |
Output is correct |
9 |
Correct |
71 ms |
8556 KB |
Output is correct |
10 |
Correct |
70 ms |
8556 KB |
Output is correct |
11 |
Correct |
76 ms |
8556 KB |
Output is correct |
12 |
Correct |
75 ms |
8556 KB |
Output is correct |
13 |
Correct |
72 ms |
8448 KB |
Output is correct |
14 |
Correct |
79 ms |
8664 KB |
Output is correct |
15 |
Correct |
78 ms |
8556 KB |
Output is correct |
16 |
Correct |
72 ms |
8556 KB |
Output is correct |
17 |
Correct |
72 ms |
8460 KB |
Output is correct |
18 |
Correct |
78 ms |
8560 KB |
Output is correct |
19 |
Correct |
71 ms |
8684 KB |
Output is correct |
20 |
Correct |
72 ms |
8664 KB |
Output is correct |
21 |
Correct |
77 ms |
8596 KB |
Output is correct |
22 |
Correct |
76 ms |
8556 KB |
Output is correct |
23 |
Correct |
70 ms |
8556 KB |
Output is correct |
24 |
Correct |
74 ms |
8684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
8428 KB |
Output is correct |
2 |
Correct |
77 ms |
8540 KB |
Output is correct |
3 |
Correct |
76 ms |
8556 KB |
Output is correct |
4 |
Correct |
74 ms |
8428 KB |
Output is correct |
5 |
Correct |
70 ms |
8556 KB |
Output is correct |
6 |
Correct |
78 ms |
8656 KB |
Output is correct |
7 |
Correct |
71 ms |
8556 KB |
Output is correct |
8 |
Correct |
77 ms |
8668 KB |
Output is correct |
9 |
Correct |
71 ms |
8556 KB |
Output is correct |
10 |
Correct |
70 ms |
8556 KB |
Output is correct |
11 |
Correct |
76 ms |
8556 KB |
Output is correct |
12 |
Correct |
75 ms |
8556 KB |
Output is correct |
13 |
Correct |
72 ms |
8448 KB |
Output is correct |
14 |
Correct |
79 ms |
8664 KB |
Output is correct |
15 |
Correct |
78 ms |
8556 KB |
Output is correct |
16 |
Correct |
72 ms |
8556 KB |
Output is correct |
17 |
Correct |
72 ms |
8460 KB |
Output is correct |
18 |
Correct |
78 ms |
8560 KB |
Output is correct |
19 |
Correct |
71 ms |
8684 KB |
Output is correct |
20 |
Correct |
72 ms |
8664 KB |
Output is correct |
21 |
Correct |
77 ms |
8596 KB |
Output is correct |
22 |
Correct |
76 ms |
8556 KB |
Output is correct |
23 |
Correct |
70 ms |
8556 KB |
Output is correct |
24 |
Correct |
74 ms |
8684 KB |
Output is correct |
25 |
Correct |
74 ms |
8556 KB |
Output is correct |
26 |
Correct |
288 ms |
14580 KB |
Output is correct |
27 |
Correct |
289 ms |
14672 KB |
Output is correct |
28 |
Correct |
289 ms |
14816 KB |
Output is correct |
29 |
Correct |
289 ms |
14676 KB |
Output is correct |
30 |
Correct |
284 ms |
14544 KB |
Output is correct |
31 |
Correct |
75 ms |
8668 KB |
Output is correct |
32 |
Correct |
301 ms |
15136 KB |
Output is correct |
33 |
Correct |
79 ms |
8556 KB |
Output is correct |
34 |
Correct |
281 ms |
14556 KB |
Output is correct |
35 |
Correct |
287 ms |
14652 KB |
Output is correct |
36 |
Correct |
283 ms |
14432 KB |
Output is correct |
37 |
Correct |
286 ms |
14552 KB |
Output is correct |
38 |
Correct |
81 ms |
8668 KB |
Output is correct |
39 |
Correct |
316 ms |
14580 KB |
Output is correct |
40 |
Correct |
334 ms |
14560 KB |
Output is correct |
41 |
Correct |
282 ms |
14432 KB |
Output is correct |
42 |
Correct |
287 ms |
14432 KB |
Output is correct |
43 |
Correct |
285 ms |
14928 KB |
Output is correct |
44 |
Correct |
76 ms |
8428 KB |
Output is correct |
45 |
Correct |
340 ms |
14844 KB |
Output is correct |
46 |
Correct |
337 ms |
14444 KB |
Output is correct |
47 |
Correct |
76 ms |
8556 KB |
Output is correct |
48 |
Correct |
288 ms |
14532 KB |
Output is correct |
49 |
Correct |
292 ms |
14636 KB |
Output is correct |
50 |
Correct |
291 ms |
14928 KB |
Output is correct |
51 |
Correct |
289 ms |
14560 KB |
Output is correct |
52 |
Correct |
286 ms |
14384 KB |
Output is correct |
53 |
Correct |
295 ms |
14588 KB |
Output is correct |
54 |
Correct |
286 ms |
14560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
8428 KB |
Output is correct |
2 |
Correct |
77 ms |
8540 KB |
Output is correct |
3 |
Correct |
76 ms |
8556 KB |
Output is correct |
4 |
Correct |
74 ms |
8428 KB |
Output is correct |
5 |
Correct |
70 ms |
8556 KB |
Output is correct |
6 |
Correct |
78 ms |
8656 KB |
Output is correct |
7 |
Correct |
71 ms |
8556 KB |
Output is correct |
8 |
Correct |
77 ms |
8668 KB |
Output is correct |
9 |
Correct |
71 ms |
8556 KB |
Output is correct |
10 |
Correct |
70 ms |
8556 KB |
Output is correct |
11 |
Correct |
76 ms |
8556 KB |
Output is correct |
12 |
Correct |
75 ms |
8556 KB |
Output is correct |
13 |
Correct |
72 ms |
8448 KB |
Output is correct |
14 |
Correct |
79 ms |
8664 KB |
Output is correct |
15 |
Correct |
78 ms |
8556 KB |
Output is correct |
16 |
Correct |
72 ms |
8556 KB |
Output is correct |
17 |
Correct |
72 ms |
8460 KB |
Output is correct |
18 |
Correct |
78 ms |
8560 KB |
Output is correct |
19 |
Correct |
71 ms |
8684 KB |
Output is correct |
20 |
Correct |
72 ms |
8664 KB |
Output is correct |
21 |
Correct |
77 ms |
8596 KB |
Output is correct |
22 |
Correct |
76 ms |
8556 KB |
Output is correct |
23 |
Correct |
70 ms |
8556 KB |
Output is correct |
24 |
Correct |
74 ms |
8684 KB |
Output is correct |
25 |
Correct |
76 ms |
8556 KB |
Output is correct |
26 |
Correct |
700 ms |
35216 KB |
Output is correct |
27 |
Correct |
710 ms |
35428 KB |
Output is correct |
28 |
Correct |
282 ms |
14672 KB |
Output is correct |
29 |
Correct |
73 ms |
8556 KB |
Output is correct |
30 |
Correct |
73 ms |
8556 KB |
Output is correct |
31 |
Correct |
720 ms |
35304 KB |
Output is correct |
32 |
Correct |
280 ms |
14556 KB |
Output is correct |
33 |
Correct |
788 ms |
40660 KB |
Output is correct |
34 |
Correct |
769 ms |
35268 KB |
Output is correct |
35 |
Correct |
285 ms |
14432 KB |
Output is correct |
36 |
Correct |
782 ms |
36268 KB |
Output is correct |
37 |
Correct |
332 ms |
14800 KB |
Output is correct |
38 |
Correct |
281 ms |
14432 KB |
Output is correct |
39 |
Correct |
673 ms |
35532 KB |
Output is correct |
40 |
Correct |
287 ms |
14836 KB |
Output is correct |
41 |
Correct |
706 ms |
35032 KB |
Output is correct |
42 |
Correct |
1004 ms |
37696 KB |
Output is correct |
43 |
Correct |
78 ms |
8556 KB |
Output is correct |
44 |
Correct |
735 ms |
40496 KB |
Output is correct |
45 |
Correct |
758 ms |
38684 KB |
Output is correct |
46 |
Correct |
284 ms |
14436 KB |
Output is correct |
47 |
Correct |
282 ms |
14560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
704 ms |
34872 KB |
Output is correct |
2 |
Correct |
750 ms |
48104 KB |
Output is correct |
3 |
Correct |
292 ms |
14560 KB |
Output is correct |
4 |
Correct |
291 ms |
14392 KB |
Output is correct |
5 |
Correct |
73 ms |
8556 KB |
Output is correct |
6 |
Correct |
77 ms |
8576 KB |
Output is correct |
7 |
Correct |
280 ms |
14556 KB |
Output is correct |
8 |
Correct |
769 ms |
37368 KB |
Output is correct |
9 |
Correct |
285 ms |
14432 KB |
Output is correct |
10 |
Correct |
732 ms |
35028 KB |
Output is correct |
11 |
Correct |
76 ms |
8556 KB |
Output is correct |
12 |
Correct |
726 ms |
35192 KB |
Output is correct |
13 |
Correct |
755 ms |
37520 KB |
Output is correct |
14 |
Correct |
822 ms |
41636 KB |
Output is correct |
15 |
Correct |
669 ms |
35580 KB |
Output is correct |
16 |
Correct |
289 ms |
14560 KB |
Output is correct |
17 |
Correct |
76 ms |
8556 KB |
Output is correct |
18 |
Correct |
768 ms |
47328 KB |
Output is correct |
19 |
Correct |
1231 ms |
50476 KB |
Output is correct |
20 |
Correct |
339 ms |
14688 KB |
Output is correct |
21 |
Correct |
78 ms |
8668 KB |
Output is correct |
22 |
Correct |
768 ms |
39968 KB |
Output is correct |
23 |
Correct |
287 ms |
14708 KB |
Output is correct |
24 |
Correct |
731 ms |
35668 KB |
Output is correct |
25 |
Correct |
768 ms |
44048 KB |
Output is correct |
26 |
Correct |
293 ms |
14616 KB |
Output is correct |
27 |
Correct |
289 ms |
14688 KB |
Output is correct |
28 |
Correct |
289 ms |
14556 KB |
Output is correct |
29 |
Correct |
284 ms |
14688 KB |
Output is correct |
30 |
Correct |
287 ms |
14580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
8428 KB |
Output is correct |
2 |
Correct |
77 ms |
8540 KB |
Output is correct |
3 |
Correct |
76 ms |
8556 KB |
Output is correct |
4 |
Correct |
74 ms |
8428 KB |
Output is correct |
5 |
Correct |
70 ms |
8556 KB |
Output is correct |
6 |
Correct |
78 ms |
8656 KB |
Output is correct |
7 |
Correct |
71 ms |
8556 KB |
Output is correct |
8 |
Correct |
77 ms |
8668 KB |
Output is correct |
9 |
Correct |
71 ms |
8556 KB |
Output is correct |
10 |
Correct |
70 ms |
8556 KB |
Output is correct |
11 |
Correct |
76 ms |
8556 KB |
Output is correct |
12 |
Correct |
75 ms |
8556 KB |
Output is correct |
13 |
Correct |
72 ms |
8448 KB |
Output is correct |
14 |
Correct |
79 ms |
8664 KB |
Output is correct |
15 |
Correct |
78 ms |
8556 KB |
Output is correct |
16 |
Correct |
72 ms |
8556 KB |
Output is correct |
17 |
Correct |
72 ms |
8460 KB |
Output is correct |
18 |
Correct |
78 ms |
8560 KB |
Output is correct |
19 |
Correct |
71 ms |
8684 KB |
Output is correct |
20 |
Correct |
72 ms |
8664 KB |
Output is correct |
21 |
Correct |
77 ms |
8596 KB |
Output is correct |
22 |
Correct |
76 ms |
8556 KB |
Output is correct |
23 |
Correct |
70 ms |
8556 KB |
Output is correct |
24 |
Correct |
74 ms |
8684 KB |
Output is correct |
25 |
Correct |
74 ms |
8556 KB |
Output is correct |
26 |
Correct |
288 ms |
14580 KB |
Output is correct |
27 |
Correct |
289 ms |
14672 KB |
Output is correct |
28 |
Correct |
289 ms |
14816 KB |
Output is correct |
29 |
Correct |
289 ms |
14676 KB |
Output is correct |
30 |
Correct |
284 ms |
14544 KB |
Output is correct |
31 |
Correct |
75 ms |
8668 KB |
Output is correct |
32 |
Correct |
301 ms |
15136 KB |
Output is correct |
33 |
Correct |
79 ms |
8556 KB |
Output is correct |
34 |
Correct |
281 ms |
14556 KB |
Output is correct |
35 |
Correct |
287 ms |
14652 KB |
Output is correct |
36 |
Correct |
283 ms |
14432 KB |
Output is correct |
37 |
Correct |
286 ms |
14552 KB |
Output is correct |
38 |
Correct |
81 ms |
8668 KB |
Output is correct |
39 |
Correct |
316 ms |
14580 KB |
Output is correct |
40 |
Correct |
334 ms |
14560 KB |
Output is correct |
41 |
Correct |
282 ms |
14432 KB |
Output is correct |
42 |
Correct |
287 ms |
14432 KB |
Output is correct |
43 |
Correct |
285 ms |
14928 KB |
Output is correct |
44 |
Correct |
76 ms |
8428 KB |
Output is correct |
45 |
Correct |
340 ms |
14844 KB |
Output is correct |
46 |
Correct |
337 ms |
14444 KB |
Output is correct |
47 |
Correct |
76 ms |
8556 KB |
Output is correct |
48 |
Correct |
288 ms |
14532 KB |
Output is correct |
49 |
Correct |
292 ms |
14636 KB |
Output is correct |
50 |
Correct |
291 ms |
14928 KB |
Output is correct |
51 |
Correct |
289 ms |
14560 KB |
Output is correct |
52 |
Correct |
286 ms |
14384 KB |
Output is correct |
53 |
Correct |
295 ms |
14588 KB |
Output is correct |
54 |
Correct |
286 ms |
14560 KB |
Output is correct |
55 |
Correct |
76 ms |
8556 KB |
Output is correct |
56 |
Correct |
700 ms |
35216 KB |
Output is correct |
57 |
Correct |
710 ms |
35428 KB |
Output is correct |
58 |
Correct |
282 ms |
14672 KB |
Output is correct |
59 |
Correct |
73 ms |
8556 KB |
Output is correct |
60 |
Correct |
73 ms |
8556 KB |
Output is correct |
61 |
Correct |
720 ms |
35304 KB |
Output is correct |
62 |
Correct |
280 ms |
14556 KB |
Output is correct |
63 |
Correct |
788 ms |
40660 KB |
Output is correct |
64 |
Correct |
769 ms |
35268 KB |
Output is correct |
65 |
Correct |
285 ms |
14432 KB |
Output is correct |
66 |
Correct |
782 ms |
36268 KB |
Output is correct |
67 |
Correct |
332 ms |
14800 KB |
Output is correct |
68 |
Correct |
281 ms |
14432 KB |
Output is correct |
69 |
Correct |
673 ms |
35532 KB |
Output is correct |
70 |
Correct |
287 ms |
14836 KB |
Output is correct |
71 |
Correct |
706 ms |
35032 KB |
Output is correct |
72 |
Correct |
1004 ms |
37696 KB |
Output is correct |
73 |
Correct |
78 ms |
8556 KB |
Output is correct |
74 |
Correct |
735 ms |
40496 KB |
Output is correct |
75 |
Correct |
758 ms |
38684 KB |
Output is correct |
76 |
Correct |
284 ms |
14436 KB |
Output is correct |
77 |
Correct |
282 ms |
14560 KB |
Output is correct |
78 |
Correct |
704 ms |
34872 KB |
Output is correct |
79 |
Correct |
750 ms |
48104 KB |
Output is correct |
80 |
Correct |
292 ms |
14560 KB |
Output is correct |
81 |
Correct |
291 ms |
14392 KB |
Output is correct |
82 |
Correct |
73 ms |
8556 KB |
Output is correct |
83 |
Correct |
77 ms |
8576 KB |
Output is correct |
84 |
Correct |
280 ms |
14556 KB |
Output is correct |
85 |
Correct |
769 ms |
37368 KB |
Output is correct |
86 |
Correct |
285 ms |
14432 KB |
Output is correct |
87 |
Correct |
732 ms |
35028 KB |
Output is correct |
88 |
Correct |
76 ms |
8556 KB |
Output is correct |
89 |
Correct |
726 ms |
35192 KB |
Output is correct |
90 |
Correct |
755 ms |
37520 KB |
Output is correct |
91 |
Correct |
822 ms |
41636 KB |
Output is correct |
92 |
Correct |
669 ms |
35580 KB |
Output is correct |
93 |
Correct |
289 ms |
14560 KB |
Output is correct |
94 |
Correct |
76 ms |
8556 KB |
Output is correct |
95 |
Correct |
768 ms |
47328 KB |
Output is correct |
96 |
Correct |
1231 ms |
50476 KB |
Output is correct |
97 |
Correct |
339 ms |
14688 KB |
Output is correct |
98 |
Correct |
78 ms |
8668 KB |
Output is correct |
99 |
Correct |
768 ms |
39968 KB |
Output is correct |
100 |
Correct |
287 ms |
14708 KB |
Output is correct |
101 |
Correct |
731 ms |
35668 KB |
Output is correct |
102 |
Correct |
768 ms |
44048 KB |
Output is correct |
103 |
Correct |
293 ms |
14616 KB |
Output is correct |
104 |
Correct |
289 ms |
14688 KB |
Output is correct |
105 |
Correct |
289 ms |
14556 KB |
Output is correct |
106 |
Correct |
284 ms |
14688 KB |
Output is correct |
107 |
Correct |
287 ms |
14580 KB |
Output is correct |
108 |
Correct |
2036 ms |
131956 KB |
Output is correct |
109 |
Correct |
2171 ms |
135720 KB |
Output is correct |
110 |
Execution timed out |
3097 ms |
132892 KB |
Time limit exceeded |
111 |
Halted |
0 ms |
0 KB |
- |