#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;
#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)
int t, n;
vi a, b;
vector<vi> adj;
int ans=0;
string x;
int cnt[300000];
int dfsc(int s, int p=-1){
cnt[s] = (x[s-1]=='1');
FOA(v,adj[s]) if(v!=p) cnt[s] += dfsc(v,s);
return cnt[s];
}
int dp[300000];
int dfs(int s, bool prev, int p=-1){
if(prev){
int ans1 = 0;
// don't take
FOA(v, adj[s]) if(v!=p) ans1 = max(ans1, dfs(v, 1, s));
//take
if(x[s-1]=='1') ans1 = max(ans1, 1);
//cout<<s<<" "<<prev<<" "<<ans1<<endl;
return ans1;
}
else{
if(dp[s]!=-1) return dp[s];
int ans2=0;
vi b;
//ans2 = max(ans2, min(2, cnt[s]));
if(x[s-1]=='1') ans2 = 1;
int ans3 = 0;
if(x[s-1]=='1') ans3--;
FOA(v, adj[s]){
if(v==p) continue;
//cout<<s<<" going to "<<v<<endl;
ans3 += dfs(v,0,s);
if(p==-1) ans2 = max(ans2, (x[s-1]=='1' ? 1 : 0) + dfs(v,0,s));
}
ans2 = max(ans2, ans3);
//cout<<s<<" "<<prev<<" "<<ans2<<" "<<ans3<<endl;
dp[s] = ans2;
return ans2;
}
}
int main(){
cin>>n;
adj.assign(n+1, vi());
FOR(i,n+1) dp[i] = -1;
FOR(i,n-1){
int a, b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin>>x;
dfsc(1);
cout<<max(min(2, cnt[1]), max(dfs(1,0), dfs(1,1)))<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |