이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (100005)
ll N;
vector<ll> v[MAXN];
ll dp[MAXN][2][2]; //index, press node x's button, final state of node x
bool initial[MAXN];
void dfs(ll x,ll p){
if(initial[x]){
dp[x][1][0] = 1;
dp[x][0][1] = 0;
}else{
dp[x][1][1] = 1;
dp[x][0][0] = 0;
}
for(auto u : v[x]){
if(u != p){
dfs(u,x);
ll a = dp[x][0][0], b = dp[x][0][1], c = dp[x][1][0], d = dp[x][1][1];
dp[x][0][0] = min(a + dp[u][0][0],b + dp[u][1][0]);
dp[x][0][1] = min(b + dp[u][0][0],a + dp[u][1][0]);
dp[x][1][0] = min(c + dp[u][0][1],d + dp[u][1][1]);
dp[x][1][1] = min(d + dp[u][0][1],c + dp[u][1][1]);
}
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
cin>>N;
for(ll i = 0;i < N - 1;i++){
ll a,b;
cin>>a>>b;
a--, b--;
v[a].push_back(b);
v[b].push_back(a);
}
for(ll i = 0;i < N;i++){
cin>>initial[i];
}
for(ll i = 0;i < N;i++){
for(ll j = 0;j < 2;j++){
for(ll k = 0;k < 2;k++){
dp[i][j][k] = 1e9;
}
}
}
dfs(0,-1);
ll minimum = 1e9;
minimum = min({minimum,dp[0][0][0],dp[0][1][0]}); //note that node 0 is the root so its final state must be turned off
if(minimum >= 1e9){
cout<<"impossible"<<'\n';
}else{
cout<<minimum<<'\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... |