답안 #138064

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
138064 2019-07-29T09:08:31 Z anayk Mergers (JOI19_mergers) C++14
24 / 100
308 ms 35540 KB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>

#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;
std::stack<std::pair<int, int> > s;
int deg[MAXN];

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 = 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);
				}
			}
		}
	}
	
	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;
	std::cout << std::endl;
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 24 ms 23928 KB Output is correct
3 Correct 23 ms 23944 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 23928 KB Output is correct
7 Correct 23 ms 23872 KB Output is correct
8 Correct 23 ms 23928 KB Output is correct
9 Correct 23 ms 23928 KB Output is correct
10 Correct 23 ms 23928 KB Output is correct
11 Correct 23 ms 23928 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 23952 KB Output is correct
17 Correct 23 ms 23928 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23928 KB Output is correct
20 Correct 23 ms 23928 KB Output is correct
21 Correct 24 ms 23928 KB Output is correct
22 Correct 29 ms 23928 KB Output is correct
23 Correct 24 ms 23928 KB Output is correct
24 Correct 23 ms 23928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 24 ms 23928 KB Output is correct
3 Correct 23 ms 23944 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 23928 KB Output is correct
7 Correct 23 ms 23872 KB Output is correct
8 Correct 23 ms 23928 KB Output is correct
9 Correct 23 ms 23928 KB Output is correct
10 Correct 23 ms 23928 KB Output is correct
11 Correct 23 ms 23928 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 23952 KB Output is correct
17 Correct 23 ms 23928 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23928 KB Output is correct
20 Correct 23 ms 23928 KB Output is correct
21 Correct 24 ms 23928 KB Output is correct
22 Correct 29 ms 23928 KB Output is correct
23 Correct 24 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 -
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 24 ms 23928 KB Output is correct
3 Correct 23 ms 23944 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 23928 KB Output is correct
7 Correct 23 ms 23872 KB Output is correct
8 Correct 23 ms 23928 KB Output is correct
9 Correct 23 ms 23928 KB Output is correct
10 Correct 23 ms 23928 KB Output is correct
11 Correct 23 ms 23928 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 23952 KB Output is correct
17 Correct 23 ms 23928 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23928 KB Output is correct
20 Correct 23 ms 23928 KB Output is correct
21 Correct 24 ms 23928 KB Output is correct
22 Correct 29 ms 23928 KB Output is correct
23 Correct 24 ms 23928 KB Output is correct
24 Correct 23 ms 23928 KB Output is correct
25 Correct 29 ms 23900 KB Output is correct
26 Correct 196 ms 30240 KB Output is correct
27 Correct 229 ms 30968 KB Output is correct
28 Correct 29 ms 24184 KB Output is correct
29 Correct 24 ms 23928 KB Output is correct
30 Correct 29 ms 23928 KB Output is correct
31 Correct 229 ms 31992 KB Output is correct
32 Correct 28 ms 24180 KB Output is correct
33 Correct 308 ms 35540 KB Output is correct
34 Correct 239 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 24188 KB Output is correct
38 Correct 28 ms 24184 KB Output is correct
39 Correct 200 ms 30068 KB Output is correct
40 Correct 28 ms 24312 KB Output is correct
41 Correct 219 ms 30840 KB Output is correct
42 Correct 266 ms 34812 KB Output is correct
43 Correct 23 ms 23928 KB Output is correct
44 Correct 276 ms 35420 KB Output is correct
45 Correct 272 ms 35196 KB Output is correct
46 Correct 28 ms 24060 KB Output is correct
47 Correct 28 ms 24184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 194 ms 30296 KB Output is correct
2 Correct 238 ms 34556 KB Output is correct
3 Incorrect 29 ms 24184 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 24 ms 23928 KB Output is correct
3 Correct 23 ms 23944 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 23928 KB Output is correct
7 Correct 23 ms 23872 KB Output is correct
8 Correct 23 ms 23928 KB Output is correct
9 Correct 23 ms 23928 KB Output is correct
10 Correct 23 ms 23928 KB Output is correct
11 Correct 23 ms 23928 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 23952 KB Output is correct
17 Correct 23 ms 23928 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23928 KB Output is correct
20 Correct 23 ms 23928 KB Output is correct
21 Correct 24 ms 23928 KB Output is correct
22 Correct 29 ms 23928 KB Output is correct
23 Correct 24 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 -