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>
#define pb push_back
#define mp make_pair
#define int long long
using namespace std;
const int N = 5e5 + 5;
const int mod = 1e9+7;
const int inf = 1e18 + 10;
const int L = 20 * 20 * 20;
vector<int> in(N), adj[N];
vector<int> arr;
void dfs(int node, int par){
arr.pb(node);
for(auto itr: adj[node]){
if(itr != par){
dfs(itr, node);
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
string s;
cin>>s;
for(int i = 0; i < n-1; i++){
int a, b;
cin>>a>>b;
in[a]++;
in[b]++;
adj[a].pb(b);
adj[b].pb(a);
}
int uc = 0;
for(int i = 1; i <= n; i++){
if(in[i] == 1){
uc = i;
break;
}
}
dfs(uc, uc);
int first = n, last = 0;
vector<int> pre(n+1);
vector<int> tree(n);
for(int i = 0; i < n; i++){
tree[i] = s[arr[i] - 1] - '0';
if(tree[i] == 0){
first = min(first, i);
last = max(last, i);
}
pre[i+1] = pre[i] + (tree[i] == 0);
}
int ans = (n - first - (n - 1 - last) - pre[n]) * 2 + pre[n];
cout<<ans<<endl;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |