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;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second
vector<vi> adj(200005);
vector<bool> visited(200005);
string hasGen;
int temp = 0;
bool many = false;
void dfs(int x, int p){
visited[x] = 1;
int branch = 0;
for(auto i : adj[x]){
int temp2 = temp;
if(hasGen[i-1] == '1' && i != p)
temp++;
if(!visited[i] && hasGen[i-1] != '1'){
dfs(i, x);
}
if(temp2 != temp)
branch++;
}
if(branch >= 2)
many = true;
else
many = false;
}
void solve(){
int n;
cin >> n;
for(int i = 0; i < n-1; i++){
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
cin >> hasGen;
int ans = 1;
for(int i = 1; i <= n; i++){
temp = 0; many = false;
if(!visited[i]){
//cout << i << ' ';
dfs(i, -1);
if(hasGen[i-1] == '1')
temp++;
//cout << temp << '\n';
}
if(many)
temp -= 2;
ans = max(ans, temp);
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;
//cin >> t;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |