Submission #140534

# Submission time Handle Problem Language Result Execution time Memory
140534 2019-08-03T12:45:00 Z MohamedAhmed04 Mergers (JOI19_mergers) C++14
100 / 100
1880 ms 143856 KB
#include <bits/stdc++.h>
 
using namespace std ;
 
const int MAX = 5e5 + 5 ;
 
vector< vector<int> >adj(MAX) ;
vector< vector<int> >st(MAX) ;
vector< vector<int> >level(MAX) ;
int n , k;
 
int state[MAX] , P[MAX][22] , depth[MAX] , lca[MAX];
int lowestdepth[MAX] , nodenum[MAX] , deg[MAX] , id[MAX] , nxt[MAX];
int nodes = 0 ;
 
void dfs(int node , int lvl , int par)
{
	depth[node] = lvl ;
	P[node][0] = par ;
	for(auto &child : adj[node])
	{
		if(child == par)
			continue ;
		dfs(child , lvl + 1 , node) ;
	}
	return ;
}
 
void preprocess()
{
	dfs(1 , 0 , 0) ;
	for(int j = 1 ; j < 22 ; ++j)
	{
		for(int i = 1 ; i <= n ; ++i)
			P[i][j] = P[P[i][j-1]][j-1] ;
	}
	return ;
}
 
int LCA(int x , int y)
{
	if(depth[x] < depth[y])
		swap(x , y) ;
	for(int i = 21 ; i >= 0 ; --i)
	{
		if(depth[x] - (1 << i) >= depth[y])
			x = P[x][i] ;
	}
	if(x == y)
		return x ;
	for(int i = 21 ; i >= 0 ; --i)
	{
		if(P[x][i] != P[y][i])
			x = P[x][i] , y = P[y][i] ;
	}
	return P[x][0] ;
}

void init()
{
	for(int i = 1 ; i < MAX ; ++i)
		nxt[i] = i ;
}

int root(int node)
{
	if(nxt[node] == node)
		return node ;
	return (nxt[node] = root(nxt[node])) ;
}

void Union(int x , int y)
{
	int a = root(x) ;
	int b = root(y) ;
	nxt[a] = b ;
}

