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;
#define mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;
#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)
int t, n;
vi a, b;
int main(){
cin>>n;
FOR(i,n-1){
int a, b;
cin>>a>>b;
}
a.resize(n+1);
b.resize(n+1);
FOR(i,n){
cin>>a[i];
b[i] = a[i];
}
int ans1=0;
bool ok1=false;
FORi(i,1,n){
if(a[i-1]==1){
a[i-1] = 1-a[i-1];
a[i] = 1-a[i];
a[i+1] = 1-a[i+1];
ans1++;
}
}
if(a[n-1]+a[n-2]==0) ok1=true;
int ans2=1;
FOR(i,n) a[i] = b[i];
a[0] = 1-a[0];
a[1] = 1-a[1];
FORi(i,1,n){
if(a[i-1]==1){
a[i-1] = 1-a[i-1];
a[i] = 1-a[i];
a[i+1] = 1-a[i+1];
ans2++;
}
}
if(a[n-1]+a[n-2]!= 0 && !ok1) cout<<"impossible\n";
else if(!ok1) cout<<ans2<<endl;
else if(a[n-1]+a[n-2]!=0) cout<<ok1<<endl;
else cout<<min(ans1, ans2);
}
# | 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... |