Submission #106097

# Submission time Handle Problem Language Result Execution time Memory
106097 2019-04-16T13:57:13 Z mzhao Mergers (JOI19_mergers) C++11
34 / 100
124 ms 48888 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];

int cur[3100][3100];

void dfs(int n, int p, int d) {
	dep[n] = d;
	par[n] = p;
	for (int i = 1; i <= K; i++) cur[n][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[n][i] = cnt[i]-cur[n][i];
	if (n == 1) return;
	bool f = 1;
	for (int i = 1; i <= K; i++) if (cur[n][i] && cur[n][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 <= 3000);
	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:81: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:84: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:89:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &S[i]);
   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 12288 KB Output is correct
2 Correct 16 ms 12544 KB Output is correct
3 Correct 15 ms 12544 KB Output is correct
4 Correct 13 ms 12160 KB Output is correct
5 Correct 1 ms 12132 KB Output is correct
6 Correct 14 ms 12544 KB Output is correct
7 Correct 14 ms 12156 KB Output is correct
8 Correct 15 ms 12544 KB Output is correct
9 Correct 16 ms 12132 KB Output is correct
10 Correct 14 ms 12160 KB Output is correct
11 Correct 15 ms 12544 KB Output is correct
12 Correct 14 ms 12544 KB Output is correct
13 Correct 15 ms 12160 KB Output is correct
14 Correct 16 ms 12544 KB Output is correct
15 Correct 17 ms 12544 KB Output is correct
16 Correct 15 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 14 ms 12572 KB Output is correct
19 Correct 14 ms 12124 KB Output is correct
20 Correct 13 ms 12160 KB Output is correct
21 Correct 16 ms 12544 KB Output is correct
22 Correct 13 ms 12544 KB Output is correct
23 Correct 16 ms 12160 KB Output is correct
24 Correct 15 ms 12160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 12288 KB Output is correct
2 Correct 16 ms 12544 KB Output is correct
3 Correct 15 ms 12544 KB Output is correct
4 Correct 13 ms 12160 KB Output is correct
5 Correct 1 ms 12132 KB Output is correct
6 Correct 14 ms 12544 KB Output is correct
7 Correct 14 ms 12156 KB Output is correct
8 Correct 15 ms 12544 KB Output is correct
9 Correct 16 ms 12132 KB Output is correct
10 Correct 14 ms 12160 KB Output is correct
11 Correct 15 ms 12544 KB Output is correct
12 Correct 14 ms 12544 KB Output is correct
13 Correct 15 ms 12160 KB Output is correct
14 Correct 16 ms 12544 KB Output is correct
15 Correct 17 ms 12544 KB Output is correct
16 Correct 15 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 14 ms 12572 KB Output is correct
19 Correct 14 ms 12124 KB Output is correct
20 Correct 13 ms 12160 KB Output is correct
21 Correct 16 ms 12544 KB Output is correct
22 Correct 13 ms 12544 KB Output is correct
23 Correct 16 ms 12160 KB Output is correct
24 Correct 15 ms 12160 KB Output is correct
25 Correct 17 ms 12416 KB Output is correct
26 Correct 94 ms 47836 KB Output is correct
27 Correct 25 ms 24960 KB Output is correct
28 Correct 124 ms 48760 KB Output is correct
29 Correct 121 ms 48888 KB Output is correct
30 Correct 31 ms 26744 KB Output is correct
31 Correct 15 ms 12416 KB Output is correct
32 Correct 73 ms 48856 KB Output is correct
33 Correct 14 ms 12288 KB Output is correct
34 Correct 32 ms 24824 KB Output is correct
35 Correct 121 ms 48760 KB Output is correct
36 Correct 27 ms 24952 KB Output is correct
37 Correct 57 ms 41976 KB Output is correct
38 Correct 17 ms 12544 KB Output is correct
39 Correct 53 ms 41964 KB Output is correct
40 Correct 26 ms 24320 KB Output is correct
41 Correct 26 ms 24448 KB Output is correct
42 Correct 57 ms 41956 KB Output is correct
43 Correct 25 ms 24576 KB Output is correct
44 Correct 15 ms 12544 KB Output is correct
45 Correct 63 ms 48736 KB Output is correct
46 Correct 50 ms 41436 KB Output is correct
47 Correct 14 ms 12544 KB Output is correct
48 Correct 41 ms 31352 KB Output is correct
49 Correct 113 ms 48732 KB Output is correct
50 Correct 111 ms 48760 KB Output is correct
51 Correct 37 ms 31416 KB Output is correct
52 Correct 27 ms 24952 KB Output is correct
53 Correct 67 ms 41976 KB Output is correct
54 Correct 27 ms 24960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 12288 KB Output is correct
2 Correct 16 ms 12544 KB Output is correct
3 Correct 15 ms 12544 KB Output is correct
4 Correct 13 ms 12160 KB Output is correct
5 Correct 1 ms 12132 KB Output is correct
6 Correct 14 ms 12544 KB Output is correct
7 Correct 14 ms 12156 KB Output is correct
8 Correct 15 ms 12544 KB Output is correct
9 Correct 16 ms 12132 KB Output is correct
10 Correct 14 ms 12160 KB Output is correct
11 Correct 15 ms 12544 KB Output is correct
12 Correct 14 ms 12544 KB Output is correct
13 Correct 15 ms 12160 KB Output is correct
14 Correct 16 ms 12544 KB Output is correct
15 Correct 17 ms 12544 KB Output is correct
16 Correct 15 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 14 ms 12572 KB Output is correct
19 Correct 14 ms 12124 KB Output is correct
20 Correct 13 ms 12160 KB Output is correct
21 Correct 16 ms 12544 KB Output is correct
22 Correct 13 ms 12544 KB Output is correct
23 Correct 16 ms 12160 KB Output is correct
24 Correct 15 ms 12160 KB Output is correct
25 Correct 14 ms 12416 KB Output is correct
26 Runtime error 33 ms 24184 KB Execution killed with signal 11 (could be triggered by violating memory limits)
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 28 ms 23960 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 12288 KB Output is correct
2 Correct 16 ms 12544 KB Output is correct
3 Correct 15 ms 12544 KB Output is correct
4 Correct 13 ms 12160 KB Output is correct
5 Correct 1 ms 12132 KB Output is correct
6 Correct 14 ms 12544 KB Output is correct
7 Correct 14 ms 12156 KB Output is correct
8 Correct 15 ms 12544 KB Output is correct
9 Correct 16 ms 12132 KB Output is correct
10 Correct 14 ms 12160 KB Output is correct
11 Correct 15 ms 12544 KB Output is correct
12 Correct 14 ms 12544 KB Output is correct
13 Correct 15 ms 12160 KB Output is correct
14 Correct 16 ms 12544 KB Output is correct
15 Correct 17 ms 12544 KB Output is correct
16 Correct 15 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 14 ms 12572 KB Output is correct
19 Correct 14 ms 12124 KB Output is correct
20 Correct 13 ms 12160 KB Output is correct
21 Correct 16 ms 12544 KB Output is correct
22 Correct 13 ms 12544 KB Output is correct
23 Correct 16 ms 12160 KB Output is correct
24 Correct 15 ms 12160 KB Output is correct
25 Correct 17 ms 12416 KB Output is correct
26 Correct 94 ms 47836 KB Output is correct
27 Correct 25 ms 24960 KB Output is correct
28 Correct 124 ms 48760 KB Output is correct
29 Correct 121 ms 48888 KB Output is correct
30 Correct 31 ms 26744 KB Output is correct
31 Correct 15 ms 12416 KB Output is correct
32 Correct 73 ms 48856 KB Output is correct
33 Correct 14 ms 12288 KB Output is correct
34 Correct 32 ms 24824 KB Output is correct
35 Correct 121 ms 48760 KB Output is correct
36 Correct 27 ms 24952 KB Output is correct
37 Correct 57 ms 41976 KB Output is correct
38 Correct 17 ms 12544 KB Output is correct
39 Correct 53 ms 41964 KB Output is correct
40 Correct 26 ms 24320 KB Output is correct
41 Correct 26 ms 24448 KB Output is correct
42 Correct 57 ms 41956 KB Output is correct
43 Correct 25 ms 24576 KB Output is correct
44 Correct 15 ms 12544 KB Output is correct
45 Correct 63 ms 48736 KB Output is correct
46 Correct 50 ms 41436 KB Output is correct
47 Correct 14 ms 12544 KB Output is correct
48 Correct 41 ms 31352 KB Output is correct
49 Correct 113 ms 48732 KB Output is correct
50 Correct 111 ms 48760 KB Output is correct
51 Correct 37 ms 31416 KB Output is correct
52 Correct 27 ms 24952 KB Output is correct
53 Correct 67 ms 41976 KB Output is correct
54 Correct 27 ms 24960 KB Output is correct
55 Correct 14 ms 12416 KB Output is correct
56 Runtime error 33 ms 24184 KB Execution killed with signal 11 (could be triggered by violating memory limits)
57 Halted 0 ms 0 KB -