Submission #138057

# Submission time Handle Problem Language Result Execution time Memory
138057 2019-07-29T07:53:57 Z anayk Mergers (JOI19_mergers) C++14
24 / 100
349 ms 41080 KB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

#define MAXN 500005
#define LOGM 20

int par[LOGM][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;
int deg[MAXN];

void dfs(int u, int p = 0)
{
	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])
			par[i][u] = par[i-1][par[i-1][u]];
	}
	
	for(int v : Adj[u])
	{
		if(v == p)
			continue;
		
		dfs(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);
	}
	
	dfs(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::cout << "lca: " << tar[i+1] << std::endl;
	}
	
	std::sort(ord, ord+k);
	
	for(int i = 0; i < k; i++)
	{
		int t = ord[i].second;
		//std::cout << "t: " << ord[i].second << std::endl;
		if(mark[t])
			continue;
				q.push(t);
		mark[t] = i+1;
		//std::cout << "q: ";
		while(!q.empty())
		{
			int c = q.front(); q.pop();
			//std::cout << c << " ";
			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);
				}
			}
		}
		//std::cout << std::endl;
	}
	
	int ans = 1;
	for(int i = 1; i <= n; i++)
	{
		for(int j : Adj[i])
			if(mark[type[i]] != mark[type[j]])
				deg[mark[type[i]]]++;
	}
	
	for(int i = 1; i <= k; i++)
		if(deg[i] == 1)
			ans++;
	
	std::cout << ans/2;
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 24 ms 23932 KB Output is correct
3 Correct 24 ms 23932 KB Output is correct
4 Correct 23 ms 23928 KB Output is correct
5 Correct 24 ms 23928 KB Output is correct
6 Correct 24 ms 24056 KB Output is correct
7 Correct 23 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 29 ms 23900 KB Output is correct
12 Correct 23 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 23 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 24 ms 23932 KB Output is correct
19 Correct 25 ms 23928 KB Output is correct
20 Correct 24 ms 23932 KB Output is correct
21 Correct 25 ms 23928 KB Output is correct
22 Correct 24 ms 23928 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 24 ms 23928 KB Output is correct
2 Correct 24 ms 23932 KB Output is correct
3 Correct 24 ms 23932 KB Output is correct
4 Correct 23 ms 23928 KB Output is correct
5 Correct 24 ms 23928 KB Output is correct
6 Correct 24 ms 24056 KB Output is correct
7 Correct 23 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 29 ms 23900 KB Output is correct
12 Correct 23 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 23 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 24 ms 23932 KB Output is correct
19 Correct 25 ms 23928 KB Output is correct
20 Correct 24 ms 23932 KB Output is correct
21 Correct 25 ms 23928 KB Output is correct
22 Correct 24 ms 23928 KB Output is correct
23 Correct 23 ms 23928 KB Output is correct
24 Correct 23 ms 23928 KB Output is correct
25 Correct 24 ms 23928 KB Output is correct
26 Incorrect 29 ms 24312 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 24 ms 23932 KB Output is correct
3 Correct 24 ms 23932 KB Output is correct
4 Correct 23 ms 23928 KB Output is correct
5 Correct 24 ms 23928 KB Output is correct
6 Correct 24 ms 24056 KB Output is correct
7 Correct 23 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 29 ms 23900 KB Output is correct
12 Correct 23 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 23 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 24 ms 23932 KB Output is correct
19 Correct 25 ms 23928 KB Output is correct
20 Correct 24 ms 23932 KB Output is correct
21 Correct 25 ms 23928 KB Output is correct
22 Correct 24 ms 23928 KB Output is correct
23 Correct 23 ms 23928 KB Output is correct
24 Correct 23 ms 23928 KB Output is correct
25 Correct 24 ms 23928 KB Output is correct
26 Correct 203 ms 31064 KB Output is correct
27 Correct 237 ms 31992 KB Output is correct
28 Correct 29 ms 24312 KB Output is correct
29 Correct 24 ms 23928 KB Output is correct
30 Correct 24 ms 24056 KB Output is correct
31 Correct 241 ms 32888 KB Output is correct
32 Correct 29 ms 24184 KB Output is correct
33 Correct 281 ms 40796 KB Output is correct
34 Correct 241 ms 32376 KB Output is correct
35 Correct 30 ms 24184 KB Output is correct
36 Correct 269 ms 35640 KB Output is correct
37 Correct 33 ms 24184 KB Output is correct
38 Correct 31 ms 24312 KB Output is correct
39 Correct 200 ms 31124 KB Output is correct
40 Correct 29 ms 24440 KB Output is correct
41 Correct 225 ms 31988 KB Output is correct
42 Correct 280 ms 37112 KB Output is correct
43 Correct 24 ms 23928 KB Output is correct
44 Correct 349 ms 41080 KB Output is correct
45 Correct 283 ms 38392 KB Output is correct
46 Correct 28 ms 24184 KB Output is correct
47 Correct 29 ms 24184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 29940 KB Output is correct
2 Correct 246 ms 34484 KB Output is correct
3 Incorrect 29 ms 24184 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 24 ms 23932 KB Output is correct
3 Correct 24 ms 23932 KB Output is correct
4 Correct 23 ms 23928 KB Output is correct
5 Correct 24 ms 23928 KB Output is correct
6 Correct 24 ms 24056 KB Output is correct
7 Correct 23 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 29 ms 23900 KB Output is correct
12 Correct 23 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 23 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 24 ms 23932 KB Output is correct
19 Correct 25 ms 23928 KB Output is correct
20 Correct 24 ms 23932 KB Output is correct
21 Correct 25 ms 23928 KB Output is correct
22 Correct 24 ms 23928 KB Output is correct
23 Correct 23 ms 23928 KB Output is correct
24 Correct 23 ms 23928 KB Output is correct
25 Correct 24 ms 23928 KB Output is correct
26 Incorrect 29 ms 24312 KB Output isn't correct
27 Halted 0 ms 0 KB -