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;
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,a,b;
cin >> n;
vector<vector<int>> graf(n, vector<int>(0));
vector<int> tablica(n,0);
bool sciezka = false;
for (int i=0; i<n-1; i++){
cin >> a >> b;
a--;b--;
graf[a].push_back(b);
graf[b].push_back(a);
if (abs(a-b)!=1) sciezka=false;
}
for (int i=0; i<n; i++) cin >> tablica[i];
if (sciezka){
int wynik=0;
for (int i=1; i<n; i++){
if (tablica[i-1]){
tablica[i-1] = 0;
tablica[i] = (tablica[i]+1)%2;
if (i<(n-1)) tablica[i+1] = (tablica[i+1]+1)%2;
wynik++;
}
}
bool git=true;
for (int i=0; i<n; i++) if(tablica[i]) git=false;
if (git) cout << wynik << endl;
else cout << "impossible\n";
}
else{
vector<int> tablica_2(n,0);
int wynik=1e7;
for (int j=0; j<=(1<<n); j++){
int wynik_2=0;
for (int i=0; i<n; i++) tablica_2[i] = tablica[i];
for (int i=0; i<n; i++){
//cout << j << " " << i <<" " << ((j)&(1<<(i))) << " " << (1<<(i)) << endl;
if ((j)&(1<<(i))){
tablica_2[i] = (tablica_2[i]+1)%2;
for (auto k : graf[i]) tablica_2[k] = (tablica_2[k]+1)%2;
wynik_2++;
}
}
bool good = true;
for (int i=0; i<n; i++){
if (tablica_2[i]) {good=false; break;}
}
if (good) wynik = min(wynik,wynik_2);
}
if (wynik==1e7) cout << "impossible\n";
else cout << wynik << endl;
}
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... |