이 제출은 이전 버전의 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 ss second
#define ff first
#define endl '\n'
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++){
int c=s[a]^(i^j);
int cnt=v[a].size();
if(p!=-1) cnt=cnt-1;
int fneed=0,sneed=1e9;
pair<int,int> need={0,1e9};
for(auto node: v[a]){
if(node!=p){
fneed=min(need.ff+dp[node][0][i],need.ss+dp[node][1][i]);
sneed=min(need.ff+dp[node][1][i],need.ss+dp[node][0][i]);
need.ff=fneed;
need.ss=sneed;
}
}
if(c==1) dp[a][i][j]=need.ss+i;
else dp[a][i][j]=need.ff+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];
}
dfs(1,-1);
int ans=min(dp[1][0][0],dp[1][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... |