#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define fi first
#define se second
#define ld long double
#define pb push_back
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int N;
set<int> v[100010];
vector<bool> vis(100010, 0);
int x, y, nodo = -1;
void dfs(int pos, int prec) {
vis[pos] = 1;
for (int k : v[pos]) {
if (k == prec) continue;
if (vis[k]) {
if (nodo == -1) {
nodo = pos;
x = prec;
y = k;
}
continue;
}
dfs(k, pos);
}
}
bool vicino = 0;
bool blux = 0;
bool bluy = 0;
int memo[100010][2][2][2][2][2];
bool vis1[100010][2][2][2][2][2];
int ans(int pos, int prec, bool blu, bool par) { // memo
if (!vicino && (pos == x || pos == y) && par) return 1e9;
if (bluy != blu && pos == y) return 1e9;
if (blux != blu && pos == x) return 1e9;
if (vis1[pos][blu][par][vicino][blux][bluy]) return memo[pos][blu][par][vicino][blux][bluy];
vis1[pos][blu][par][vicino][blux][bluy] = 1;
int tot = 0;
tot += blu;
vector<pair<int, int> > k;
int figli = 0;
for (int x : v[pos]) {
if (x == prec) continue;
figli++;
if (blu) {
if (par) {
tot += ans(x, pos, 0, 1);
} else {
k.pb({ans(x, pos, 1, 1), ans(x, pos, 0, 1)});
}
} else if (par) {
tot += ans(x, pos, 0, 0);
} else {
k.pb({ans(x, pos, 1, 0), ans(x, pos, 0, 0)});
}
}
if (!par && figli == 0 && (vicino || (pos != x && pos != y))) return memo[pos][blu][par][vicino][blux][bluy] = 1e9;
if ((!blu && !par) || (blu && !par)) {
int o = 0;
for (auto [a, b] : k) o += b;
int mi = 1e9;
for (auto [a, b] : k) mi = min(mi, o - b + a);
if (!vicino && (pos == x || pos == y))
tot += o;
else
tot += mi;
}
return memo[pos][blu][par][vicino][blux][bluy] = tot;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
v[a].insert(b);
v[b].insert(a);
}
dfs(0, -1);
// cout << x << " " << y << " " << nodo << '\n';
v[x].erase(nodo);
v[y].erase(nodo);
v[nodo].erase(x);
v[nodo].erase(y);
int res = 1e9;
blux = 0;
bluy = 0;
vicino = 0;
res = min(res, ans(x, -1, 0, 0) + ans(nodo, -1, 1, 0));
res = min(res, ans(x, -1, 1, 0) + ans(nodo, -1, 1, 0));
blux = 0;
bluy = 0;
vicino = 1;
res = min(res, ans(x, -1, 0, 0) + ans(nodo, -1, 0, 0));
res = min(res, ans(x, -1, 1, 0) + ans(nodo, -1, 0, 0));
blux = 0;
bluy = 1;
vicino = 0;
res = min(res, ans(x, -1, 0, 0) + ans(nodo, -1, 1, 1));
res = min(res, ans(x, -1, 1, 0) + ans(nodo, -1, 1, 1));
blux = 0;
bluy = 1;
vicino = 1;
// cout << ans(y, -1, 1, 0) << " " << ans(nodo, -1, 0, 1) << '\n';
res = min(res, ans(x, -1, 0, 0) + ans(nodo, -1, 0, 1));
res = min(res, ans(x, -1, 1, 0) + ans(nodo, -1, 0, 1));
blux = 1;
bluy = 0;
vicino = 0;
res = min(res, ans(x, -1, 0, 0) + ans(nodo, -1, 1, 1));
res = min(res, ans(x, -1, 1, 0) + ans(nodo, -1, 1, 1));
blux = 1;
bluy = 0;
vicino = 1;
res = min(res, ans(x, -1, 0, 0) + ans(nodo, -1, 0, 1));
res = min(res, ans(x, -1, 1, 0) + ans(nodo, -1, 0, 1));
if (res > 1e8)
cout << -1 << '\n';
else
cout << res << '\n';
// cout << ans(nodo, -1, 0, 0) << " " << ans(nodo, -1, 1, 0) << '\n';
// cout << mi << '\n';
}
Compilation message
Main.cpp: In function 'long long int ans(long long int, long long int, bool, bool)':
Main.cpp:67:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
67 | for (auto [a, b] : k) o += b;
| ^
Main.cpp:69:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
69 | for (auto [a, b] : k) mi = min(mi, o - b + a);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5044 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
497 ms |
59024 KB |
Output is correct |
6 |
Correct |
479 ms |
60172 KB |
Output is correct |
7 |
Correct |
507 ms |
60292 KB |
Output is correct |
8 |
Correct |
523 ms |
60280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4960 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4960 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
5 ms |
5460 KB |
Output is correct |
14 |
Correct |
5 ms |
5296 KB |
Output is correct |
15 |
Correct |
5 ms |
5332 KB |
Output is correct |
16 |
Correct |
5 ms |
5332 KB |
Output is correct |
17 |
Correct |
4 ms |
5076 KB |
Output is correct |
18 |
Correct |
4 ms |
5332 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
5 ms |
5332 KB |
Output is correct |
21 |
Correct |
3 ms |
5464 KB |
Output is correct |
22 |
Correct |
4 ms |
5336 KB |
Output is correct |
23 |
Correct |
5 ms |
5588 KB |
Output is correct |
24 |
Correct |
4 ms |
5432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5044 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
497 ms |
59024 KB |
Output is correct |
6 |
Correct |
479 ms |
60172 KB |
Output is correct |
7 |
Correct |
507 ms |
60292 KB |
Output is correct |
8 |
Correct |
523 ms |
60280 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4960 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
5 ms |
5460 KB |
Output is correct |
22 |
Correct |
5 ms |
5296 KB |
Output is correct |
23 |
Correct |
5 ms |
5332 KB |
Output is correct |
24 |
Correct |
5 ms |
5332 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5332 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5332 KB |
Output is correct |
29 |
Correct |
3 ms |
5464 KB |
Output is correct |
30 |
Correct |
4 ms |
5336 KB |
Output is correct |
31 |
Correct |
5 ms |
5588 KB |
Output is correct |
32 |
Correct |
4 ms |
5432 KB |
Output is correct |
33 |
Correct |
366 ms |
39888 KB |
Output is correct |
34 |
Correct |
357 ms |
40012 KB |
Output is correct |
35 |
Correct |
352 ms |
39900 KB |
Output is correct |
36 |
Correct |
342 ms |
39900 KB |
Output is correct |
37 |
Correct |
61 ms |
13616 KB |
Output is correct |
38 |
Correct |
397 ms |
39516 KB |
Output is correct |
39 |
Correct |
19 ms |
7992 KB |
Output is correct |
40 |
Correct |
359 ms |
38776 KB |
Output is correct |
41 |
Correct |
201 ms |
47248 KB |
Output is correct |
42 |
Correct |
216 ms |
46036 KB |
Output is correct |
43 |
Correct |
437 ms |
60184 KB |
Output is correct |
44 |
Correct |
315 ms |
53756 KB |
Output is correct |