# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
293711 | arman_ferdous | Power Plant (JOI20_power) | C++17 | 243 ms | 26872 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define dbg(x) cerr << #x << " is " << x << "\n";
using ll = long long;
using ii = pair<ll,ll>;
const int N = 2e5+10;
int n;
vector<int> g[N];
char s[N];
int dp[2][N];
void solve(int u, int f) {
int childcnt = 0;
for(int v : g[u]) if(v != f) {
childcnt++;
solve(v, u);
}
if(childcnt == 0) {
dp[0][u] = dp[1][u] = (s[u] == '1');
return;
}
if(s[u] == '0') {
dp[0][u] = 0;
for(int v : g[u]) if(v != f)
dp[0][u] = max(dp[0][u], dp[0][v]);
dp[1][u] = 0;
for(int v : g[u]) if(v != f)
dp[1][u] += dp[1][v];
}
else {
int on = 1, off = 0;
off = 0;
for(int v : g[u]) if(v != f)
off = max(off, dp[0][v]);
for(int v : g[u]) if(v != f)
on = max(on, dp[1][v] + 1);
dp[0][u] = max(on, off);
dp[1][u] = -1;
for(int v : g[u]) if(v != f)
dp[1][u] += dp[1][v];
dp[1][u] = max(1, dp[1][u]);
}
}
int main() {
scanf("%d", &n);
for(int i = 1; i < n; i++) {
int u, v; scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
scanf(" %s", s + 1);
solve(1, 0);
printf("%d\n", max(dp[0][1], dp[1][1]));
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |