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 <vector>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 200'005;
vector<int> adjlist[MAXN];
string str;
int ans = 0;
int dfs(int x, int par) {
int take = 0, notake = 0;
if (str[x] == '1') {
notake--;
for (int nxt : adjlist[x]) {
if (nxt == par) continue;
int val = dfs(nxt, x);
take = max(take, val);
notake += val;
}
ans = max(ans, max(notake, take + 1));
return max(notake, 1);
}
else {
for (int nxt : adjlist[x]) {
if (nxt == par) continue;
int val = dfs(nxt, x);
notake += val;
}
ans = max(ans, notake);
return notake;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int nodes; cin >> nodes;
for (int i = 0; i < nodes - 1; i++) {
int a, b; cin >> a >> b;
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
cin >> str;
str = ' ' + str;
dfs(1, -1);
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |