이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 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;
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 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 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';
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'long long int ans(long long int, long long int, bool, bool)':
Main.cpp:61:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | for (auto [a, b] : k) o += b;
| ^
Main.cpp:63:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
63 | for (auto [a, b] : k) mi = min(mi, o - b + a);
| ^
# | 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... |