이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ull unsigned ll
#define epb emplace_back
#define pb push_back
#define inf 1e9+1
#define linf 1e18+1;
using namespace std;
ll dp[100001][2][2];
ll dp1[100001][2][2];
ll d[100001];
vector<vector<int> >g(100001);
int c[100001];
void dfs(int v,int e=-1){
bool ind=false;
if(e!=-1)
dp[v][c[v]][c[e]]=dp1[v][c[v]][c[e]]=0;
for(int to:g[v]){
if(to==e)continue;
ind=true;
dfs(to,v);
dp1[v][0][1]=min({dp[to][0][0]+dp[v][0][1],dp[to][1][1]+dp[v][1][0]+1,(ll)inf});
dp1[v][1][0]=min({dp[to][0][1]+dp[v][1][0],dp[to][1][0]+dp[v][0][1]+1,(ll)inf});
dp1[v][0][0]=min({dp[to][0][0]+dp[v][0][0],dp[to][1][1]+dp[v][1][1]+1,(ll)inf});
dp1[v][1][1]=min({dp[to][0][1]+dp[v][1][1],dp[to][1][0]+dp[v][0][0]+1,(ll)inf});
dp[v][0][1]=dp1[v][0][1];
dp[v][1][0]=dp1[v][1][0];
dp[v][0][0]=dp1[v][0][0];
dp[v][1][1]=dp1[v][1][1];
}
if(ind==false){
dp1[v][0][1]=min(dp[v][0][1],dp[v][1][0]+1);
dp1[v][0][0]=min(dp[v][0][0],dp[v][1][1]+1);
dp1[v][1][0]=min(dp[v][1][0],dp[v][0][1]+1);
dp1[v][1][1]=min(dp[v][1][1],dp[v][0][0]+1);
dp[v][0][1]=dp1[v][0][1];
dp[v][1][0]=dp1[v][1][0];
dp[v][0][0]=dp1[v][0][0];
dp[v][1][1]=dp1[v][1][1];
}
}
void solve(){
int n;cin>>n;
for(int i=2;i<=n;i++){
dp[i][0][0]=dp[i][1][1]=dp[i][0][1]=dp[i][1][0]=inf;
dp1[i][0][0]=dp1[i][1][1]=dp1[i][0][1]=dp1[i][1][0]=inf;
}
for(int i=1;i<=n-1;i++){
int u,v;cin>>u>>v;
g[u].pb(v),g[v].pb(u);
}
for(int i=1;i<=n;i++)cin>>c[i];
dfs(1);
if(min(dp[1][0][0],dp[1][0][1])>=inf)cout<<"impossible";
else cout<<min(dp[1][0][0],dp[1][0][1]);
}
int main(){
solve();
}
# | 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... |