답안 #138077

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
138077 2019-07-29T09:43:02 Z anayk Mergers (JOI19_mergers) C++14
100 / 100
2197 ms 110844 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;
	
	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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 24 ms 23960 KB Output is correct
4 Correct 24 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 24 ms 23980 KB Output is correct
8 Correct 24 ms 24060 KB Output is correct
9 Correct 23 ms 23932 KB Output is correct
10 Correct 28 ms 23928 KB Output is correct
11 Correct 28 ms 24056 KB Output is correct
12 Correct 29 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 24 ms 23932 KB Output is correct
15 Correct 24 ms 24056 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 28 ms 23928 KB Output is correct
18 Correct 24 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 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
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 24 ms 23960 KB Output is correct
4 Correct 24 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 24 ms 23980 KB Output is correct
8 Correct 24 ms 24060 KB Output is correct
9 Correct 23 ms 23932 KB Output is correct
10 Correct 28 ms 23928 KB Output is correct
11 Correct 28 ms 24056 KB Output is correct
12 Correct 29 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 24 ms 23932 KB Output is correct
15 Correct 24 ms 24056 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 28 ms 23928 KB Output is correct
18 Correct 24 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 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 29 ms 24312 KB Output is correct
27 Correct 28 ms 24312 KB Output is correct
28 Correct 30 ms 24440 KB Output is correct
29 Correct 29 ms 24440 KB Output is correct
30 Correct 29 ms 24312 KB Output is correct
31 Correct 24 ms 23928 KB Output is correct
32 Correct 30 ms 24440 KB Output is correct
33 Correct 24 ms 23928 KB Output is correct
34 Correct 28 ms 24156 KB Output is correct
35 Correct 47 ms 24312 KB Output is correct
36 Correct 29 ms 24312 KB Output is correct
37 Correct 29 ms 24312 KB Output is correct
38 Correct 28 ms 23928 KB Output is correct
39 Correct 32 ms 24312 KB Output is correct
40 Correct 34 ms 24316 KB Output is correct
41 Correct 29 ms 24312 KB Output is correct
42 Correct 29 ms 24312 KB Output is correct
43 Correct 29 ms 24316 KB Output is correct
44 Correct 24 ms 24056 KB Output is correct
45 Correct 29 ms 24440 KB Output is correct
46 Correct 30 ms 24260 KB Output is correct
47 Correct 24 ms 24056 KB Output is correct
48 Correct 29 ms 24312 KB Output is correct
49 Correct 29 ms 24312 KB Output is correct
50 Correct 29 ms 24440 KB Output is correct
51 Correct 29 ms 24312 KB Output is correct
52 Correct 28 ms 24184 KB Output is correct
53 Correct 30 ms 24184 KB Output is correct
54 Correct 29 ms 24364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 24 ms 23960 KB Output is correct
4 Correct 24 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 24 ms 23980 KB Output is correct
8 Correct 24 ms 24060 KB Output is correct
9 Correct 23 ms 23932 KB Output is correct
10 Correct 28 ms 23928 KB Output is correct
11 Correct 28 ms 24056 KB Output is correct
12 Correct 29 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 24 ms 23932 KB Output is correct
15 Correct 24 ms 24056 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 28 ms 23928 KB Output is correct
18 Correct 24 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 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 24056 KB Output is correct
26 Correct 209 ms 30320 KB Output is correct
27 Correct 230 ms 31072 KB Output is correct
28 Correct 29 ms 24252 KB Output is correct
29 Correct 24 ms 24056 KB Output is correct
30 Correct 23 ms 23928 KB Output is correct
31 Correct 233 ms 31864 KB Output is correct
32 Correct 28 ms 24184 KB Output is correct
33 Correct 297 ms 35540 KB Output is correct
34 Correct 252 ms 31352 KB Output is correct
35 Correct 33 ms 24184 KB Output is correct
36 Correct 283 ms 34296 KB Output is correct
37 Correct 29 ms 24184 KB Output is correct
38 Correct 31 ms 24184 KB Output is correct
39 Correct 212 ms 30260 KB Output is correct
40 Correct 34 ms 24316 KB Output is correct
41 Correct 255 ms 31092 KB Output is correct
42 Correct 283 ms 34680 KB Output is correct
43 Correct 29 ms 23928 KB Output is correct
44 Correct 343 ms 35420 KB Output is correct
45 Correct 317 ms 35068 KB Output is correct
46 Correct 32 ms 24184 KB Output is correct
47 Correct 29 ms 24184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 208 ms 30192 KB Output is correct
2 Correct 264 ms 35016 KB Output is correct
3 Correct 29 ms 24312 KB Output is correct
4 Correct 28 ms 24312 KB Output is correct
5 Correct 24 ms 23928 KB Output is correct
6 Correct 24 ms 23932 KB Output is correct
7 Correct 31 ms 24176 KB Output is correct
8 Correct 325 ms 35320 KB Output is correct
9 Correct 29 ms 24184 KB Output is correct
10 Correct 246 ms 33272 KB Output is correct
11 Correct 24 ms 24056 KB Output is correct
12 Correct 260 ms 33784 KB Output is correct
13 Correct 304 ms 34952 KB Output is correct
14 Correct 308 ms 37084 KB Output is correct
15 Correct 209 ms 31084 KB Output is correct
16 Correct 34 ms 24312 KB Output is correct
17 Correct 29 ms 24056 KB Output is correct
18 Correct 316 ms 35276 KB Output is correct
19 Correct 366 ms 41080 KB Output is correct
20 Correct 34 ms 24312 KB Output is correct
21 Correct 28 ms 24056 KB Output is correct
22 Correct 286 ms 33412 KB Output is correct
23 Correct 29 ms 24184 KB Output is correct
24 Correct 275 ms 33472 KB Output is correct
25 Correct 365 ms 41080 KB Output is correct
26 Correct 29 ms 24312 KB Output is correct
27 Correct 29 ms 24440 KB Output is correct
28 Correct 30 ms 24316 KB Output is correct
29 Correct 28 ms 24184 KB Output is correct
30 Correct 30 ms 24316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 24 ms 23960 KB Output is correct
4 Correct 24 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 24 ms 23980 KB Output is correct
8 Correct 24 ms 24060 KB Output is correct
9 Correct 23 ms 23932 KB Output is correct
10 Correct 28 ms 23928 KB Output is correct
11 Correct 28 ms 24056 KB Output is correct
12 Correct 29 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 24 ms 23932 KB Output is correct
15 Correct 24 ms 24056 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 28 ms 23928 KB Output is correct
18 Correct 24 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 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 29 ms 24312 KB Output is correct
27 Correct 28 ms 24312 KB Output is correct
28 Correct 30 ms 24440 KB Output is correct
29 Correct 29 ms 24440 KB Output is correct
30 Correct 29 ms 24312 KB Output is correct
31 Correct 24 ms 23928 KB Output is correct
32 Correct 30 ms 24440 KB Output is correct
33 Correct 24 ms 23928 KB Output is correct
34 Correct 28 ms 24156 KB Output is correct
35 Correct 47 ms 24312 KB Output is correct
36 Correct 29 ms 24312 KB Output is correct
37 Correct 29 ms 24312 KB Output is correct
38 Correct 28 ms 23928 KB Output is correct
39 Correct 32 ms 24312 KB Output is correct
40 Correct 34 ms 24316 KB Output is correct
41 Correct 29 ms 24312 KB Output is correct
42 Correct 29 ms 24312 KB Output is correct
43 Correct 29 ms 24316 KB Output is correct
44 Correct 24 ms 24056 KB Output is correct
45 Correct 29 ms 24440 KB Output is correct
46 Correct 30 ms 24260 KB Output is correct
47 Correct 24 ms 24056 KB Output is correct
48 Correct 29 ms 24312 KB Output is correct
49 Correct 29 ms 24312 KB Output is correct
50 Correct 29 ms 24440 KB Output is correct
51 Correct 29 ms 24312 KB Output is correct
52 Correct 28 ms 24184 KB Output is correct
53 Correct 30 ms 24184 KB Output is correct
54 Correct 29 ms 24364 KB Output is correct
55 Correct 24 ms 24056 KB Output is correct
56 Correct 209 ms 30320 KB Output is correct
57 Correct 230 ms 31072 KB Output is correct
58 Correct 29 ms 24252 KB Output is correct
59 Correct 24 ms 24056 KB Output is correct
60 Correct 23 ms 23928 KB Output is correct
61 Correct 233 ms 31864 KB Output is correct
62 Correct 28 ms 24184 KB Output is correct
63 Correct 297 ms 35540 KB Output is correct
64 Correct 252 ms 31352 KB Output is correct
65 Correct 33 ms 24184 KB Output is correct
66 Correct 283 ms 34296 KB Output is correct
67 Correct 29 ms 24184 KB Output is correct
68 Correct 31 ms 24184 KB Output is correct
69 Correct 212 ms 30260 KB Output is correct
70 Correct 34 ms 24316 KB Output is correct
71 Correct 255 ms 31092 KB Output is correct
72 Correct 283 ms 34680 KB Output is correct
73 Correct 29 ms 23928 KB Output is correct
74 Correct 343 ms 35420 KB Output is correct
75 Correct 317 ms 35068 KB Output is correct
76 Correct 32 ms 24184 KB Output is correct
77 Correct 29 ms 24184 KB Output is correct
78 Correct 208 ms 30192 KB Output is correct
79 Correct 264 ms 35016 KB Output is correct
80 Correct 29 ms 24312 KB Output is correct
81 Correct 28 ms 24312 KB Output is correct
82 Correct 24 ms 23928 KB Output is correct
83 Correct 24 ms 23932 KB Output is correct
84 Correct 31 ms 24176 KB Output is correct
85 Correct 325 ms 35320 KB Output is correct
86 Correct 29 ms 24184 KB Output is correct
87 Correct 246 ms 33272 KB Output is correct
88 Correct 24 ms 24056 KB Output is correct
89 Correct 260 ms 33784 KB Output is correct
90 Correct 304 ms 34952 KB Output is correct
91 Correct 308 ms 37084 KB Output is correct
92 Correct 209 ms 31084 KB Output is correct
93 Correct 34 ms 24312 KB Output is correct
94 Correct 29 ms 24056 KB Output is correct
95 Correct 316 ms 35276 KB Output is correct
96 Correct 366 ms 41080 KB Output is correct
97 Correct 34 ms 24312 KB Output is correct
98 Correct 28 ms 24056 KB Output is correct
99 Correct 286 ms 33412 KB Output is correct
100 Correct 29 ms 24184 KB Output is correct
101 Correct 275 ms 33472 KB Output is correct
102 Correct 365 ms 41080 KB Output is correct
103 Correct 29 ms 24312 KB Output is correct
104 Correct 29 ms 24440 KB Output is correct
105 Correct 30 ms 24316 KB Output is correct
106 Correct 28 ms 24184 KB Output is correct
107 Correct 30 ms 24316 KB Output is correct
108 Correct 1561 ms 64936 KB Output is correct
109 Correct 1870 ms 83980 KB Output is correct
110 Correct 1908 ms 86016 KB Output is correct
111 Correct 2173 ms 110844 KB Output is correct
112 Correct 2197 ms 109196 KB Output is correct
113 Correct 1673 ms 77248 KB Output is correct
114 Correct 1306 ms 61032 KB Output is correct
115 Correct 1273 ms 60792 KB Output is correct
116 Correct 1924 ms 69740 KB Output is correct
117 Correct 1685 ms 85988 KB Output is correct
118 Correct 1418 ms 65068 KB Output is correct
119 Correct 1882 ms 86080 KB Output is correct
120 Correct 1983 ms 108916 KB Output is correct
121 Correct 2067 ms 86088 KB Output is correct
122 Correct 2043 ms 74984 KB Output is correct
123 Correct 1391 ms 79536 KB Output is correct
124 Correct 1950 ms 97524 KB Output is correct