답안 #220622

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
220622 2020-04-08T09:38:47 Z atoiz Mergers (JOI19_mergers) C++14
0 / 100
95 ms 31724 KB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 500007;
int N, K, ans = 0;
int dsu[MAXN], group[MAXN], last[MAXN], val[MAXN], deg[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];
		deg[u] += (val[v] ? deg[v] : 1);
	}

	if (u == 1) ans += (deg[u] == 1);
	else ans += (deg[u] + !val[u] == 1);
}

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;

	dfs(1, 0);
	cout << (ans + 1) / 2 << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Incorrect 17 ms 23808 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Incorrect 17 ms 23808 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Incorrect 17 ms 23808 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 31724 KB Output is correct
2 Correct 86 ms 28604 KB Output is correct
3 Incorrect 27 ms 24076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Incorrect 17 ms 23808 KB Output isn't correct
3 Halted 0 ms 0 KB -