This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// from duckindog wth depression
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int n;
vector<int> ad[N];
bool dd[N];
int st, ed;
void locate(int u, int pre = 0) {
dd[u] = 1;
for (int v : ad[u]) {
if (v == pre) continue;
if (dd[v]) {
st = u, ed = v;
continue;
}
locate(v, u);
}
}
int f[N][2][2][2][2];
int dfs(int u, int pre, int sub, int par, int cst, int ced) {
if (u == ed && (par && cst || sub != ced)) return 1e6;
auto &ret = f[u][sub][par][cst][ced];
if (ret > -1) return ret; ret = sub;
bool seen = par;
if (u == ed) seen |= cst;
int diff = 1e6, nv = 0;
for (int v : ad[u]) {
if (v == pre) continue;
if (par) {
ret += dfs(v, u, 0, sub, cst, ced);
}
if (!par) {
int upper = dfs(v, u, 1, sub, cst, ced);
int lower = dfs(v, u, 0, sub, cst, ced);
int differ = upper - lower;
if (differ < diff) diff = differ, nv = v;
}
}
if (nv) {
for (auto v : ad[u]) {
if (v == pre) continue;
if (!par) {
seen = 1;
if (v == nv) ret += dfs(v, u, 1, sub, cst, ced);
else ret += dfs(v, u, 0, sub, cst, ced);
}
}
}
if (!seen) ret = 1e6;
return ret;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("duck.inp", "r")) {
freopen("duck.inp", "r", stdin);
freopen("duck.out", "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; ++i) {
int u, v; cin >> u >> v;
ad[u].push_back(v);
ad[v].push_back(u);
} locate(1);
ad[st].erase(remove(ad[st].begin(), ad[st].end(), ed), ad[st].end());
ad[ed].erase(remove(ad[ed].begin(), ad[ed].end(), st), ad[ed].end());
memset(f, -1, sizeof f);
int answer = 1e6;
// for (int i = 0; i <= 1; ++i)
// for (int j = 0; j <= 1; ++j) cerr << dfs(st, ed, i, j, i, j) << '\n';
for (int i = 0; i <= 1; ++i)
for (int j = 0; j <= 1; ++j) answer = min(answer, dfs(st, ed, i, j, i, j));
cout << (answer >= n ? -1 : answer) << '\n';
}
Compilation message (stderr)
Main.cpp: In function 'long long int dfs(long long int, long long int, long long int, long long int, long long int, long long int)':
Main.cpp:26:23: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
26 | if (u == ed && (par && cst || sub != ced)) return 1e6;
| ~~~~^~~~~~
Main.cpp:29:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
29 | if (ret > -1) return ret; ret = sub;
| ^~
Main.cpp:29:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
29 | if (ret > -1) return ret; ret = sub;
| ^~~
Main.cpp: In function 'int32_t main()':
Main.cpp:72:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen("duck.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen("duck.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |