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;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
void setIO() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen("input.inp", "r")) {
freopen("input.inp", "r", stdin);
freopen("input.out", "w", stdout);
}
}
const int N = 2e5+5;
int n, m, e[N];
string s;
vector<int> tree[N];
int ans, dp[N];
void dfs(int u, int p) {
dp[u] = e[u];
int sum = 0, ma = 0;
fore(v, tree[u]) if (v != p) {
dfs(v, u);
sum += dp[v];
ma = max(ma, dp[v]);
}
dp[u] = max(dp[u], sum - e[u]);
ans = max(ans, max(sum - e[u], ma + e[u]));
}
int main() {
setIO();
cin >> n;
foru(i, 2, n) {
int u, v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
cin >> s;
foru(i, 1, n) e[i] = (s[i-1] == '1');
dfs(1, 0);
cout << ans;
}
Compilation message (stderr)
power.cpp: In function 'void setIO()':
power.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen("input.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen("input.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |