이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const int N=100050;
int n,u,v;
long long dp[2][2][N];
bool st[N];
vector<int> adj[N];
void dfs(int nde,int par=-1){
//flip now? is on?
bool pt=1;
long long curr[2][2][2]={{{0,0},{0,0}},{{INT_MAX,INT_MAX},{INT_MAX,INT_MAX}}};
//chosen%2 flip this
for (int i:adj[nde]){
if (i==par) continue;
dfs(i,nde);
curr[0][0][pt]=min(curr[0][0][!pt]+dp[0][0][i],curr[1][0][!pt]+dp[1][0][i]);
curr[0][1][pt]=min(curr[0][1][!pt]+dp[0][1][i],curr[1][1][!pt]+dp[1][1][i]);
curr[1][0][pt]=min(curr[1][0][!pt]+dp[0][0][i],curr[0][0][!pt]+dp[1][0][i]);
curr[1][1][pt]=min(curr[1][1][!pt]+dp[0][1][i],curr[0][1][!pt]+dp[1][1][i]);
//cerr<<nde<<"::"<<curr[0][0][pt]<<" "<<curr[0][1][pt]<<" "<<curr[1][0][pt]<<" "<<curr[1][1][pt]<<"\n";
pt^=1;
}
//no flip
dp[0][0][nde]=curr[st[nde]][0][!pt];
dp[0][1][nde]=curr[!st[nde]][0][!pt];
//flip
dp[1][0][nde]=curr[!st[nde]][1][!pt]+1;
dp[1][1][nde]=curr[st[nde]][1][!pt]+1;
//cout<<nde<<"::"<<st[nde]<<": noflip:"<<dp[0][0][nde]<<" "<<dp[0][1][nde]<<" flip:"<<dp[1][0][nde]<<" "<<dp[1][1][nde]<<"\n";
}
int main(){
cin>>n;
for (int i=1;i<n;i++){
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i=1;i<=n;i++) cin>>st[i];
dfs(1);
long long ans=min(dp[0][0][1],dp[1][0][1]);
if (ans>=1e9) cout<<"impossible\n";
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... |