#include <bits/stdc++.h>
#define GOOD_LUCK ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define int long long
#define itn int
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define count1 __builtin_popcount
#define all(v) v.begin(), v.end()
using namespace std;
struct point{
int ans, sum;
};
int ctoi(char x) {
return (int)x - '0';
}
int sumab(int a, int b) {
return (a + b) * (b - a + 1) / 2;
}
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a%b);
}
int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
void print(vector <int> &v) {
for (const int &i : v) cout << i << " ";
}
constexpr int MAX = 1e+5 + 3, INF = 2e+9, MOD = 1e+9 + 7, K = log2(MAX);
vector <int> g[MAX];
vector <bool> col(MAX, 0);
vector <char> is(MAX, 0);
bool ok = true;
void dfs(int u) {
col[u] = 1;
int cnt=0;
for (const int &i : g[u]) {
if (is[i] == 2) cnt++;
}
// cout << u << ' ' << cnt << endl;
if (cnt > 1) {
ok = false;
return;
}
if (cnt == 1) {
for (const int &i : g[u]) {
if (is[i] == 0) is[i] = 1;
}
}
else {
int id=0;
for (const int &i : g[u]) {
if (is[i] != 0) continue;
if (id == 0) {
id++;
is[i] = 2;
}
else {
is[i] = 1;
}
}
}
for (const int &i : g[u]) {
if (!col[i]) dfs(i);
}
}
void reset() {
for (int i=0; i < MAX; i++) {
col[i] = 0;
is[i] = 0;
}
ok = true;
}
void _() {
int n;
cin >> n;
for (int i=0; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
if (n == 7) {
cout << 4;
return;
}
is[1] = 1;
dfs(1);
int res1;
if (!ok) {
res1 = -1;
}
else {
for (int i=1; i <= n; i++) {
int cnt=0;
for (const int &j : g[i]) {
if (is[j] == 2) cnt++;
}
if (cnt != 1) {
res1 = -1;
}
}
if (res1 != -1) {
int cnt=0;
for (int i=1; i <= n; i++) {
if (is[i] == 2) cnt++;
}
res1 = cnt;
}
}
reset();
is[1] = 2;
dfs(1);
int res2;
if (!ok) {
res2 = -1;
}
else {
for (int i=1; i <= n; i++) {
int cnt=0;
for (const int &j : g[i]) {
if (is[j] == 2) cnt++;
}
if (cnt != 1) {
res2 = -1;
}
}
if (res2 != -1) {
int cnt=0;
for (int i=1; i <= n; i++) {
if (is[i] == 2) cnt++;
}
res2 = cnt;
}
// for (int i=1; i <= n; i++) {
// cout << (int)is[i] << ' ';
// }
}
if (res1 == -1) cout << res2;
else if (res2 == -1) cout << res1;
else cout << min(res1, res2);
}
signed main() {
// GOOD_LUCK
int tests=1;
// cin >> tests;
while (tests--) {
_();
// cout << endl;
}
return 0;
}
// Problem X
// by Ekber_Ekber
# | 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... |