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>
#define int long long
using namespace std;
const int MAX=1e9;
int n;
vector<vector<int>> adj;
bool can(vector<int> c, int mask) {
int i=0;
while(mask) {
if(mask&1) {
c[i]^=1;
for(int p : adj[i]) c[p]^=1;
}
mask>>=1; i++;
}
int sum=0;
for(int p : c) sum+=p;
return sum==0;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n; adj.resize(n);
for(int i=1; i<n; i++) {
int u, v; cin >> u >> v; --u; --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int ans=MAX;
vector<int> c(n); for(int &p : c) cin >> p;
for(int i=0; i<(1 << n); i++)
if(can(c, i)) ans=min(ans, (int)__builtin_popcount(i));
if(ans>=MAX) cout << "impossible\n";
else cout << ans << '\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... |