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"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif
const int N = 1e5 + 5;
// 1: last position of x, -1: second-last position of x -> sum(l, r) = 0-> non-unique subarray, > 0: unique
vector <int> adj[N];
int n;
signed main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
int x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
bool sub1 = true;
for (int i = 1; i <= n; ++i) {
if ((int) adj[i].size() != 2) sub1 = false;
}
if (sub1) {
cout << (n % 4 == 0? (n / 2) : -1) << "\n";
return 0;
}
bool sub2 = (n <= 20);
if (sub2) {
int res = 1e9;
for (int msk = 0; msk < (1 << n); ++msk) {
vector <bool> b(n + 1, 0);
int cur = 0;
for (int i = 1; i <= n; ++i) {
if (msk & (1 << (i - 1))) {
b[i] = 1;
++cur;
}
}
bool ans = true;
for (int i = 1; i <= n; ++i) {
int cnt = 0;
for (int j : adj[i])
if (b[j])
++cnt;
if (cnt != 1) ans = false;
}
if (ans) {
res = min(res, cur);
}
}
cout << (res != 1e9? res : -1) << "\n";
return 0;
}
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... |