Submission #138078

# Submission time Handle Problem Language Result Execution time Memory
138078 2019-07-29T09:44:40 Z anayk Mergers (JOI19_mergers) C++14
100 / 100
2471 ms 110124 KB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>

#define MAXN 500005
#define LOGM 22

int par[LOGM][MAXN];
int p[MAXN];
int lev[MAXN];
int mark[MAXN];
int type[MAXN];
int tar[MAXN];
std::vector<int> Adj[MAXN];
std::vector<int> coll[MAXN];
std::pair<int, int> ord[MAXN];
std::queue<int> q;
std::stack<std::pair<int, int> > s;
int deg[MAXN];

int find(int u)
{
	if(p[u] < 0)
		return u;
	else
		return p[u] = find(p[u]);
}

void merge(int u, int v)
{
	u = find(u);
	v = find(v);
	
	if(u == v)
		return;
	
	if(p[u] > p[v])
		u ^= v ^= u ^= v;
	
	p[u] += p[v];
	p[v] = u;
}

void dfs()
{
	while(!s.empty())
	{
		int u = s.top().first;
		int p;
		p = s.top().second;
		
		lev[u] = 0;
		if(p)
		{
			par[0][u] = p;
			lev[u] = lev[p]+1;
		}
		
		for(int i = 1; i < LOGM; i++)
		{
			if(par[i-1][u] > 0)
				par[i][u] = par[i-1][par[i-1][u]];
		}
		
		s.pop();
		
		for(int v : Adj[u])
		{
			if(v == p)
				continue;
			
			s.push({v, u});
		}
	}
}

int lca(int u, int v)
{
	if(lev[u] > lev[v])
		u ^= v ^= u ^= v;
	
	for(int i = LOGM-1; i >= 0; i--)
	{
		if(lev[v]-lev[u] >= (1 << i))
			v = par[i][v];
	}
	
	if(u == v)
		return u;
	
	for(int i = LOGM-1; i >= 0; i--)
	{
		if(par[i][u] != par[i][v])
		{
			u = par[i][u];
			v = par[i][v];
		}
	}
	
	return par[0][u];
}

int main()
{
	int n, k;
	std::cin >> n >> k;
	
	for(int i = 1; i < n; i++)
	{
		int a, b;
		std::cin >> a >> b;
		Adj[a].push_back(b);
		Adj[b].push_back(a);
	}
	
	s.push({1, 0});
	dfs();
	
	for(int i = 0; i <= k; i++)
		p[i] = -1;
	
	for(int i = 1; i <= n; i++)
	{
		int t;
		std::cin >> t;
		coll[t].push_back(i);
		type[i] = t;
		
		if(tar[t] == 0)
			tar[t] = i;
		else
			tar[t] = lca(tar[t], i);
	}
	
	for(int i = 0; i < k; i++)
	{
		ord[i] = {lev[tar[i+1]], i+1};
	}
	
	std::sort(ord, ord+k);
	
	for(int i = 0; i < k; i++)
	{
		int t = ord[i].second;
		if(mark[t])
			continue;
				q.push(t);
		mark[t] = i+1;
		while(!q.empty())
		{
			int c = q.front(); q.pop();
			for(int j : coll[c])
			{
				if(j == tar[t])
					continue;
				
				int v = type[par[0][j]];
				if(!mark[v])
				{
					mark[v] = i+1;
					q.push(v);
				}
				else if(mark[v] != i+1)
				{
					merge(i+1, mark[v]);
				}
			}
		}
	}
	
	int ans = 1;
	for(int i = 1; i <= n; i++)
	{
		for(int j : Adj[i])
			if(find(mark[type[i]]) != find(mark[type[j]]))
				deg[find(mark[type[i]])]++;
	}
	
	for(int i = 1; i <= k; i++)
		if(deg[i] == 1)
			ans++;
	
	std::cout << ans/2;
	std::cout << std::endl;
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 29 ms 23928 KB Output is correct
2 Correct 24 ms 24056 KB Output is correct
3 Correct 24 ms 24056 KB Output is correct
4 Correct 23 ms 23928 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23928 KB Output is correct
7 Correct 24 ms 23928 KB Output is correct
8 Correct 24 ms 23928 KB Output is correct
9 Correct 24 ms 23928 KB Output is correct
10 Correct 24 ms 23928 KB Output is correct
11 Correct 24 ms 24056 KB Output is correct
12 Correct 23 ms 23928 KB Output is correct
13 Correct 24 ms 23932 KB Output is correct
14 Correct 24 ms 24056 KB Output is correct
15 Correct 23 ms 24060 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 23 ms 23928 KB Output is correct
18 Correct 24 ms 24036 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 23 ms 23928 KB Output is correct
21 Correct 23 ms 23928 KB Output is correct
22 Correct 24 ms 24056 KB Output is correct
23 Correct 23 ms 23928 KB Output is correct
24 Correct 23 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 23928 KB Output is correct
2 Correct 24 ms 24056 KB Output is correct
3 Correct 24 ms 24056 KB Output is correct
4 Correct 23 ms 23928 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23928 KB Output is correct
7 Correct 24 ms 23928 KB Output is correct
8 Correct 24 ms 23928 KB Output is correct
9 Correct 24 ms 23928 KB Output is correct
10 Correct 24 ms 23928 KB Output is correct
11 Correct 24 ms 24056 KB Output is correct
12 Correct 23 ms 23928 KB Output is correct
13 Correct 24 ms 23932 KB Output is correct
14 Correct 24 ms 24056 KB Output is correct
15 Correct 23 ms 24060 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 23 ms 23928 KB Output is correct
18 Correct 24 ms 24036 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 23 ms 23928 KB Output is correct
21 Correct 23 ms 23928 KB Output is correct
22 Correct 24 ms 24056 KB Output is correct
23 Correct 23 ms 23928 KB Output is correct
24 Correct 23 ms 23928 KB Output is correct
25 Correct 23 ms 23928 KB Output is correct
26 Correct 30 ms 24232 KB Output is correct
27 Correct 29 ms 24184 KB Output is correct
28 Correct 29 ms 24440 KB Output is correct
29 Correct 30 ms 24312 KB Output is correct
30 Correct 28 ms 24184 KB Output is correct
31 Correct 24 ms 23928 KB Output is correct
32 Correct 29 ms 24440 KB Output is correct
33 Correct 25 ms 23928 KB Output is correct
34 Correct 28 ms 24184 KB Output is correct
35 Correct 29 ms 24312 KB Output is correct
36 Correct 29 ms 24192 KB Output is correct
37 Correct 32 ms 24312 KB Output is correct
38 Correct 24 ms 24056 KB Output is correct
39 Correct 29 ms 24184 KB Output is correct
40 Correct 28 ms 24184 KB Output is correct
41 Correct 28 ms 24184 KB Output is correct
42 Correct 28 ms 24184 KB Output is correct
43 Correct 28 ms 24312 KB Output is correct
44 Correct 24 ms 24056 KB Output is correct
45 Correct 29 ms 24440 KB Output is correct
46 Correct 29 ms 24312 KB Output is correct
47 Correct 23 ms 24056 KB Output is correct
48 Correct 29 ms 24156 KB Output is correct
49 Correct 28 ms 24312 KB Output is correct
50 Correct 29 ms 24440 KB Output is correct
51 Correct 28 ms 24184 KB Output is correct
52 Correct 28 ms 24184 KB Output is correct
53 Correct 29 ms 24184 KB Output is correct
54 Correct 29 ms 24184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 23928 KB Output is correct
2 Correct 24 ms 24056 KB Output is correct
3 Correct 24 ms 24056 KB Output is correct
4 Correct 23 ms 23928 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23928 KB Output is correct
7 Correct 24 ms 23928 KB Output is correct
8 Correct 24 ms 23928 KB Output is correct
9 Correct 24 ms 23928 KB Output is correct
10 Correct 24 ms 23928 KB Output is correct
11 Correct 24 ms 24056 KB Output is correct
12 Correct 23 ms 23928 KB Output is correct
13 Correct 24 ms 23932 KB Output is correct
14 Correct 24 ms 24056 KB Output is correct
15 Correct 23 ms 24060 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 23 ms 23928 KB Output is correct
18 Correct 24 ms 24036 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 23 ms 23928 KB Output is correct
21 Correct 23 ms 23928 KB Output is correct
22 Correct 24 ms 24056 KB Output is correct
23 Correct 23 ms 23928 KB Output is correct
24 Correct 23 ms 23928 KB Output is correct
25 Correct 23 ms 23928 KB Output is correct
26 Correct 210 ms 30352 KB Output is correct
27 Correct 224 ms 31144 KB Output is correct
28 Correct 28 ms 24184 KB Output is correct
29 Correct 24 ms 23928 KB Output is correct
30 Correct 24 ms 23928 KB Output is correct
31 Correct 249 ms 32028 KB Output is correct
32 Correct 28 ms 24188 KB Output is correct
33 Correct 282 ms 35552 KB Output is correct
34 Correct 242 ms 31224 KB Output is correct
35 Correct 28 ms 24184 KB Output is correct
36 Correct 264 ms 34296 KB Output is correct
37 Correct 28 ms 24184 KB Output is correct
38 Correct 28 ms 24184 KB Output is correct
39 Correct 212 ms 30064 KB Output is correct
40 Correct 29 ms 24312 KB Output is correct
41 Correct 249 ms 30968 KB Output is correct
42 Correct 279 ms 34680 KB Output is correct
43 Correct 23 ms 24056 KB Output is correct
44 Correct 279 ms 35496 KB Output is correct
45 Correct 334 ms 35196 KB Output is correct
46 Correct 34 ms 24184 KB Output is correct
47 Correct 29 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 213 ms 30204 KB Output is correct
2 Correct 275 ms 35052 KB Output is correct
3 Correct 29 ms 24312 KB Output is correct
4 Correct 28 ms 24184 KB Output is correct
5 Correct 24 ms 23928 KB Output is correct
6 Correct 27 ms 24056 KB Output is correct
7 Correct 28 ms 24184 KB Output is correct
8 Correct 305 ms 34268 KB Output is correct
9 Correct 28 ms 24184 KB Output is correct
10 Correct 273 ms 32248 KB Output is correct
11 Correct 23 ms 24056 KB Output is correct
12 Correct 258 ms 32888 KB Output is correct
13 Correct 303 ms 33816 KB Output is correct
14 Correct 285 ms 36216 KB Output is correct
15 Correct 228 ms 30064 KB Output is correct
16 Correct 29 ms 24184 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 287 ms 34416 KB Output is correct
19 Correct 306 ms 40008 KB Output is correct
20 Correct 30 ms 24312 KB Output is correct
21 Correct 23 ms 24056 KB Output is correct
22 Correct 256 ms 32112 KB Output is correct
23 Correct 29 ms 24184 KB Output is correct
24 Correct 291 ms 32476 KB Output is correct
25 Correct 328 ms 40088 KB Output is correct
26 Correct 29 ms 24316 KB Output is correct
27 Correct 29 ms 24312 KB Output is correct
28 Correct 29 ms 24184 KB Output is correct
29 Correct 28 ms 24184 KB Output is correct
30 Correct 29 ms 24184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 23928 KB Output is correct
2 Correct 24 ms 24056 KB Output is correct
3 Correct 24 ms 24056 KB Output is correct
4 Correct 23 ms 23928 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23928 KB Output is correct
7 Correct 24 ms 23928 KB Output is correct
8 Correct 24 ms 23928 KB Output is correct
9 Correct 24 ms 23928 KB Output is correct
10 Correct 24 ms 23928 KB Output is correct
11 Correct 24 ms 24056 KB Output is correct
12 Correct 23 ms 23928 KB Output is correct
13 Correct 24 ms 23932 KB Output is correct
14 Correct 24 ms 24056 KB Output is correct
15 Correct 23 ms 24060 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 23 ms 23928 KB Output is correct
18 Correct 24 ms 24036 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 23 ms 23928 KB Output is correct
21 Correct 23 ms 23928 KB Output is correct
22 Correct 24 ms 24056 KB Output is correct
23 Correct 23 ms 23928 KB Output is correct
24 Correct 23 ms 23928 KB Output is correct
25 Correct 23 ms 23928 KB Output is correct
26 Correct 30 ms 24232 KB Output is correct
27 Correct 29 ms 24184 KB Output is correct
28 Correct 29 ms 24440 KB Output is correct
29 Correct 30 ms 24312 KB Output is correct
30 Correct 28 ms 24184 KB Output is correct
31 Correct 24 ms 23928 KB Output is correct
32 Correct 29 ms 24440 KB Output is correct
33 Correct 25 ms 23928 KB Output is correct
34 Correct 28 ms 24184 KB Output is correct
35 Correct 29 ms 24312 KB Output is correct
36 Correct 29 ms 24192 KB Output is correct
37 Correct 32 ms 24312 KB Output is correct
38 Correct 24 ms 24056 KB Output is correct
39 Correct 29 ms 24184 KB Output is correct
40 Correct 28 ms 24184 KB Output is correct
41 Correct 28 ms 24184 KB Output is correct
42 Correct 28 ms 24184 KB Output is correct
43 Correct 28 ms 24312 KB Output is correct
44 Correct 24 ms 24056 KB Output is correct
45 Correct 29 ms 24440 KB Output is correct
46 Correct 29 ms 24312 KB Output is correct
47 Correct 23 ms 24056 KB Output is correct
48 Correct 29 ms 24156 KB Output is correct
49 Correct 28 ms 24312 KB Output is correct
50 Correct 29 ms 24440 KB Output is correct
51 Correct 28 ms 24184 KB Output is correct
52 Correct 28 ms 24184 KB Output is correct
53 Correct 29 ms 24184 KB Output is correct
54 Correct 29 ms 24184 KB Output is correct
55 Correct 23 ms 23928 KB Output is correct
56 Correct 210 ms 30352 KB Output is correct
57 Correct 224 ms 31144 KB Output is correct
58 Correct 28 ms 24184 KB Output is correct
59 Correct 24 ms 23928 KB Output is correct
60 Correct 24 ms 23928 KB Output is correct
61 Correct 249 ms 32028 KB Output is correct
62 Correct 28 ms 24188 KB Output is correct
63 Correct 282 ms 35552 KB Output is correct
64 Correct 242 ms 31224 KB Output is correct
65 Correct 28 ms 24184 KB Output is correct
66 Correct 264 ms 34296 KB Output is correct
67 Correct 28 ms 24184 KB Output is correct
68 Correct 28 ms 24184 KB Output is correct
69 Correct 212 ms 30064 KB Output is correct
70 Correct 29 ms 24312 KB Output is correct
71 Correct 249 ms 30968 KB Output is correct
72 Correct 279 ms 34680 KB Output is correct
73 Correct 23 ms 24056 KB Output is correct
74 Correct 279 ms 35496 KB Output is correct
75 Correct 334 ms 35196 KB Output is correct
76 Correct 34 ms 24184 KB Output is correct
77 Correct 29 ms 24312 KB Output is correct
78 Correct 213 ms 30204 KB Output is correct
79 Correct 275 ms 35052 KB Output is correct
80 Correct 29 ms 24312 KB Output is correct
81 Correct 28 ms 24184 KB Output is correct
82 Correct 24 ms 23928 KB Output is correct
83 Correct 27 ms 24056 KB Output is correct
84 Correct 28 ms 24184 KB Output is correct
85 Correct 305 ms 34268 KB Output is correct
86 Correct 28 ms 24184 KB Output is correct
87 Correct 273 ms 32248 KB Output is correct
88 Correct 23 ms 24056 KB Output is correct
89 Correct 258 ms 32888 KB Output is correct
90 Correct 303 ms 33816 KB Output is correct
91 Correct 285 ms 36216 KB Output is correct
92 Correct 228 ms 30064 KB Output is correct
93 Correct 29 ms 24184 KB Output is correct
94 Correct 24 ms 23928 KB Output is correct
95 Correct 287 ms 34416 KB Output is correct
96 Correct 306 ms 40008 KB Output is correct
97 Correct 30 ms 24312 KB Output is correct
98 Correct 23 ms 24056 KB Output is correct
99 Correct 256 ms 32112 KB Output is correct
100 Correct 29 ms 24184 KB Output is correct
101 Correct 291 ms 32476 KB Output is correct
102 Correct 328 ms 40088 KB Output is correct
103 Correct 29 ms 24316 KB Output is correct
104 Correct 29 ms 24312 KB Output is correct
105 Correct 29 ms 24184 KB Output is correct
106 Correct 28 ms 24184 KB Output is correct
107 Correct 29 ms 24184 KB Output is correct
108 Correct 1500 ms 62148 KB Output is correct
109 Correct 2001 ms 82892 KB Output is correct
110 Correct 1936 ms 84836 KB Output is correct
111 Correct 2033 ms 110124 KB Output is correct
112 Correct 2471 ms 108172 KB Output is correct
113 Correct 1781 ms 76072 KB Output is correct
114 Correct 1344 ms 60012 KB Output is correct
115 Correct 1368 ms 60004 KB Output is correct
116 Correct 1835 ms 68900 KB Output is correct
117 Correct 1691 ms 85120 KB Output is correct
118 Correct 1360 ms 64120 KB Output is correct
119 Correct 1696 ms 85140 KB Output is correct
120 Correct 2021 ms 108128 KB Output is correct
121 Correct 1703 ms 85180 KB Output is correct
122 Correct 1959 ms 74176 KB Output is correct
123 Correct 1394 ms 78772 KB Output is correct
124 Correct 1838 ms 96732 KB Output is correct