Submission #424317

# Submission time Handle Problem Language Result Execution time Memory
424317 2021-06-11T19:39:59 Z abdzag Power Plant (JOI20_power) C++17
100 / 100
434 ms 124564 KB
#include<bits/stdc++.h>
#include<unordered_map>
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
#define rrep(i,a,b) for(int i=int(a);i>int(b);i--)
#define trav(a,v) for(auto& a: v)
#define sz(v) v.size()
#define all(v) v.begin(),v.end()
#define vi vector<int>

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const long long inf = 1e15;

using namespace std;
vector<vector<ll>> dp(1e6,vector<ll>(2,-inf));
vector<ll> parents(1e6);
vector<vector<ll>> children(1e6);
string s;
ll solve(ll cur,bool lit) {
	if (children[cur].size() == 0) {
		return s[cur-1] == '1';
	}
	if (dp[cur][lit] != -inf)return dp[cur][lit];
	ll ans1=-inf;
	ll ans2=-inf;
	ll ans3 = 0;
	ll ans4 = 0;
	trav(a, children[cur]) {
		ans1 = max(ans1, solve(a,lit));
	}
	trav(a, children[cur]) {
		ans2 = max(ans2, solve(a, 1));
	}
	ll one = 1;
	ll zero = 0;
	if (s[cur - 1] == '1') {
		if (lit)ans2 -= 1, ans1 -= 1;
		else ans2++;
		ans2 = max(one, ans2);
	}
	ans1 = max(zero, ans1);
	trav(a, children[cur]) {
		ans3 += solve(a, 1);
	}
	trav(a, children[cur]) {
		ans4 += solve(a, 1);
	}
	if (s[cur - 1] == '1') {
		ans3--;
		ans4--;
	}
	//cout << "cur: " << cur << endl;
	//cout << ans1 << " "<<ans2<<" "<<ans3<<" "<<ans4<<endl;
	return dp[cur][lit] = max(max(ans1, ans2),max(ans3,ans4));
}
int main() {
	cin.sync_with_stdio(false);
	ll n;
	cin >> n;
	vector<vector<ll>> g(n + 1);
	rep(i, 0, n-1) {
		ll a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	cin >> s;
	if (n == 1) {
		cout << (s == "1" ? 1 : 0)<<endl;
		return 0;
	}
	else if (n == 2) {
		ll ans = 0;
		trav(a, s)ans += a == '1';
		cout << ans << endl;
		return 0;
	}
	queue<ll> q;
	vector<bool> isLeaf(n+1);
	rep(i, 1, n + 1)if (g[i].size() == 1)isLeaf[i]=1;
	ll root = -1;
	rep(i, 1, n + 1)if (!isLeaf[i])root = i;
	q.push(root);
	vector<bool> visited(n + 1);
	visited[root] = 1;
	while (!q.empty()) {
		ll cur = q.front();
		q.pop();
		trav(a, g[cur]) {
			if (!visited[a]) {
				parents[a] = cur;
				children[cur].push_back(a);
				visited[a] = 1;
				q.push(a);
			}
		}
	}
	rep(i, 1, n + 1) {
		if (isLeaf[i]) {
			q.push(i);
		}
	}
	parents[root] = -1;
	cout << solve(root,0);
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 88 ms 86340 KB Output is correct
2 Correct 95 ms 86360 KB Output is correct
3 Correct 88 ms 86316 KB Output is correct
4 Correct 92 ms 86396 KB Output is correct
5 Correct 86 ms 86416 KB Output is correct
6 Correct 87 ms 86392 KB Output is correct
7 Correct 90 ms 86340 KB Output is correct
8 Correct 89 ms 86412 KB Output is correct
9 Correct 86 ms 86352 KB Output is correct
10 Correct 90 ms 86388 KB Output is correct
11 Correct 88 ms 86380 KB Output is correct
12 Correct 85 ms 86332 KB Output is correct
13 Correct 85 ms 86308 KB Output is correct
14 Correct 84 ms 86340 KB Output is correct
15 Correct 85 ms 86308 KB Output is correct
16 Correct 85 ms 86316 KB Output is correct
17 Correct 88 ms 86296 KB Output is correct
18 Correct 84 ms 86336 KB Output is correct
19 Correct 85 ms 86396 KB Output is correct
20 Correct 89 ms 86304 KB Output is correct
21 Correct 86 ms 86408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 86340 KB Output is correct
2 Correct 95 ms 86360 KB Output is correct
3 Correct 88 ms 86316 KB Output is correct
4 Correct 92 ms 86396 KB Output is correct
5 Correct 86 ms 86416 KB Output is correct
6 Correct 87 ms 86392 KB Output is correct
7 Correct 90 ms 86340 KB Output is correct
8 Correct 89 ms 86412 KB Output is correct
9 Correct 86 ms 86352 KB Output is correct
10 Correct 90 ms 86388 KB Output is correct
11 Correct 88 ms 86380 KB Output is correct
12 Correct 85 ms 86332 KB Output is correct
13 Correct 85 ms 86308 KB Output is correct
14 Correct 84 ms 86340 KB Output is correct
15 Correct 85 ms 86308 KB Output is correct
16 Correct 85 ms 86316 KB Output is correct
17 Correct 88 ms 86296 KB Output is correct
18 Correct 84 ms 86336 KB Output is correct
19 Correct 85 ms 86396 KB Output is correct
20 Correct 89 ms 86304 KB Output is correct
21 Correct 86 ms 86408 KB Output is correct
22 Correct 98 ms 86556 KB Output is correct
23 Correct 88 ms 86724 KB Output is correct
24 Correct 87 ms 86588 KB Output is correct
25 Correct 90 ms 86540 KB Output is correct
26 Correct 86 ms 86600 KB Output is correct
27 Correct 89 ms 86596 KB Output is correct
28 Correct 87 ms 86528 KB Output is correct
29 Correct 85 ms 86672 KB Output is correct
30 Correct 90 ms 86724 KB Output is correct
31 Correct 88 ms 86556 KB Output is correct
32 Correct 87 ms 86716 KB Output is correct
33 Correct 86 ms 86596 KB Output is correct
34 Correct 86 ms 86500 KB Output is correct
35 Correct 87 ms 86576 KB Output is correct
36 Correct 90 ms 86508 KB Output is correct
37 Correct 86 ms 86496 KB Output is correct
38 Correct 91 ms 86528 KB Output is correct
39 Correct 89 ms 86596 KB Output is correct
40 Correct 88 ms 86588 KB Output is correct
41 Correct 89 ms 86548 KB Output is correct
42 Correct 88 ms 86596 KB Output is correct
43 Correct 90 ms 86636 KB Output is correct
44 Correct 87 ms 86584 KB Output is correct
45 Correct 88 ms 86660 KB Output is correct
46 Correct 88 ms 86576 KB Output is correct
47 Correct 92 ms 86568 KB Output is correct
48 Correct 102 ms 86568 KB Output is correct
49 Correct 93 ms 86556 KB Output is correct
50 Correct 89 ms 86664 KB Output is correct
51 Correct 90 ms 86596 KB Output is correct
52 Correct 85 ms 86516 KB Output is correct
53 Correct 87 ms 86636 KB Output is correct
54 Correct 85 ms 86596 KB Output is correct
55 Correct 90 ms 86532 KB Output is correct
56 Correct 87 ms 86516 KB Output is correct
57 Correct 91 ms 86820 KB Output is correct
58 Correct 89 ms 86600 KB Output is correct
59 Correct 86 ms 86560 KB Output is correct
60 Correct 88 ms 86676 KB Output is correct
61 Correct 88 ms 86568 KB Output is correct
62 Correct 85 ms 86692 KB Output is correct
63 Correct 87 ms 86596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 86340 KB Output is correct
2 Correct 95 ms 86360 KB Output is correct
3 Correct 88 ms 86316 KB Output is correct
4 Correct 92 ms 86396 KB Output is correct
5 Correct 86 ms 86416 KB Output is correct
6 Correct 87 ms 86392 KB Output is correct
7 Correct 90 ms 86340 KB Output is correct
8 Correct 89 ms 86412 KB Output is correct
9 Correct 86 ms 86352 KB Output is correct
10 Correct 90 ms 86388 KB Output is correct
11 Correct 88 ms 86380 KB Output is correct
12 Correct 85 ms 86332 KB Output is correct
13 Correct 85 ms 86308 KB Output is correct
14 Correct 84 ms 86340 KB Output is correct
15 Correct 85 ms 86308 KB Output is correct
16 Correct 85 ms 86316 KB Output is correct
17 Correct 88 ms 86296 KB Output is correct
18 Correct 84 ms 86336 KB Output is correct
19 Correct 85 ms 86396 KB Output is correct
20 Correct 89 ms 86304 KB Output is correct
21 Correct 86 ms 86408 KB Output is correct
22 Correct 98 ms 86556 KB Output is correct
23 Correct 88 ms 86724 KB Output is correct
24 Correct 87 ms 86588 KB Output is correct
25 Correct 90 ms 86540 KB Output is correct
26 Correct 86 ms 86600 KB Output is correct
27 Correct 89 ms 86596 KB Output is correct
28 Correct 87 ms 86528 KB Output is correct
29 Correct 85 ms 86672 KB Output is correct
30 Correct 90 ms 86724 KB Output is correct
31 Correct 88 ms 86556 KB Output is correct
32 Correct 87 ms 86716 KB Output is correct
33 Correct 86 ms 86596 KB Output is correct
34 Correct 86 ms 86500 KB Output is correct
35 Correct 87 ms 86576 KB Output is correct
36 Correct 90 ms 86508 KB Output is correct
37 Correct 86 ms 86496 KB Output is correct
38 Correct 91 ms 86528 KB Output is correct
39 Correct 89 ms 86596 KB Output is correct
40 Correct 88 ms 86588 KB Output is correct
41 Correct 89 ms 86548 KB Output is correct
42 Correct 88 ms 86596 KB Output is correct
43 Correct 90 ms 86636 KB Output is correct
44 Correct 87 ms 86584 KB Output is correct
45 Correct 88 ms 86660 KB Output is correct
46 Correct 88 ms 86576 KB Output is correct
47 Correct 92 ms 86568 KB Output is correct
48 Correct 102 ms 86568 KB Output is correct
49 Correct 93 ms 86556 KB Output is correct
50 Correct 89 ms 86664 KB Output is correct
51 Correct 90 ms 86596 KB Output is correct
52 Correct 85 ms 86516 KB Output is correct
53 Correct 87 ms 86636 KB Output is correct
54 Correct 85 ms 86596 KB Output is correct
55 Correct 90 ms 86532 KB Output is correct
56 Correct 87 ms 86516 KB Output is correct
57 Correct 91 ms 86820 KB Output is correct
58 Correct 89 ms 86600 KB Output is correct
59 Correct 86 ms 86560 KB Output is correct
60 Correct 88 ms 86676 KB Output is correct
61 Correct 88 ms 86568 KB Output is correct
62 Correct 85 ms 86692 KB Output is correct
63 Correct 87 ms 86596 KB Output is correct
64 Correct 328 ms 106128 KB Output is correct
65 Correct 344 ms 106136 KB Output is correct
66 Correct 342 ms 106092 KB Output is correct
67 Correct 335 ms 106096 KB Output is correct
68 Correct 345 ms 106144 KB Output is correct
69 Correct 308 ms 106032 KB Output is correct
70 Correct 313 ms 106084 KB Output is correct
71 Correct 326 ms 106032 KB Output is correct
72 Correct 340 ms 106116 KB Output is correct
73 Correct 393 ms 123280 KB Output is correct
74 Correct 423 ms 120244 KB Output is correct
75 Correct 321 ms 105952 KB Output is correct
76 Correct 370 ms 105868 KB Output is correct
77 Correct 329 ms 106212 KB Output is correct
78 Correct 361 ms 106228 KB Output is correct
79 Correct 347 ms 106244 KB Output is correct
80 Correct 327 ms 106128 KB Output is correct
81 Correct 371 ms 105724 KB Output is correct
82 Correct 320 ms 105788 KB Output is correct
83 Correct 383 ms 113872 KB Output is correct
84 Correct 348 ms 111648 KB Output is correct
85 Correct 388 ms 112920 KB Output is correct
86 Correct 320 ms 107652 KB Output is correct
87 Correct 348 ms 107844 KB Output is correct
88 Correct 308 ms 107452 KB Output is correct
89 Correct 344 ms 107820 KB Output is correct
90 Correct 305 ms 107452 KB Output is correct
91 Correct 358 ms 107888 KB Output is correct
92 Correct 302 ms 107896 KB Output is correct
93 Correct 330 ms 113072 KB Output is correct
94 Correct 320 ms 110552 KB Output is correct
95 Correct 338 ms 113032 KB Output is correct
96 Correct 326 ms 110208 KB Output is correct
97 Correct 364 ms 106868 KB Output is correct
98 Correct 324 ms 106588 KB Output is correct
99 Correct 326 ms 106484 KB Output is correct
100 Correct 352 ms 109596 KB Output is correct
101 Correct 302 ms 107520 KB Output is correct
102 Correct 329 ms 106748 KB Output is correct
103 Correct 386 ms 106968 KB Output is correct
104 Correct 233 ms 105716 KB Output is correct
105 Correct 426 ms 119984 KB Output is correct
106 Correct 229 ms 105524 KB Output is correct
107 Correct 219 ms 105556 KB Output is correct
108 Correct 434 ms 121052 KB Output is correct
109 Correct 214 ms 105752 KB Output is correct
110 Correct 393 ms 124564 KB Output is correct
111 Correct 306 ms 105640 KB Output is correct