#include <bits/stdc++.h>
#define ll long long
#define ld double
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
int inf=1000000007;
int mod=1000000007;
int mod1=998244353;
const int N=500007;
vector<int>G[N];
int t[N];
bool is[N];
int DP[N][2][2];
void dfs(int v,int o)
{
t[v]=!is[v];
for(int i=0;i<2;i++) for(int j=0;j<2;j++) DP[v][i][j]=inf;
DP[v][is[v]^1][0]=1;
for(auto u:G[v])
{
if(u==o) continue;
dfs(u,v);
t[v]+=t[u];
if(t[u]==0) continue;
vector<vector<int>>nDP(2,vector<int>(2,inf));
for(int x=0;x<2;x++)
{
for(int a=0;a<2;a++)
{
for(int y=0;y<2;y++)
{
for(int b=0;b<2;b++)
{
if(a&&b) continue;
int X=x,C=0;
if(y==0)
{
C+=2;
X^=1;
}
if(!b)
{
C++;
X^=1;
}
nDP[X][a|b]=min(nDP[X][a|b],DP[v][x][a]+DP[u][y][b]+C);
}
}
}
}
for(int x=0;x<2;x++) for(int a=0;a<2;a++) DP[v][x][a]=nDP[x][a];
}
DP[v][0][1]=min(DP[v][0][1],DP[v][0][0]);
DP[v][1][1]=min(DP[v][1][1],DP[v][1][0]);
}
vector<int>P[N][2][2];
vector<int>S[N][2][2];
int mem[N][2][2];
int ans=inf;
void dfs1(int v,int o)
{
ans=min(ans,DP[v][1][1]);
//cout<<v<<" "<<DP[v][1][1]<<endl;
//liczymy P,S;
for(int x=0;x<2;x++) for(int a=0;a<2;a++) S[v][x][a].resize(sz(G[v])+2,inf);
for(int x=0;x<2;x++) for(int a=0;a<2;a++) P[v][x][a].resize(sz(G[v])+2,inf);
P[v][is[v]^1][0][0]=1;
for(int i=1;i<=sz(G[v]);i++)
{
int u=G[v][i-1];
if(t[u]==0)
{
for(int x=0;x<2;x++) for(int a=0;a<2;a++) P[v][x][a][i]=P[v][x][a][i-1];
}
else
{
for(int x=0; x<2; x++)
{
for(int a=0; a<2; a++)
{
for(int y=0; y<2; y++)
{
for(int b=0; b<2; b++)
{
if(a&&b) continue;
int X=x,C=0;
if(y==0)
{
C+=2;
X^=1;
}
if(!b)
{
C++;
X^=1;
}
P[v][X][a|b][i]=min(P[v][X][a|b][i],P[v][x][a][i-1]+DP[u][y][b]+C);
}
}
}
}
}
}
S[v][0][0][sz(G[v])+1]=0;
for(int i=sz(G[v]);i>0;i--)
{
int u=G[v][i-1];
if(t[u]==0)
{
for(int x=0;x<2;x++) for(int a=0;a<2;a++) S[v][x][a][i]=S[v][x][a][i+1];
}
else
{
for(int x=0; x<2; x++)
{
for(int a=0; a<2; a++)
{
for(int y=0; y<2; y++)
{
for(int b=0; b<2; b++)
{
if(a&&b) continue;
int X=x,C=0;
if(y==0)
{
C+=2;
X^=1;
}
if(!b)
{
C++;
X^=1;
}
S[v][X][a|b][i]=min(S[v][X][a|b][i],S[v][x][a][i+1]+DP[u][y][b]+C);
}
}
}
}
}
}
for(int i=1;i<=sz(G[v]);i++)
{
int u=G[v][i-1];
if(u==o) continue;
for(int x=0;x<2;x++) for(int a=0;a<2;a++) DP[v][x][a]=inf;
for(int x=0; x<2; x++)
{
for(int a=0; a<2; a++)
{
for(int y=0; y<2; y++)
{
for(int b=0; b<2; b++)
{
if(a&&b) continue;
DP[v][x^y][a|b]=min(DP[v][x^y][a|b],P[v][x][a][i-1]+S[v][y][b][i+1]);
}
}
}
}
t[v]-=t[u];
t[u]+=t[v];
for(int x=0;x<2;x++) for(int a=0;a<2;a++) mem[u][x][a]=DP[u][x][a];
if(t[v]>0)
{
vector<vector<int>>nDP(2,vector<int>(2,inf));
for(int x=0; x<2; x++)
{
for(int a=0; a<2; a++)
{
for(int y=0; y<2; y++)
{
for(int b=0; b<2; b++)
{
if(a&&b) continue;
int X=x,C=0;
if(y==0)
{
C+=2;
X^=1;
}
if(!b)
{
C++;
X^=1;
}
nDP[X][a|b]=min(nDP[X][a|b],DP[u][x][a]+DP[v][y][b]+C);
}
}
}
}
for(int x=0; x<2; x++) for(int a=0; a<2; a++) DP[u][x][a]=nDP[x][a];
}
dfs1(u,v);
}
for(int x=0;x<2;x++) for(int a=0;a<2;a++) DP[v][x][a]=mem[v][x][a];
t[v]-=t[o];
t[o]+=t[v];
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,a,b;
string s;
cin>>n>>s;
for(int i=0;i<n;i++) is[i+1]=s[i]-'0';
for(int i=1;i<n;i++)
{
cin>>a>>b;
G[a].pb(b);
G[b].pb(a);
}
dfs(1,0);
dfs1(1,0);
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
111700 KB |
Output is correct |
2 |
Correct |
25 ms |
111780 KB |
Output is correct |
3 |
Correct |
25 ms |
111708 KB |
Output is correct |
4 |
Correct |
26 ms |
111704 KB |
Output is correct |
5 |
Correct |
25 ms |
111708 KB |
Output is correct |
6 |
Correct |
28 ms |
111708 KB |
Output is correct |
7 |
Correct |
24 ms |
111752 KB |
Output is correct |
8 |
Correct |
27 ms |
111708 KB |
Output is correct |
9 |
Correct |
24 ms |
111688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
779 ms |
262112 KB |
Output is correct |
2 |
Correct |
1102 ms |
343476 KB |
Output is correct |
3 |
Correct |
1102 ms |
358696 KB |
Output is correct |
4 |
Correct |
969 ms |
300784 KB |
Output is correct |
5 |
Correct |
1065 ms |
328504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
892 ms |
236076 KB |
Output is correct |
2 |
Correct |
1084 ms |
269300 KB |
Output is correct |
3 |
Correct |
1018 ms |
271200 KB |
Output is correct |
4 |
Correct |
823 ms |
248232 KB |
Output is correct |
5 |
Correct |
919 ms |
270348 KB |
Output is correct |
6 |
Correct |
963 ms |
259376 KB |
Output is correct |
7 |
Correct |
912 ms |
278820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
111700 KB |
Output is correct |
2 |
Correct |
25 ms |
111780 KB |
Output is correct |
3 |
Correct |
25 ms |
111708 KB |
Output is correct |
4 |
Correct |
26 ms |
111704 KB |
Output is correct |
5 |
Correct |
25 ms |
111708 KB |
Output is correct |
6 |
Correct |
28 ms |
111708 KB |
Output is correct |
7 |
Correct |
24 ms |
111752 KB |
Output is correct |
8 |
Correct |
27 ms |
111708 KB |
Output is correct |
9 |
Correct |
24 ms |
111688 KB |
Output is correct |
10 |
Correct |
779 ms |
262112 KB |
Output is correct |
11 |
Correct |
1102 ms |
343476 KB |
Output is correct |
12 |
Correct |
1102 ms |
358696 KB |
Output is correct |
13 |
Correct |
969 ms |
300784 KB |
Output is correct |
14 |
Correct |
1065 ms |
328504 KB |
Output is correct |
15 |
Correct |
892 ms |
236076 KB |
Output is correct |
16 |
Correct |
1084 ms |
269300 KB |
Output is correct |
17 |
Correct |
1018 ms |
271200 KB |
Output is correct |
18 |
Correct |
823 ms |
248232 KB |
Output is correct |
19 |
Correct |
919 ms |
270348 KB |
Output is correct |
20 |
Correct |
963 ms |
259376 KB |
Output is correct |
21 |
Correct |
912 ms |
278820 KB |
Output is correct |
22 |
Correct |
906 ms |
252900 KB |
Output is correct |
23 |
Correct |
979 ms |
260536 KB |
Output is correct |
24 |
Correct |
889 ms |
254500 KB |
Output is correct |
25 |
Correct |
850 ms |
246872 KB |
Output is correct |
26 |
Correct |
922 ms |
281988 KB |
Output is correct |
27 |
Correct |
875 ms |
279528 KB |
Output is correct |
28 |
Correct |
707 ms |
258788 KB |
Output is correct |
29 |
Correct |
1101 ms |
277080 KB |
Output is correct |
30 |
Correct |
841 ms |
266296 KB |
Output is correct |