int main()
{
	//ios_base::sync_with_stdio(0) ;
	//cin.tie(0) ;
	cin>>n>>k ;
	for(int i = 0 ; i < n-1 ; ++i)
	{
		int x , y ;
		cin>>x>>y ;
		adj[x].push_back(y) ;
		adj[y].push_back(x) ;
	}
	for(int i = 1 ; i <= n ; ++i)
	{
		cin>>state[i] ;
		st[state[i]].push_back(i) ;
	}
	preprocess() ;
	for(int i = 1 ; i <= k ; ++i)
	{
		int lca2 = st[i][0] ;
		for(auto &node : st[i])
			lca2 = LCA(lca2 , node) ;
		for(auto &node : st[i])
		{
			lca[node] = lca2 ;
		}
	}
	init() ;
	for(int i = 1 ; i <= n ; ++i)
	{
		int now = root(i) ;
		for(; ;)
		{
			if(depth[now] <= depth[lca[i]])
				break ;
			Union(now , P[now][0]) ;
			now = root(now) ;
		}
	}
	for(int i = 1 ; i <= n ; ++i)
	{
		if(root(i) == i && P[i][0] != 0)
			deg[root(P[i][0])]++ , deg[i]++ ;
	}
	int cnt = 0 ;
	for(int i = 1 ; i <= n ; ++i)
	{
		if(deg[i] == 1)
			cnt++ ;
	}
	return cout<<(cnt+1)/2<<"\n" , 0 ;
}
# Verdict Execution time Memory Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 38 ms 37624 KB Output is correct
3 Correct 36 ms 37496 KB Output is correct
4 Correct 36 ms 37624 KB Output is correct
5 Correct 36 ms 37624 KB Output is correct
6 Correct 36 ms 37496 KB Output is correct
7 Correct 36 ms 37624 KB Output is correct
8 Correct 41 ms 37624 KB Output is correct
9 Correct 43 ms 37624 KB Output is correct
10 Correct 39 ms 37624 KB Output is correct
11 Correct 36 ms 37500 KB Output is correct
12 Correct 36 ms 37624 KB Output is correct
13 Correct 35 ms 37624 KB Output is correct
14 Correct 36 ms 37624 KB Output is correct
15 Correct 36 ms 37496 KB Output is correct
16 Correct 36 ms 37588 KB Output is correct
17 Correct 36 ms 37496 KB Output is correct
18 Correct 44 ms 37500 KB Output is correct
19 Correct 36 ms 37496 KB Output is correct
20 Correct 36 ms 37500 KB Output is correct
21 Correct 44 ms 37496 KB Output is correct
22 Correct 37 ms 37524 KB Output is correct
23 Correct 37 ms 37492 KB Output is correct
24 Correct 38 ms 37608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 38 ms 37624 KB Output is correct
3 Correct 36 ms 37496 KB Output is correct
4 Correct 36 ms 37624 KB Output is correct
5 Correct 36 ms 37624 KB Output is correct
6 Correct 36 ms 37496 KB Output is correct
7 Correct 36 ms 37624 KB Output is correct
8 Correct 41 ms 37624 KB Output is correct
9 Correct 43 ms 37624 KB Output is correct
10 Correct 39 ms 37624 KB Output is correct
11 Correct 36 ms 37500 KB Output is correct
12 Correct 36 ms 37624 KB Output is correct
13 Correct 35 ms 37624 KB Output is correct
14 Correct 36 ms 37624 KB Output is correct
15 Correct 36 ms 37496 KB Output is correct
16 Correct 36 ms 37588 KB Output is correct
17 Correct 36 ms 37496 KB Output is correct
18 Correct 44 ms 37500 KB Output is correct
19 Correct 36 ms 37496 KB Output is correct
20 Correct 36 ms 37500 KB Output is correct
21 Correct 44 ms 37496 KB Output is correct
22 Correct 37 ms 37524 KB Output is correct
23 Correct 37 ms 37492 KB Output is correct
24 Correct 38 ms 37608 KB Output is correct
25 Correct 36 ms 37596 KB Output is correct
26 Correct 42 ms 38012 KB Output is correct
27 Correct 48 ms 38008 KB Output is correct
28 Correct 41 ms 38108 KB Output is correct
29 Correct 42 ms 38136 KB Output is correct
30 Correct 41 ms 38136 KB Output is correct
31 Correct 36 ms 37624 KB Output is correct
32 Correct 43 ms 38264 KB Output is correct
33 Correct 37 ms 37604 KB Output is correct
34 Correct 42 ms 38012 KB Output is correct
35 Correct 42 ms 38136 KB Output is correct
36 Correct 42 ms 38156 KB Output is correct
37 Correct 43 ms 37980 KB Output is correct
38 Correct 36 ms 37496 KB Output is correct
39 Correct 42 ms 38008 KB Output is correct
40 Correct 41 ms 38008 KB Output is correct
41 Correct 42 ms 38008 KB Output is correct
42 Correct 42 ms 38108 KB Output is correct
43 Correct 42 ms 38148 KB Output is correct
44 Correct 36 ms 37624 KB Output is correct
45 Correct 46 ms 38136 KB Output is correct
46 Correct 42 ms 38008 KB Output is correct
47 Correct 39 ms 37496 KB Output is correct
48 Correct 41 ms 38008 KB Output is correct
49 Correct 41 ms 38136 KB Output is correct
50 Correct 41 ms 38136 KB Output is correct
51 Correct 41 ms 38012 KB Output is correct
52 Correct 41 ms 37980 KB Output is correct
53 Correct 41 ms 38024 KB Output is correct
54 Correct 41 ms 37984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 38 ms 37624 KB Output is correct
3 Correct 36 ms 37496 KB Output is correct
4 Correct 36 ms 37624 KB Output is correct
5 Correct 36 ms 37624 KB Output is correct
6 Correct 36 ms 37496 KB Output is correct
7 Correct 36 ms 37624 KB Output is correct
8 Correct 41 ms 37624 KB Output is correct
9 Correct 43 ms 37624 KB Output is correct
10 Correct 39 ms 37624 KB Output is correct
11 Correct 36 ms 37500 KB Output is correct
12 Correct 36 ms 37624 KB Output is correct
13 Correct 35 ms 37624 KB Output is correct
14 Correct 36 ms 37624 KB Output is correct
15 Correct 36 ms 37496 KB Output is correct
16 Correct 36 ms 37588 KB Output is correct
17 Correct 36 ms 37496 KB Output is correct
18 Correct 44 ms 37500 KB Output is correct
19 Correct 36 ms 37496 KB Output is correct
20 Correct 36 ms 37500 KB Output is correct
21 Correct 44 ms 37496 KB Output is correct
22 Correct 37 ms 37524 KB Output is correct
23 Correct 37 ms 37492 KB Output is correct
24 Correct 38 ms 37608 KB Output is correct
25 Correct 35 ms 37496 KB Output is correct
26 Correct 226 ms 52884 KB Output is correct
27 Correct 267 ms 52516 KB Output is correct
28 Correct 50 ms 38008 KB Output is correct
29 Correct 39 ms 37624 KB Output is correct
30 Correct 36 ms 37624 KB Output is correct
31 Correct 261 ms 52640 KB Output is correct
32 Correct 41 ms 38008 KB Output is correct
33 Correct 313 ms 56796 KB Output is correct
34 Correct 261 ms 52600 KB Output is correct
35 Correct 42 ms 38136 KB Output is correct
36 Correct 300 ms 53112 KB Output is correct
37 Correct 42 ms 38008 KB Output is correct
38 Correct 41 ms 38136 KB Output is correct
39 Correct 230 ms 52824 KB Output is correct
40 Correct 41 ms 38136 KB Output is correct
41 Correct 259 ms 52468 KB Output is correct
42 Correct 303 ms 54008 KB Output is correct
43 Correct 36 ms 37496 KB Output is correct
44 Correct 353 ms 56964 KB Output is correct
45 Correct 288 ms 54776 KB Output is correct
46 Correct 41 ms 38008 KB Output is correct
47 Correct 41 ms 38008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 223 ms 52848 KB Output is correct
2 Correct 271 ms 56176 KB Output is correct
3 Correct 42 ms 38136 KB Output is correct
4 Correct 41 ms 38012 KB Output is correct
5 Correct 37 ms 37496 KB Output is correct
6 Correct 37 ms 37624 KB Output is correct
7 Correct 41 ms 37980 KB Output is correct
8 Correct 301 ms 54256 KB Output is correct
9 Correct 41 ms 38008 KB Output is correct
10 Correct 267 ms 53112 KB Output is correct
11 Correct 37 ms 37496 KB Output is correct
12 Correct 287 ms 52988 KB Output is correct
13 Correct 299 ms 54264 KB Output is correct
14 Correct 292 ms 55800 KB Output is correct
15 Correct 237 ms 52848 KB Output is correct
16 Correct 42 ms 38140 KB Output is correct
17 Correct 36 ms 37624 KB Output is correct
18 Correct 272 ms 55896 KB Output is correct
19 Correct 304 ms 58204 KB Output is correct
20 Correct 42 ms 38008 KB Output is correct
21 Correct 36 ms 37624 KB Output is correct
22 Correct 264 ms 54384 KB Output is correct
23 Correct 42 ms 38008 KB Output is correct
24 Correct 292 ms 53400 KB Output is correct
25 Correct 302 ms 57336 KB Output is correct
26 Correct 41 ms 38136 KB Output is correct
27 Correct 41 ms 38136 KB Output is correct
28 Correct 42 ms 38008 KB Output is correct
29 Correct 41 ms 38008 KB Output is correct
30 Correct 41 ms 38112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 38 ms 37624 KB Output is correct
3 Correct 36 ms 37496 KB Output is correct
4 Correct 36 ms 37624 KB Output is correct
5 Correct 36 ms 37624 KB Output is correct
6 Correct 36 ms 37496 KB Output is correct
7 Correct 36 ms 37624 KB Output is correct
8 Correct 41 ms 37624 KB Output is correct
9 Correct 43 ms 37624 KB Output is correct
10 Correct 39 ms 37624 KB Output is correct
11 Correct 36 ms 37500 KB Output is correct
12 Correct 36 ms 37624 KB Output is correct
13 Correct 35 ms 37624 KB Output is correct
14 Correct 36 ms 37624 KB Output is correct
15 Correct 36 ms 37496 KB Output is correct
16 Correct 36 ms 37588 KB Output is correct
17 Correct 36 ms 37496 KB Output is correct
18 Correct 44 ms 37500 KB Output is correct
19 Correct 36 ms 37496 KB Output is correct
20 Correct 36 ms 37500 KB Output is correct
21 Correct 44 ms 37496 KB Output is correct
22 Correct 37 ms 37524 KB Output is correct
23 Correct 37 ms 37492 KB Output is correct
24 Correct 38 ms 37608 KB Output is correct
25 Correct 36 ms 37596 KB Output is correct
26 Correct 42 ms 38012 KB Output is correct
27 Correct 48 ms 38008 KB Output is correct
28 Correct 41 ms 38108 KB Output is correct
29 Correct 42 ms 38136 KB Output is correct
30 Correct 41 ms 38136 KB Output is correct
31 Correct 36 ms 37624 KB Output is correct
32 Correct 43 ms 38264 KB Output is correct
33 Correct 37 ms 37604 KB Output is correct
34 Correct 42 ms 38012 KB Output is correct
35 Correct 42 ms 38136 KB Output is correct
36 Correct 42 ms 38156 KB Output is correct
37 Correct 43 ms 37980 KB Output is correct
38 Correct 36 ms 37496 KB Output is correct
39 Correct 42 ms 38008 KB Output is correct
40 Correct 41 ms 38008 KB Output is correct
41 Correct 42 ms 38008 KB Output is correct
42 Correct 42 ms 38108 KB Output is correct
43 Correct 42 ms 38148 KB Output is correct
44 Correct 36 ms 37624 KB Output is correct
45 Correct 46 ms 38136 KB Output is correct
46 Correct 42 ms 38008 KB Output is correct
47 Correct 39 ms 37496 KB Output is correct
48 Correct 41 ms 38008 KB Output is correct
49 Correct 41 ms 38136 KB Output is correct
50 Correct 41 ms 38136 KB Output is correct
51 Correct 41 ms 38012 KB Output is correct
52 Correct 41 ms 37980 KB Output is correct
53 Correct 41 ms 38024 KB Output is correct
54 Correct 41 ms 37984 KB Output is correct
55 Correct 35 ms 37496 KB Output is correct
56 Correct 226 ms 52884 KB Output is correct
57 Correct 267 ms 52516 KB Output is correct
58 Correct 50 ms 38008 KB Output is correct
59 Correct 39 ms 37624 KB Output is correct
60 Correct 36 ms 37624 KB Output is correct
61 Correct 261 ms 52640 KB Output is correct
62 Correct 41 ms 38008 KB Output is correct
63 Correct 313 ms 56796 KB Output is correct
64 Correct 261 ms 52600 KB Output is correct
65 Correct 42 ms 38136 KB Output is correct
66 Correct 300 ms 53112 KB Output is correct
67 Correct 42 ms 38008 KB Output is correct
68 Correct 41 ms 38136 KB Output is correct
69 Correct 230 ms 52824 KB Output is correct
70 Correct 41 ms 38136 KB Output is correct
71 Correct 259 ms 52468 KB Output is correct
72 Correct 303 ms 54008 KB Output is correct
73 Correct 36 ms 37496 KB Output is correct
74 Correct 353 ms 56964 KB Output is correct
75 Correct 288 ms 54776 KB Output is correct
76 Correct 41 ms 38008 KB Output is correct
77 Correct 41 ms 38008 KB Output is correct
78 Correct 223 ms 52848 KB Output is correct
79 Correct 271 ms 56176 KB Output is correct
80 Correct 42 ms 38136 KB Output is correct
81 Correct 41 ms 38012 KB Output is correct
82 Correct 37 ms 37496 KB Output is correct
83 Correct 37 ms 37624 KB Output is correct
84 Correct 41 ms 37980 KB Output is correct
85 Correct 301 ms 54256 KB Output is correct
86 Correct 41 ms 38008 KB Output is correct
87 Correct 267 ms 53112 KB Output is correct
88 Correct 37 ms 37496 KB Output is correct
89 Correct 287 ms 52988 KB Output is correct
90 Correct 299 ms 54264 KB Output is correct
91 Correct 292 ms 55800 KB Output is correct
92 Correct 237 ms 52848 KB Output is correct
93 Correct 42 ms 38140 KB Output is correct
94 Correct 36 ms 37624 KB Output is correct
95 Correct 272 ms 55896 KB Output is correct
96 Correct 304 ms 58204 KB Output is correct
97 Correct 42 ms 38008 KB Output is correct
98 Correct 36 ms 37624 KB Output is correct
99 Correct 264 ms 54384 KB Output is correct
100 Correct 42 ms 38008 KB Output is correct
101 Correct 292 ms 53400 KB Output is correct
102 Correct 302 ms 57336 KB Output is correct
103 Correct 41 ms 38136 KB Output is correct
104 Correct 41 ms 38136 KB Output is correct
105 Correct 42 ms 38008 KB Output is correct
106 Correct 41 ms 38008 KB Output is correct
107 Correct 41 ms 38112 KB Output is correct
108 Correct 1505 ms 122596 KB Output is correct
109 Correct 1810 ms 122968 KB Output is correct
110 Correct 1880 ms 129740 KB Output is correct
111 Correct 1801 ms 143856 KB Output is correct
112 Correct 1810 ms 138104 KB Output is correct
113 Correct 1512 ms 130244 KB Output is correct
114 Correct 1575 ms 112572 KB Output is correct
115 Correct 1412 ms 112488 KB Output is correct
116 Correct 1725 ms 118776 KB Output is correct
117 Correct 1652 ms 130404 KB Output is correct
118 Correct 1498 ms 114424 KB Output is correct
119 Correct 1674 ms 130140 KB Output is correct
120 Correct 1732 ms 138352 KB Output is correct
121 Correct 1689 ms 130424 KB Output is correct
122 Correct 1770 ms 123980 KB Output is correct
123 Correct 1599 ms 132072 KB Output is correct
124 Correct 1744 ms 128188 KB Output is correct