이 제출은 이전 버전의 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 pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;
const int NMAX = 1e5 + 1;
const int inf = 1e9 + 1;
int dp[NMAX][2][2];
int a[NMAX];
vector <vector <int> > g(NMAX);
void dfs(int v, int e = -1){
int ind = 0;
for(int to : g[v]){
if(to == e) continue;
dfs(to, v);
ind++;
}
if(!ind){
dp[v][a[v]][0] = 0;
dp[v][1 ^ a[v]][1] = 1;
return;
}
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
int mn = inf;
int cnt = 0;
int sum = 0;
for(int to : g[v]){
if(to == e) continue;
if(dp[to][y][0] > dp[to][y][1])
sum += dp[to][y][1], cnt++, mn = min(mn, dp[to][y][0] - dp[to][y][1]);
else sum += dp[to][y][0], mn = min(dp[to][y][1] - dp[to][y][0], mn);
sum = min(sum, inf);
}
if((cnt + y + a[v]) % 2 == x) dp[v][x][y] = min(dp[v][x][y], sum + y);
else dp[v][x][y] = min(dp[v][x][y], sum + mn + y);
}
}
}
int main(){
int n; cin >> n;
for(int i = 1; i <= n; i++){
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2;y++) dp[i][x][y] = inf;
}
}
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1; i <= n; i++) cin >> a[i];
dfs(1);
/*for(int i= 1; i <= n; i++){
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++) cout << dp[i][x][y] << ' ';
}
cout << "\n";
}*/
int v = min(dp[1][0][1], dp[1][0][0]);
if(v == inf){
cout << "impossible\n";
return 0;
}
cout << v;
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... |