#include <bits/stdc++.h>
using namespace std;
#define oo 1e9
#define fi first
#define se second
#define sp(iiii) setprecision(iiii)
#define IO ios_base::sync_with_stdio(false); cin.tie(0)
#define ms(aaaa,xxxx) memset(aaaa,xxxx,sizeof(aaaa))
#define cntbit(xxxx) __builtin_popcount(xxxx)
#define getbit(xxxx,aaaa) (((xxxx)>>(aaaa-1))&1)
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<pair<int,int>,int> piii;
typedef pair<pair<int,int>,pair<int,int>> piiii;
typedef pair<long long,long long> pll;
typedef pair<pair<long long,long long>,long long> plll;
typedef pair<pair<long long,long long>,pair<long long,long long>> pllll;
typedef pair<pair<long long,long long>,bool> pllb;
typedef pair<long double,long double> pld;
typedef pair<pair<long double,long double>,long double> plld;
typedef pair<long double,long long> pdl;
const ll base=361;
const ll mod=1e9;
const ll mod2=1e9;
const ld eps=1e-3;
const ld eps2=1e-7;
const ll maxn=1e6+9;
const ll maxsize=6e5+29;
const ld pi=acos(-1);
//const ll delta=1e5+7;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
const int dxkn[8]={1,1,2,2,-1,-1,-2,-2};
const int dykn[8]={2,-2,1,-1,2,-2,1,-1};
const int dxki[8]={1,1,1,0,0,-1,-1,-1};
const int dyki[8]={1,0,-1,1,-1,1,0,-1};
int n,i,u,v,f[200009][3][3],newf[200009][3][3],a[200009],j,ans;
vector<int> g[200009];
void dfs(int x,int y) {
if (a[x]==0) {
f[x][0][0]=0;
f[x][0][1]=oo;
f[x][1][0]=oo;
f[x][1][1]=1;
}
else {
f[x][0][0]=oo;
f[x][0][1]=1;
f[x][1][0]=0;
f[x][1][1]=oo;
}
for (int ii:g[x]) {
if (ii!=y) {
dfs(ii,x);
newf[x][0][0]=min(f[x][0][0]+f[ii][0][0],f[x][1][0]+f[ii][0][1]);
newf[x][0][1]=min(f[x][0][1]+f[ii][1][0],f[x][1][1]+f[ii][1][1]);
newf[x][1][0]=min(f[x][1][0]+f[ii][0][0],f[x][0][0]+f[ii][0][1]);
newf[x][1][1]=min(f[x][1][1]+f[ii][1][0],f[x][0][1]+f[ii][1][1]);
f[x][0][0]=newf[x][0][0];
f[x][0][1]=newf[x][0][1];
f[x][1][0]=newf[x][1][0];
f[x][1][1]=newf[x][1][1];
}
}
}
int main() {
IO;
cin>>n;
for (i=1;i<n;i++) {
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
for (i=1;i<=n;i++) {
cin>>a[i];
}
dfs(1,1);
//for (i=1;i<=n;i++) {
//cout<<f[i][0][0]<<' '<<f[i][0][1]<<' '<<f[i][1][0]<<' '<<f[i][1][1]<<'\n';
//}
ans=min({f[1][0][0],f[1][0][1]});
if (ans>=2*n+1) {
cout<<"impossible\n";
return 0;
}
else {
cout<<ans<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
24532 KB |
Output is correct |
2 |
Correct |
84 ms |
24424 KB |
Output is correct |
3 |
Correct |
64 ms |
24644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
24584 KB |
Output is correct |
2 |
Correct |
62 ms |
24304 KB |
Output is correct |
3 |
Correct |
70 ms |
24724 KB |
Output is correct |
4 |
Correct |
82 ms |
15544 KB |
Output is correct |
5 |
Incorrect |
78 ms |
16580 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |