#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e10;
const int MAXN = 1e5 + 25;
vector <int> adj[MAXN];
pair <int, int> bad;
bool vis[MAXN];
void dfs (int pos, int par = -1) {
vis[pos] = 1;
for (auto j : adj[pos]) {
if (j == par) continue;
if (vis[j]) {
bad = {j, pos};
} else {
dfs(j, pos);
}
}
}
int dp[MAXN][2][2][2][2];
void dfs2 (int pos, int par) {
for (int j = 0; j < (int)adj[pos].size(); j++) {
if (adj[pos][j] == par) {
adj[pos].erase(adj[pos].begin() + j);
}
}
for (auto j : adj[pos]) dfs2(j, pos);
}
int ans (int pos, int w, int x, int y, int z) {
if (pos == bad.second && w != z) return inf;
if (pos == bad.first && w != y) return inf;
int &ret = dp[pos][w][x][y][z];
if (ret != -1) return ret;
int flag = x;
flag += (pos == bad.first && z);
flag += (pos == bad.second && y);
for (auto j : adj[pos]) flag += (j == bad.second && z);
if (flag > 1) return ret = inf;
if (flag) {
int sum = 0;
for (auto j : adj[pos]) {
if (j == bad.second) {
sum += ans(j, z, w, y, z);
} else {
sum += ans(j, 0, w, y, z);
}
}
return ret = w + sum;
}
int sum = 0;
for (auto j : adj[pos]) {
sum += ans(j, 0, w, y, z);
}
int mn = inf;
for (auto j : adj[pos]) {
mn = min(mn, ans(j, 1, w, y, z) - ans(j, 0, w, y, z));
}
return ret = w + sum + mn;
}
signed main () {
memset(dp, -1, sizeof(dp));
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
for (int i = 0; i < (int)adj[bad.first].size(); i++) {
if (adj[bad.first][i] == bad.second) {
adj[bad.first].erase(adj[bad.first].begin() + i);
break;
}
}
for (int i = 0; i < (int)adj[bad.second].size(); i++) {
if (adj[bad.second][i] == bad.first) {
adj[bad.second].erase(adj[bad.second].begin() + i);
break;
}
}
dfs2(bad.first, -1);
int x = min({
ans(bad.first, 0, 0, 0, 1),
ans(bad.first, 0, 0, 0, 0),
ans(bad.first, 1, 0, 1, 0),
ans(bad.first, 1, 0, 1, 1)
});
cout << (x >= inf ? -1 : x) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15196 KB |
Output is correct |
2 |
Correct |
2 ms |
15196 KB |
Output is correct |
3 |
Correct |
2 ms |
15196 KB |
Output is correct |
4 |
Correct |
2 ms |
15200 KB |
Output is correct |
5 |
Correct |
149 ms |
29468 KB |
Output is correct |
6 |
Correct |
161 ms |
29252 KB |
Output is correct |
7 |
Correct |
144 ms |
29264 KB |
Output is correct |
8 |
Correct |
167 ms |
29264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15196 KB |
Output is correct |
2 |
Correct |
2 ms |
15196 KB |
Output is correct |
3 |
Correct |
2 ms |
15196 KB |
Output is correct |
4 |
Correct |
2 ms |
15448 KB |
Output is correct |
5 |
Correct |
2 ms |
15196 KB |
Output is correct |
6 |
Correct |
3 ms |
15192 KB |
Output is correct |
7 |
Correct |
2 ms |
15196 KB |
Output is correct |
8 |
Correct |
2 ms |
15192 KB |
Output is correct |
9 |
Correct |
2 ms |
15196 KB |
Output is correct |
10 |
Correct |
2 ms |
15196 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
2 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15196 KB |
Output is correct |
2 |
Correct |
2 ms |
15196 KB |
Output is correct |
3 |
Correct |
2 ms |
15196 KB |
Output is correct |
4 |
Correct |
2 ms |
15448 KB |
Output is correct |
5 |
Correct |
2 ms |
15196 KB |
Output is correct |
6 |
Correct |
3 ms |
15192 KB |
Output is correct |
7 |
Correct |
2 ms |
15196 KB |
Output is correct |
8 |
Correct |
2 ms |
15192 KB |
Output is correct |
9 |
Correct |
2 ms |
15196 KB |
Output is correct |
10 |
Correct |
2 ms |
15196 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
2 ms |
15196 KB |
Output is correct |
13 |
Correct |
3 ms |
15452 KB |
Output is correct |
14 |
Correct |
3 ms |
15196 KB |
Output is correct |
15 |
Correct |
3 ms |
15196 KB |
Output is correct |
16 |
Correct |
3 ms |
15196 KB |
Output is correct |
17 |
Correct |
2 ms |
15192 KB |
Output is correct |
18 |
Correct |
4 ms |
15196 KB |
Output is correct |
19 |
Correct |
2 ms |
15196 KB |
Output is correct |
20 |
Correct |
3 ms |
15196 KB |
Output is correct |
21 |
Correct |
3 ms |
15452 KB |
Output is correct |
22 |
Correct |
3 ms |
15448 KB |
Output is correct |
23 |
Correct |
3 ms |
15452 KB |
Output is correct |
24 |
Correct |
3 ms |
15452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15196 KB |
Output is correct |
2 |
Correct |
2 ms |
15196 KB |
Output is correct |
3 |
Correct |
2 ms |
15196 KB |
Output is correct |
4 |
Correct |
2 ms |
15200 KB |
Output is correct |
5 |
Correct |
149 ms |
29468 KB |
Output is correct |
6 |
Correct |
161 ms |
29252 KB |
Output is correct |
7 |
Correct |
144 ms |
29264 KB |
Output is correct |
8 |
Correct |
167 ms |
29264 KB |
Output is correct |
9 |
Correct |
2 ms |
15196 KB |
Output is correct |
10 |
Correct |
2 ms |
15196 KB |
Output is correct |
11 |
Correct |
2 ms |
15196 KB |
Output is correct |
12 |
Correct |
2 ms |
15448 KB |
Output is correct |
13 |
Correct |
2 ms |
15196 KB |
Output is correct |
14 |
Correct |
3 ms |
15192 KB |
Output is correct |
15 |
Correct |
2 ms |
15196 KB |
Output is correct |
16 |
Correct |
2 ms |
15192 KB |
Output is correct |
17 |
Correct |
2 ms |
15196 KB |
Output is correct |
18 |
Correct |
2 ms |
15196 KB |
Output is correct |
19 |
Correct |
3 ms |
15196 KB |
Output is correct |
20 |
Correct |
2 ms |
15196 KB |
Output is correct |
21 |
Correct |
3 ms |
15452 KB |
Output is correct |
22 |
Correct |
3 ms |
15196 KB |
Output is correct |
23 |
Correct |
3 ms |
15196 KB |
Output is correct |
24 |
Correct |
3 ms |
15196 KB |
Output is correct |
25 |
Correct |
2 ms |
15192 KB |
Output is correct |
26 |
Correct |
4 ms |
15196 KB |
Output is correct |
27 |
Correct |
2 ms |
15196 KB |
Output is correct |
28 |
Correct |
3 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
15452 KB |
Output is correct |
30 |
Correct |
3 ms |
15448 KB |
Output is correct |
31 |
Correct |
3 ms |
15452 KB |
Output is correct |
32 |
Correct |
3 ms |
15452 KB |
Output is correct |
33 |
Correct |
91 ms |
18436 KB |
Output is correct |
34 |
Correct |
85 ms |
18520 KB |
Output is correct |
35 |
Correct |
102 ms |
18592 KB |
Output is correct |
36 |
Correct |
107 ms |
18552 KB |
Output is correct |
37 |
Correct |
22 ms |
16220 KB |
Output is correct |
38 |
Correct |
90 ms |
18512 KB |
Output is correct |
39 |
Correct |
9 ms |
15448 KB |
Output is correct |
40 |
Correct |
95 ms |
18656 KB |
Output is correct |
41 |
Correct |
65 ms |
19224 KB |
Output is correct |
42 |
Correct |
69 ms |
19260 KB |
Output is correct |
43 |
Correct |
133 ms |
29464 KB |
Output is correct |
44 |
Correct |
118 ms |
24396 KB |
Output is correct |