# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
529048 |
2022-02-22T03:49:09 Z |
d2k05 |
Power Plant (JOI20_power) |
C++14 |
|
12 ms |
23808 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) {
for (auto to : adj[v]) {
if (to != p) calc(to, v), d[v] += d[to];
}
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 |
12 ms |
23804 KB |
Output is correct |
2 |
Correct |
12 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23708 KB |
Output is correct |
4 |
Correct |
12 ms |
23780 KB |
Output is correct |
5 |
Correct |
12 ms |
23756 KB |
Output is correct |
6 |
Correct |
12 ms |
23756 KB |
Output is correct |
7 |
Correct |
11 ms |
23808 KB |
Output is correct |
8 |
Incorrect |
12 ms |
23756 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23804 KB |
Output is correct |
2 |
Correct |
12 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23708 KB |
Output is correct |
4 |
Correct |
12 ms |
23780 KB |
Output is correct |
5 |
Correct |
12 ms |
23756 KB |
Output is correct |
6 |
Correct |
12 ms |
23756 KB |
Output is correct |
7 |
Correct |
11 ms |
23808 KB |
Output is correct |
8 |
Incorrect |
12 ms |
23756 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23804 KB |
Output is correct |
2 |
Correct |
12 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23708 KB |
Output is correct |
4 |
Correct |
12 ms |
23780 KB |
Output is correct |
5 |
Correct |
12 ms |
23756 KB |
Output is correct |
6 |
Correct |
12 ms |
23756 KB |
Output is correct |
7 |
Correct |
11 ms |
23808 KB |
Output is correct |
8 |
Incorrect |
12 ms |
23756 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |