답안 #106096

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
106096 2019-04-16T13:54:32 Z mzhao Mergers (JOI19_mergers) C++11
10 / 100
33 ms 24064 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#define D(x...) printf(x)
#else
#define D(x...)
#endif

#define MN 500100
#define x first
#define y second

int N, K, S[MN];
vector<int> adj[MN];

int tot[MN], cnt[MN], ans, dep[MN], par[MN];
bool bad[MN];

void dfs(int n, int p, int d) {
	dep[n] = d;
	par[n] = p;
	int cur[8];
	for (int i = 1; i <= K; i++) cur[i] = cnt[i];
	cnt[S[n]]++;
	for (int i : adj[n]) if (i != p) {
		dfs(i, n, d+1);
	}
	for (int i = 1; i <= K; i++) cur[i] = cnt[i]-cur[i];
	if (n == 1) return;
	bool f = 1;
	for (int i = 1; i <= K; i++) if (cur[i] && cur[i] != tot[i]) {
		f = 0;
		break;
	}
	bad[n] = f;
}

pair<int, int> dp[MN];
pair<int, int> bpos;
int bval;

void dfs2(int n, int p) {
	pair<int, int> b1 = {0, 0}, b2 = {0, 0};
	for (int i : adj[n]) if (i != p) {
		dfs2(i, n);
		if (dp[i].x >= b1.x) {
			b2 = b1;
			b1 = dp[i];
		} else if (dp[i].x >= b2.x) {
			b2 = dp[i];
		}
	}
	if (b2.y && b1.x+b2.x > bval) {
		bval = b1.x+b2.x;
		bpos = {b1.y, b2.y};
	}
	if (b1.x == 0 && bad[n]) {
		b1 = {1, n};
	} else if (bad[n]) {
		b1.x++;
	}
	dp[n] = b1;
}

void rem() {
	int a = bpos.x, b = bpos.y;
	if (dep[a] < dep[b]) swap(a, b);
	while (dep[a] > dep[b]) {
		bad[a] = 0;
		a = par[a];
	}
	while (a != b) {
		bad[a] = bad[b] = 0;
		a = par[a]; b = par[b];
	}
}

int main() {
	scanf("%d%d", &N, &K);
	assert(N <= 100);
	for (int i = 1, A, B; i < N; i++) {
		scanf("%d%d", &A, &B);
		adj[A].push_back(B);
		adj[B].push_back(A);
	}
	for (int i = 1; i <= N; i++) {
		scanf("%d", &S[i]);
		tot[S[i]]++;
	}
	dfs(1, 0, 0);
	while (1) {
		for (int j = 1; j <= N; j++) dp[j] = {0, 0};
		bval = 0;
		bpos = {0, 0};
		dfs2(1, 0);
		D("%d %d %d\n", bval, bpos.x, bpos.y);
		if (!bval) {
			if (dp[1].x) ans++;
			break;
		} else {
			rem();
			ans++;
		}
	}
	printf("%d\n", ans);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &K);
  ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &A, &B);
   ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &S[i]);
   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 14 ms 12160 KB Output is correct
