이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const ll inf = 1e18;
const int i_inf = 1e9;
const ll mod = 1e9+7;
mt19937 _rand(time(NULL));
const int mxn = 2e5;
int n;
vector<int> g[mxn];
int a[mxn];
int dp[mxn][2][2];
void dfs(int u, int p){
int ver = -1;
for(auto e : g[u]){
if(e == p) continue;
ver = e;
dfs(e, u);
}
fr(t, 0, 2) fr(p, 0, 2) dp[u][t][p] = 1e9;
if(ver == -1){
if(a[u] == 0){
dp[u][0][0] = 0;
dp[u][1][1] = 1;
}
else{
dp[u][0][1] = 1;
dp[u][1][0] = 0;
}
return;
}
int D[2][2];
int D2[2][2];
fr(t, 0, 2){
fr(i, 0, 2) D[t][i] = dp[ver][t][i];
for(auto e : g[u]){
if(e == p || e == ver) continue;
fr(i, 0, 2) D2[t][i] = 1e9;
fr(prv, 0, 2){
fr(k, 0, 2){
D2[t][prv^k] = min(D2[t][prv^k], D[t][prv] + dp[e][t][k]);
}
}
fr(i, 0, 2) D[t][i] = D2[t][i];
}
}
fr(t, 0, 2){
fr(p, 0, 2){
int rem = t^p^a[u];
dp[u][t][p] = D[p][rem]+p;
}
}
}
void solve(){
cin >> n;
fr(i, 0, n-1){
int u, v;
cin >> u >> v;
--u, --v;
g[u].pb(v);
g[v].pb(u);
}
fr(i, 0, n){
cin >> a[i];
}dfs(0, 0);
int ans = min(dp[0][0][0], dp[0][0][1]);
if(ans >= 1e9){
cout<<"impossible"<<endl;
}
else{
cout<<ans<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
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... |