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<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N = 2e5 + 8;
int n, a[N];
char s[N];
vector<int> g[N];
void upd(int &a, int b) { if (a < b) a = b; }
int dp[N], cnt[N][3], tmp[4];
int ans;
void dfs(int u, int p) {
for (int v : g[u]) if (v != p) {
dfs(v, u);
if (u == 2) {
int bug = 1;
}
fill(tmp, tmp + 4, 0);
for (int i = 0; i < 3; ++i)
upd(tmp[i + 1], cnt[u][i] + dp[v]);
upd(cnt[u][1], tmp[1]);
upd(cnt[u][2], tmp[2]);
upd(cnt[u][2], tmp[3]);
}
dp[u] = max({ a[u], cnt[u][1] - a[u], cnt[u][2] - a[u] });
ans = max({ ans, cnt[u][1] + a[u], cnt[u][2] - a[u] });
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n;
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> (s + 1);
for (int i = 1; i <= n; ++i) a[i] = s[i] ^ 48;
dfs(1, 0);
cout << ans << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
Compilation message (stderr)
power.cpp: In function 'void dfs(int, int)':
power.cpp:20:17: warning: unused variable 'bug' [-Wunused-variable]
20 | int bug = 1;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |