This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define fi first
#define se second
#define pb push_back
#define TC int t; cin>>t; while(t--)
#define all(x) (x).begin(),(x).end()
//*AC BERSAMA ALLAH FORTIS FORTUNA ADIUVAT
const ll nx = 1e5+5;
vector<ll> adj[nx];
bool on[nx], vis[nx];
ll dp[nx][2][2];
ll n;
void dfs(ll nd){
vis[nd] = 1;
if(on[nd])
dp[nd][1][0] = 1, dp[nd][0][1] = 0;
else
dp[nd][1][1] = 1, dp[nd][0][0] = 0;
for(ll i : adj[nd]){
if(!vis[i]){
dfs(i);
ll x = dp[nd][0][0], y = dp[nd][0][1];
dp[nd][0][0] = min(x + dp[i][0][0], y + dp[i][1][0]);
dp[nd][0][1] = min(y + dp[i][0][0], x + dp[i][1][0]);
x = dp[nd][1][0], y = dp[nd][1][1];
dp[nd][1][0] = min(x + dp[i][0][1], y + dp[i][1][1]);
dp[nd][1][1] = min(y + dp[i][0][1], x + dp[i][1][1]);
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(ll i=1; i<n; i++){
ll u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(ll i=1; i<=n; i++){
cin >> on[i];
}
for(ll i=1; i<=n; i++){
for(ll j=0; j<=1; j++)
dp[i][j][0] = dp[i][j][1] = 1e6;
}
dfs(1);
if(min(dp[1][1][0], dp[1][0][0]) > n){
cout << "impossible\n";
}
else cout << min(dp[1][1][0], dp[1][0][0]) << '\n';
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... |