이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
f[x][0][0]=min(f[x][0][0],int(oo));
f[x][0][1]=min(f[x][0][1],int(oo));
f[x][1][0]=min(f[x][1][0],int(oo));
f[x][1][1]=min(f[x][1][1],int(oo));
}
}
f[x][0][0]=min(f[x][0][0],int(oo));
f[x][0][1]=min(f[x][0][1],int(oo));
f[x][1][0]=min(f[x][1][0],int(oo));
f[x][1][1]=min(f[x][1][1],int(oo));
}
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |