Submission #529052

# Submission time Handle Problem Language Result Execution time Memory
529052 2022-02-22T04:01:39 Z d2k05 Power Plant (JOI20_power) C++14
47 / 100
1500 ms 34840 KB
#include <bits/stdc++.h>

#define fastio ios_base :: sync_with_stdio(0), cin.tie(0);

using namespace std;
using ll = long long;

const int mxN = 1e6 + 5, mod = 1e9 + 7;

int n, d[mxN], b[mxN], ans; string a;
vector <int> adj[mxN];

void calc(int v, int p = -1) {
	int cnt = 0;
	for (auto to : adj[v]) {
		if (to != p) {
			calc(to, v);
			if (d[to] > 0)
				++cnt;
			d[v] += d[to];
		}
	}
	if (cnt <= 1 && p == -1) 
		d[v] += a[v - 1];
	else {
		if (d[v] > 0) 
			d[v] -= a[v - 1];
		if (!d[v]) 
			d[v] = a[v - 1];
	}
}

void dfs(int v, int p = -1) {
	int cnt = 0, h = 0;
	for (auto to : adj[v])
		cnt += d[to], h += b[to]; 
	ans = max(ans, (int)a[v - 1]);
	ans = max(ans, cnt + (h > 0 ? -a[v - 1] : a[v - 1]));
	for (auto to : adj[v]) {
		if (to != p) {
			int dv = d[v], dto = d[to];
			int bv = b[v], bto = b[to];
			d[v] -= d[to];
			if (d[v] <= 1) {
				if (a[v - 1])
					d[v] = b[v] = 1;
				else {
					if (d[v] <= 0)
						b[v] = 0;
				}
			}
			if (d[to] == 1 && a[to - 1] == 1) 
				d[to] = b[to] = 0;
			d[to] += d[v]; b[to] |= b[v];
			d[to] += a[to - 1] * (b[to] > 0 ? -1 : 1);
			b[to] |= a[to - 1];
			if (!d[to])
				d[to] = b[to] = a[to - 1];
			dfs(to, v);
			d[v] = dv, d[to] = dto, b[v] = bv, b[to] = bto;
		}
	}
}

int main() {
	fastio;
	cin >> n;
	for (int i = 1, u, v; i < n; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	cin >> a;
	for (auto &x : a) x -= '0';
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) d[j] = b[j] = 0;
		calc(i);
		ans = max(ans, d[i]);
	}
	cout << ans;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23756 KB Output is correct
2 Correct 11 ms 23752 KB Output is correct
3 Correct 11 ms 23732 KB Output is correct
4 Correct 12 ms 23756 KB Output is correct
5 Correct 13 ms 23700 KB Output is correct
6 Correct 12 ms 23744 KB Output is correct
7 Correct 12 ms 23696 KB Output is correct
8 Correct 12 ms 23756 KB Output is correct
9 Correct 14 ms 23768 KB Output is correct
10 Correct 12 ms 23756 KB Output is correct
11 Correct 11 ms 23756 KB Output is correct
12 Correct 11 ms 23772 KB Output is correct
13 Correct 12 ms 23808 KB Output is correct
14 Correct 11 ms 23728 KB Output is correct
15 Correct 12 ms 23776 KB Output is correct
16 Correct 12 ms 23756 KB Output is correct
17 Correct 12 ms 23776 KB Output is correct
18 Correct 11 ms 23760 KB Output is correct
19 Correct 13 ms 23744 KB Output is correct
20 Correct 12 ms 23736 KB Output is correct
21 Correct 12 ms 23816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23756 KB Output is correct
2 Correct 11 ms 23752 KB Output is correct
3 Correct 11 ms 23732 KB Output is correct
4 Correct 12 ms 23756 KB Output is correct
5 Correct 13 ms 23700 KB Output is correct
6 Correct 12 ms 23744 KB Output is correct
7 Correct 12 ms 23696 KB Output is correct
8 Correct 12 ms 23756 KB Output is correct
9 Correct 14 ms 23768 KB Output is correct
10 Correct 12 ms 23756 KB Output is correct
11 Correct 11 ms 23756 KB Output is correct
12 Correct 11 ms 23772 KB Output is correct
13 Correct 12 ms 23808 KB Output is correct
14 Correct 11 ms 23728 KB Output is correct
15 Correct 12 ms 23776 KB Output is correct
16 Correct 12 ms 23756 KB Output is correct
17 Correct 12 ms 23776 KB Output is correct
18 Correct 11 ms 23760 KB Output is correct
19 Correct 13 ms 23744 KB Output is correct
20 Correct 12 ms 23736 KB Output is correct
21 Correct 12 ms 23816 KB Output is correct
22 Correct 89 ms 23900 KB Output is correct
23 Correct 88 ms 23900 KB Output is correct
24 Correct 81 ms 23804 KB Output is correct
25 Correct 80 ms 23896 KB Output is correct
26 Correct 83 ms 23884 KB Output is correct
27 Correct 96 ms 23896 KB Output is correct
28 Correct 89 ms 23884 KB Output is correct
29 Correct 94 ms 24012 KB Output is correct
30 Correct 104 ms 24048 KB Output is correct
31 Correct 88 ms 23900 KB Output is correct
32 Correct 92 ms 23884 KB Output is correct
33 Correct 87 ms 23976 KB Output is correct
34 Correct 84 ms 23896 KB Output is correct
35 Correct 83 ms 23900 KB Output is correct
36 Correct 82 ms 23892 KB Output is correct
37 Correct 70 ms 23896 KB Output is correct
38 Correct 81 ms 23900 KB Output is correct
39 Correct 103 ms 23884 KB Output is correct
40 Correct 89 ms 23964 KB Output is correct
41 Correct 89 ms 23884 KB Output is correct
42 Correct 101 ms 23832 KB Output is correct
43 Correct 107 ms 23856 KB Output is correct
44 Correct 68 ms 23944 KB Output is correct
45 Correct 69 ms 23884 KB Output is correct
46 Correct 70 ms 23884 KB Output is correct
47 Correct 90 ms 23884 KB Output is correct
48 Correct 87 ms 23976 KB Output is correct
49 Correct 72 ms 23976 KB Output is correct
50 Correct 68 ms 23884 KB Output is correct
51 Correct 68 ms 23944 KB Output is correct
52 Correct 92 ms 23796 KB Output is correct
53 Correct 71 ms 23952 KB Output is correct
54 Correct 69 ms 23940 KB Output is correct
55 Correct 76 ms 23928 KB Output is correct
56 Correct 44 ms 23912 KB Output is correct
57 Correct 97 ms 24044 KB Output is correct
58 Correct 46 ms 23940 KB Output is correct
59 Correct 53 ms 23916 KB Output is correct
60 Correct 93 ms 24012 KB Output is correct
61 Correct 43 ms 24004 KB Output is correct
62 Correct 96 ms 24052 KB Output is correct
63 Correct 63 ms 23796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23756 KB Output is correct
2 Correct 11 ms 23752 KB Output is correct
3 Correct 11 ms 23732 KB Output is correct
4 Correct 12 ms 23756 KB Output is correct
5 Correct 13 ms 23700 KB Output is correct
6 Correct 12 ms 23744 KB Output is correct
7 Correct 12 ms 23696 KB Output is correct
8 Correct 12 ms 23756 KB Output is correct
9 Correct 14 ms 23768 KB Output is correct
10 Correct 12 ms 23756 KB Output is correct
11 Correct 11 ms 23756 KB Output is correct
12 Correct 11 ms 23772 KB Output is correct
13 Correct 12 ms 23808 KB Output is correct
14 Correct 11 ms 23728 KB Output is correct
15 Correct 12 ms 23776 KB Output is correct
16 Correct 12 ms 23756 KB Output is correct
17 Correct 12 ms 23776 KB Output is correct
18 Correct 11 ms 23760 KB Output is correct
19 Correct 13 ms 23744 KB Output is correct
20 Correct 12 ms 23736 KB Output is correct
21 Correct 12 ms 23816 KB Output is correct
22 Correct 89 ms 23900 KB Output is correct
23 Correct 88 ms 23900 KB Output is correct
24 Correct 81 ms 23804 KB Output is correct
25 Correct 80 ms 23896 KB Output is correct
26 Correct 83 ms 23884 KB Output is correct
27 Correct 96 ms 23896 KB Output is correct
28 Correct 89 ms 23884 KB Output is correct
29 Correct 94 ms 24012 KB Output is correct
30 Correct 104 ms 24048 KB Output is correct
31 Correct 88 ms 23900 KB Output is correct
32 Correct 92 ms 23884 KB Output is correct
33 Correct 87 ms 23976 KB Output is correct
34 Correct 84 ms 23896 KB Output is correct
35 Correct 83 ms 23900 KB Output is correct
36 Correct 82 ms 23892 KB Output is correct
37 Correct 70 ms 23896 KB Output is correct
38 Correct 81 ms 23900 KB Output is correct
39 Correct 103 ms 23884 KB Output is correct
40 Correct 89 ms 23964 KB Output is correct
41 Correct 89 ms 23884 KB Output is correct
42 Correct 101 ms 23832 KB Output is correct
43 Correct 107 ms 23856 KB Output is correct
44 Correct 68 ms 23944 KB Output is correct
45 Correct 69 ms 23884 KB Output is correct
46 Correct 70 ms 23884 KB Output is correct
47 Correct 90 ms 23884 KB Output is correct
48 Correct 87 ms 23976 KB Output is correct
49 Correct 72 ms 23976 KB Output is correct
50 Correct 68 ms 23884 KB Output is correct
51 Correct 68 ms 23944 KB Output is correct
52 Correct 92 ms 23796 KB Output is correct
53 Correct 71 ms 23952 KB Output is correct
54 Correct 69 ms 23940 KB Output is correct
55 Correct 76 ms 23928 KB Output is correct
56 Correct 44 ms 23912 KB Output is correct
57 Correct 97 ms 24044 KB Output is correct
58 Correct 46 ms 23940 KB Output is correct
59 Correct 53 ms 23916 KB Output is correct
60 Correct 93 ms 24012 KB Output is correct
61 Correct 43 ms 24004 KB Output is correct
62 Correct 96 ms 24052 KB Output is correct
63 Correct 63 ms 23796 KB Output is correct
64 Execution timed out 1571 ms 34840 KB Time limit exceeded
65 Halted 0 ms 0 KB -