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 dp[N];
void dfs(int u, int p, int root) {
// only root
dp[u] = e[u];
// if (u == 5) {
// cout << e[u] << "\n";
// }
int sum = 0;
fore(v, tree[u]) if (v != p) {
dfs(v, u, root);
// only one child
if (u == root) dp[u] = max(dp[u], dp[v] + 1);
// take more than two child
sum += dp[v];
}
// if (u == 5) cout << dp[5] << "\n";
// if (u == 5) cout << sum - e[u] << "\n";
dp[u] = max(dp[u], sum - 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(4, 4, 4);
// cout << dp[4] << " " << dp[3] << "\n";
// cout << dp[1] << " " << dp[2] << "\n";
// cout << dp[5] << "\n";
int ans = 0;
foru(i, 1, n) if (e[i]) {
dfs(i, i, i);
ans = max(ans, dp[i]);
// if (dp[i] == 4) {
// cout << "ddm\n";
// cout << i;
// return 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... |