답안 #220617

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
220617 2020-04-08T09:12:39 Z atoiz Mergers (JOI19_mergers) C++14
10 / 100
111 ms 38520 KB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 500007;
int N, K, rt = 1, ans = 0;
int dsu[MAXN], group[MAXN], last[MAXN], val[MAXN];
vector<int> adj[MAXN], add[MAXN];

int root(int u) { return (dsu[u] != u ? dsu[u] = root(dsu[u]) : u); }

void dfs(int u, int p)
{
	dsu[u] = u;
	for (int v : add[u]) {
		if (dsu[v]) val[root(v)] -= 2;
	}

	for (int v : adj[u]) if (v != p) {
		dfs(v, u);
		dsu[v] = u;
		val[u] += val[v];
	}

	if (u != rt && !val[u]) ++val[u], ++ans;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> K;
	for (int i = 0; i < N - 1; ++i) {
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for (int u = 1; u <= N; ++u) {
		cin >> group[u];
		if (last[group[u]]) {
			int v = last[group[u]];
			add[u].push_back(v);
			add[v].push_back(u);
			++val[u], ++val[v];
		}
		last[group[u]] = u;
	}

	if (N == 2) return cout << K - 1 << endl, 0;

	for (rt = 1; adj[rt].size() == 1; ++rt);
	dfs(rt, 0);
	cout << (ans + 1) / 2 << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 23808 KB Output is correct
2 Correct 19 ms 23808 KB Output is correct
3 Correct 19 ms 23936 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 18 ms 23936 KB Output is correct
6 Correct 20 ms 23936 KB Output is correct
7 Correct 18 ms 23936 KB Output is correct
8 Correct 19 ms 23808 KB Output is correct
9 Correct 19 ms 23936 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 19 ms 23808 KB Output is correct
13 Correct 19 ms 23808 KB Output is correct
14 Correct 18 ms 23936 KB Output is correct
15 Correct 18 ms 23936 KB Output is correct
16 Correct 20 ms 23808 KB Output is correct
17 Correct 19 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 18 ms 23808 KB Output is correct
20 Correct 18 ms 23808 KB Output is correct
21 Correct 18 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 18 ms 23936 KB Output is correct
24 Correct 19 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 23808 KB Output is correct
2 Correct 19 ms 23808 KB Output is correct
3 Correct 19 ms 23936 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 18 ms 23936 KB Output is correct
6 Correct 20 ms 23936 KB Output is correct
7 Correct 18 ms 23936 KB Output is correct
8 Correct 19 ms 23808 KB Output is correct
9 Correct 19 ms 23936 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 19 ms 23808 KB Output is correct
13 Correct 19 ms 23808 KB Output is correct
14 Correct 18 ms 23936 KB Output is correct
15 Correct 18 ms 23936 KB Output is correct
16 Correct 20 ms 23808 KB Output is correct
17 Correct 19 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 18 ms 23808 KB Output is correct
20 Correct 18 ms 23808 KB Output is correct
21 Correct 18 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 18 ms 23936 KB Output is correct
24 Correct 19 ms 23808 KB Output is correct
25 Correct 19 ms 23808 KB Output is correct
26 Correct 20 ms 24064 KB Output is correct
27 Correct 20 ms 24064 KB Output is correct
28 Correct 21 ms 24064 KB Output is correct
29 Correct 21 ms 24064 KB Output is correct
30 Correct 20 ms 24064 KB Output is correct
31 Correct 19 ms 23808 KB Output is correct
32 Correct 20 ms 24192 KB Output is correct
33 Correct 18 ms 23808 KB Output is correct
34 Correct 20 ms 24064 KB Output is correct
35 Correct 21 ms 24064 KB Output is correct
36 Incorrect 20 ms 24064 KB Output isn't correct
37 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 23808 KB Output is correct
2 Correct 19 ms 23808 KB Output is correct
3 Correct 19 ms 23936 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 18 ms 23936 KB Output is correct
6 Correct 20 ms 23936 KB Output is correct
7 Correct 18 ms 23936 KB Output is correct
8 Correct 19 ms 23808 KB Output is correct
9 Correct 19 ms 23936 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 19 ms 23808 KB Output is correct
13 Correct 19 ms 23808 KB Output is correct
14 Correct 18 ms 23936 KB Output is correct
15 Correct 18 ms 23936 KB Output is correct
16 Correct 20 ms 23808 KB Output is correct
17 Correct 19 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 18 ms 23808 KB Output is correct
20 Correct 18 ms 23808 KB Output is correct
21 Correct 18 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 18 ms 23936 KB Output is correct
24 Correct 19 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 93 ms 33136 KB Output is correct
27 Correct 109 ms 32888 KB Output is correct
28 Correct 20 ms 24064 KB Output is correct
29 Correct 19 ms 23808 KB Output is correct
30 Correct 19 ms 23808 KB Output is correct
31 Correct 102 ms 32764 KB Output is correct
32 Correct 20 ms 24064 KB Output is correct
33 Correct 111 ms 38520 KB Output is correct
34 Correct 107 ms 32888 KB Output is correct
35 Incorrect 21 ms 24064 KB Output isn't correct
36 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 31728 KB Output is correct
2 Correct 78 ms 28912 KB Output is correct
3 Correct 20 ms 24064 KB Output is correct
4 Correct 22 ms 24064 KB Output is correct
5 Correct 19 ms 23936 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 20 ms 24064 KB Output is correct
8 Correct 104 ms 32760 KB Output is correct
9 Incorrect 21 ms 24064 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 23808 KB Output is correct
2 Correct 19 ms 23808 KB Output is correct
3 Correct 19 ms 23936 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 18 ms 23936 KB Output is correct
6 Correct 20 ms 23936 KB Output is correct
7 Correct 18 ms 23936 KB Output is correct
8 Correct 19 ms 23808 KB Output is correct
9 Correct 19 ms 23936 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 19 ms 23808 KB Output is correct
13 Correct 19 ms 23808 KB Output is correct
14 Correct 18 ms 23936 KB Output is correct
15 Correct 18 ms 23936 KB Output is correct
16 Correct 20 ms 23808 KB Output is correct
17 Correct 19 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 18 ms 23808 KB Output is correct
20 Correct 18 ms 23808 KB Output is correct
21 Correct 18 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 18 ms 23936 KB Output is correct
24 Correct 19 ms 23808 KB Output is correct
25 Correct 19 ms 23808 KB Output is correct
26 Correct 20 ms 24064 KB Output is correct
27 Correct 20 ms 24064 KB Output is correct
28 Correct 21 ms 24064 KB Output is correct
29 Correct 21 ms 24064 KB Output is correct
30 Correct 20 ms 24064 KB Output is correct
31 Correct 19 ms 23808 KB Output is correct
32 Correct 20 ms 24192 KB Output is correct
33 Correct 18 ms 23808 KB Output is correct
34 Correct 20 ms 24064 KB Output is correct
35 Correct 21 ms 24064 KB Output is correct
36 Incorrect 20 ms 24064 KB Output isn't correct
37 Halted 0 ms 0 KB -