#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define vll vector <ll>
#define vbool vector<bool>
#define pairll pair<ll,ll>
#define fi first
#define sc second
#define rever greater<ll>()
using namespace std;
void solve(){
ll n; cin >> n;
vector<vll> path(n+1);
for(ll i = 0, u, v; i < n; i++){
cin >> u >> v;
path[u].pb(v); path[v].pb(u);
}
ll a = 0, b = 0;
for(ll i = 1; i <= n; i++){
if(path[i].size() == 1){
if(a)b = i;
else a = i;
}
}
if(a == 0){
if(n%4)cout << -1 << endl;
else cout << n/2 << endl;
return;
}
if(a)a = path[a][0];
if(b)b = path[b][0];
vll par(n+1);
for(ll i : path[a])par[i] = a;
if(b){
for(ll i : path[b]){
if(par[i] != 0){
cout << -1 << endl; return;
}par[i] = b;
}
}
ll res = 0;
if(a)res++;
if(b)res++;
for(ll i = 1; i <= n; i++){
if(i == a || i == b)continue;
bool ans = 1;
for(ll j : path[i]){
if(par[j] != 0 && par[j] != i){ans = false; break;}
}
if(ans){
res++;
for(ll j : path[i])par[j] = i;
}
}
bool ans = true;
for(ll i = 1; i <= n; i++){
if(!par[i])ans = false;
}
if(ans){
cout << res << endl;
}else cout << -1 << endl;
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
ll t=1; //cin >> t;
for(int i = 1; i <= t; i++){
solve();
}
}
# | 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... |