3 Correct 13 ms 12288 KB Output is correct
4 Correct 11 ms 12160 KB Output is correct
5 Correct 15 ms 12160 KB Output is correct
6 Correct 14 ms 12160 KB Output is correct
7 Correct 14 ms 12160 KB Output is correct
8 Correct 13 ms 12160 KB Output is correct
9 Correct 13 ms 12160 KB Output is correct
10 Correct 14 ms 12160 KB Output is correct
11 Correct 13 ms 12160 KB Output is correct
12 Correct 13 ms 12160 KB Output is correct
13 Correct 15 ms 12160 KB Output is correct
14 Correct 15 ms 12280 KB Output is correct
15 Correct 14 ms 12160 KB Output is correct
16 Correct 14 ms 12160 KB Output is correct
17 Correct 16 ms 12160 KB Output is correct
18 Correct 13 ms 12160 KB Output is correct
19 Correct 14 ms 12160 KB Output is correct
20 Correct 13 ms 12280 KB Output is correct
21 Correct 14 ms 12160 KB Output is correct
22 Correct 14 ms 12132 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 13 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 14 ms 12160 KB Output is correct
3 Correct 13 ms 12288 KB Output is correct
4 Correct 11 ms 12160 KB Output is correct
5 Correct 15 ms 12160 KB Output is correct
6 Correct 14 ms 12160 KB Output is correct
7 Correct 14 ms 12160 KB Output is correct
8 Correct 13 ms 12160 KB Output is correct
9 Correct 13 ms 12160 KB Output is correct
10 Correct 14 ms 12160 KB Output is correct
11 Correct 13 ms 12160 KB Output is correct
12 Correct 13 ms 12160 KB Output is correct
13 Correct 15 ms 12160 KB Output is correct
14 Correct 15 ms 12280 KB Output is correct
15 Correct 14 ms 12160 KB Output is correct
16 Correct 14 ms 12160 KB Output is correct
17 Correct 16 ms 12160 KB Output is correct
18 Correct 13 ms 12160 KB Output is correct
19 Correct 14 ms 12160 KB Output is correct
20 Correct 13 ms 12280 KB Output is correct
21 Correct 14 ms 12160 KB Output is correct
22 Correct 14 ms 12132 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 13 ms 12160 KB Output is correct
25 Runtime error 33 ms 24056 KB Execution killed with signal 11 (could be triggered by violating memory limits)
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 14 ms 12160 KB Output is correct
3 Correct 13 ms 12288 KB Output is correct
4 Correct 11 ms 12160 KB Output is correct
5 Correct 15 ms 12160 KB Output is correct
6 Correct 14 ms 12160 KB Output is correct
7 Correct 14 ms 12160 KB Output is correct
8 Correct 13 ms 12160 KB Output is correct
9 Correct 13 ms 12160 KB Output is correct
10 Correct 14 ms 12160 KB Output is correct
11 Correct 13 ms 12160 KB Output is correct
12 Correct 13 ms 12160 KB Output is correct
13 Correct 15 ms 12160 KB Output is correct
14 Correct 15 ms 12280 KB Output is correct
15 Correct 14 ms 12160 KB Output is correct
16 Correct 14 ms 12160 KB Output is correct
17 Correct 16 ms 12160 KB Output is correct
18 Correct 13 ms 12160 KB Output is correct
19 Correct 14 ms 12160 KB Output is correct
20 Correct 13 ms 12280 KB Output is correct
21 Correct 14 ms 12160 KB Output is correct
22 Correct 14 ms 12132 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 13 ms 12160 KB Output is correct
25 Runtime error 25 ms 24064 KB Execution killed with signal 11 (could be triggered by violating memory limits)
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 25 ms 24064 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 14 ms 12160 KB Output is correct
3 Correct 13 ms 12288 KB Output is correct
4 Correct 11 ms 12160 KB Output is correct
5 Correct 15 ms 12160 KB Output is correct
6 Correct 14 ms 12160 KB Output is correct
7 Correct 14 ms 12160 KB Output is correct
8 Correct 13 ms 12160 KB Output is correct
9 Correct 13 ms 12160 KB Output is correct
10 Correct 14 ms 12160 KB Output is correct
11 Correct 13 ms 12160 KB Output is correct
12 Correct 13 ms 12160 KB Output is correct
13 Correct 15 ms 12160 KB Output is correct
14 Correct 15 ms 12280 KB Output is correct
15 Correct 14 ms 12160 KB Output is correct
16 Correct 14 ms 12160 KB Output is correct
17 Correct 16 ms 12160 KB Output is correct
18 Correct 13 ms 12160 KB Output is correct
19 Correct 14 ms 12160 KB Output is correct
20 Correct 13 ms 12280 KB Output is correct
21 Correct 14 ms 12160 KB Output is correct
22 Correct 14 ms 12132 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 13 ms 12160 KB Output is correct
25 Runtime error 33 ms 24056 KB Execution killed with signal 11 (could be triggered by violating memory limits)
26 Halted 0 ms 0 KB -