#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=5e5;
const int INF=1e9+7;
vector<int> e[N+10];
bool t[N+10];
pair<int,bool> dp0[N+10];
int dp1[N+10][2];
void dfs1(int x,int p)
{
for(auto v:e[x])
{
if(v!=p)
dfs1(v,x);
}
int sum=0;
dp0[x]={0,0};
for(auto v:e[x])
{
if(v==p)
continue;
sum+=dp0[v].se;
dp0[x].fi+=dp0[v].fi;
}
sum+=e[x].size();
if(sum%2==t[x])
{
dp0[x].se=1;
sum++;
}
dp0[x].fi+=sum;
int mn[2]={INF,INF};
mn[dp0[x].se]=0;
for(auto v:e[x])
{
if(v==p)
continue;
mn[dp0[x].se]=min(mn[dp0[x].se],dp1[v][!dp0[v].se]-2*dp0[v].se-dp0[v].fi);
mn[!dp0[x].se]=min(mn[!dp0[x].se],dp1[v][dp0[v].se]-2*dp0[x].se-dp0[v].fi);
}
dp1[x][0]=dp0[x].fi+mn[0];
dp1[x][1]=dp0[x].fi+mn[1];
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
char c;
cin>>c;
t[i]=(c=='1');
}
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
e[a].push_back(b);
e[b].push_back(a);
}
int ans=INF;
for(int i=1;i<=n;i++)
{
dfs1(i,0);
ans=min(ans,dp1[i][0]);
}
cout<<ans<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
11980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2085 ms |
56336 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2094 ms |
36056 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
11980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |