#include <bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define vn vector<ll>
using namespace std;
const int MAX_N = 100000 + 7;
#define pii pair<ll, ll>
#define pb push_back
bool check = true;
#define srt(vp) sort(vp.begin(), vp.end())
vector<vector<int>> adj;
vn colours;
void dfs(int node, int colour) {
colours[node] = colour;
for (int u : adj[node]) {
if (colours[u] == 0) {
dfs(u, 3 - colour);
} else if (colours[u] == colour) {
check = false;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
adj.resize(n + 1);
colours.resize(n + 1);
for (int i = 0; i < n; i++) {
ll a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
for (int i = 1; i <= n; i++) {
if (colours[i] == 0) {
dfs(i, 1);
}
}
if (!check) {
cout << -1 << endl;
return 0;
}
ll sum1 = 0, sum2 = 0;
for (int i = 1; i <= n; i++) {
if (colours[i] == 1) sum1++;
else sum2++;
}
cout << min(sum1, sum2) << endl;
}
# | 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... |