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;
const int sz = 1e6 + 5;
#define int long long
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
struct DSU{
vector<int>e;
DSU(int n){
e.assign(n + 1, -1);
}
int _find(int v){
if(e[v] < 0){
return v;
}
return e[v] = _find(e[v]);
}
int _union(int u,int v){
u = _find(u);
v = _find(v);
if(u != v){
if(e[u] > e[v]){
swap(u, v);
}
e[u] += e[v];
e[v] = u;
return 1;
}
return 0;
}
};
string s;
int n, res, sum, mx;
vector<vector<int>>v(sz);
vector<int>dp(sz);
void dfs(int u,int p){
sum = 0;
mx = 0;
for(auto i : v[u]){
if(i == p){
continue;
}
dfs(i, u);
sum += dp[i];
mx = max(mx, dp[i]);
}
dp[u] = max((int)(s[u] == '1'), sum - (s[u] == '1'));
res = max(res, sum - (int)(s[u] == '1'));
res = max(res, mx + (int)(s[u] == '1'));
}
void test_case(int testcase){
cin >> n;
for(int i = 0; i < n - 1; ++i){
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
cin >> s;
s = '#' + s;
dfs(1, -1);
cout << res << endl;
}
signed main(){
int T = 1;
for(int test = 1; test <= T; ++test){
test_case(test);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |