이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
#include<numeric>
#include<tuple>
#include<bitset>
using namespace std;
#define MOD 1000000007
#define int long long
#define ff first
#define ss second
#define endl '\n'
int st=-1;
const int mxN=1e5+1;
int dp[mxN][2][2];
vector<int> v[mxN],s(mxN);
void dfs(int a, int p){
for(auto node: v[a]){
if(node!=p){
dfs(node,a);
}
}
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
bool k=0;
int c=s[a]^(i^j);
pair<int,int> need;
int cnt=v[a].size();
if(a!=st) cnt--;
for(auto node: v[a]){
if(node!=p){
k^=1;
if(cnt==1){
if(c==1) need={dp[node][1][i],1e9};
else need={dp[node][0][i],1e9};
}else{
if(c==1){
if(k==1){
need.ff+=dp[node][0][i];
need.ss+=dp[node][1][i];
}else{
need.ff+=dp[node][1][i];
need.ss+=dp[node][0][i];
}
}else{
need.ff+=dp[node][0][i];
need.ss+=dp[node][1][i];
}
}
}else if(cnt==0){
if(c==1) need={1e9,1e9};
else need={0,0};
}
}
dp[a][i][j]=min(need.ff,need.ss)+i;
}
}
}
void solve(){
int n; cin>>n;
for(int i=1; i<n; i++){
int a,b; cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
for(int i=1; i<=n; i++){
cin>>s[i];
if(v[i].size()<=2) st=i;
}
dfs(st,-1);
int ans=min(dp[st][0][0],dp[st][1][0]);
if(ans>=1e9) cout<<"impossible";
else cout<<ans;
}
signed main(){
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
cout<<endl;
}
return 0;
}
# | 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... |