#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const int INF = 1'000'000'007;
struct union_find {
vi p, rank;
union_find(int n) {
p.resize(n);
rep(i, 0, n) p[i] = i;
rank.assign(n, 0);
}
int find(int i) { return p[i] == i ? i : p[i] = find(p[i]); }
bool same(int i, int j) { return find(i) == find(j); }
void union_set(int i, int j) {
i = find(i);
j = find(j);
if (i == j) return;
if (rank[i] < rank[j]) swap(i, j);
p[j] = i;
if (rank[i] == rank[j]) ++rank[i];
}
};
vi graph[100001];
int root, rem, memo[100001][2][2][2][2], dfs_parent[100001];
int dp(int u, bool take_cur, bool parent, bool taken, bool root_taken) {
if (u == rem) {
if (taken != take_cur) return INF;
if (root_taken && parent) return INF;
}
int &ans = memo[u][take_cur][parent][taken][root_taken];
if (ans != -1) return ans;
if(u==rem)parent |= root_taken;
ans = 0;
int cnt = 0;
ll mn = INF,cur_ans=0;
trav(v, graph[u]) if (v != dfs_parent[u]) {
dfs_parent[v]=u;
++cnt;
ll val = dp(v, 0, take_cur, taken, root_taken);
cur_ans+=val;
if (!parent) {
ll diff = dp(v,1,take_cur,taken,root_taken)-val;
mn=min(mn,diff);
}
}
if(!parent&&!cnt)return ans= INF;
if(!parent)ans=min((ll)INF,cur_ans+mn);
else ans=min((ll)INF,cur_ans);
//cout<<"u = "<<u+1<<" take_cur = "<<take_cur<<" parent = "<<parent<<" ans = "<<ans<<endl;
return ans+take_cur;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
memset(memo, -1, sizeof(memo));
int n;
cin >> n;
union_find ufds(n);
rep(i, 0, n) {
int u, v;
cin >> u >> v;
if (ufds.same(--u, --v)) {
root = u;
rem = v;
continue;
}
graph[u].pb(v);
graph[v].pb(u);
ufds.union_set(u, v);
}
dfs_parent[root] = root;
int ans = INF;
ans = dp(root, 1, 0, 0, 1);
ans = min(ans, dp(root, 1, 1, 1, 1));
ans = min(ans, dp(root, 0, 0, 0, 0));
ans = min(ans, dp(root, 0, 1, 1, 0));
if (ans > n) ans = -1;
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8908 KB |
Output is correct |
2 |
Correct |
4 ms |
8908 KB |
Output is correct |
3 |
Correct |
4 ms |
8908 KB |
Output is correct |
4 |
Correct |
4 ms |
8908 KB |
Output is correct |
5 |
Correct |
203 ms |
27232 KB |
Output is correct |
6 |
Correct |
202 ms |
27312 KB |
Output is correct |
7 |
Correct |
211 ms |
27308 KB |
Output is correct |
8 |
Correct |
190 ms |
27312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8908 KB |
Output is correct |
2 |
Correct |
4 ms |
8912 KB |
Output is correct |
3 |
Correct |
4 ms |
8908 KB |
Output is correct |
4 |
Correct |
4 ms |
8908 KB |
Output is correct |
5 |
Correct |
4 ms |
8908 KB |
Output is correct |
6 |
Correct |
4 ms |
8864 KB |
Output is correct |
7 |
Correct |
4 ms |
8908 KB |
Output is correct |
8 |
Correct |
4 ms |
8908 KB |
Output is correct |
9 |
Correct |
4 ms |
8908 KB |
Output is correct |
10 |
Correct |
4 ms |
8884 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
4 ms |
8908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8908 KB |
Output is correct |
2 |
Correct |
4 ms |
8912 KB |
Output is correct |
3 |
Correct |
4 ms |
8908 KB |
Output is correct |
4 |
Correct |
4 ms |
8908 KB |
Output is correct |
5 |
Correct |
4 ms |
8908 KB |
Output is correct |
6 |
Correct |
4 ms |
8864 KB |
Output is correct |
7 |
Correct |
4 ms |
8908 KB |
Output is correct |
8 |
Correct |
4 ms |
8908 KB |
Output is correct |
9 |
Correct |
4 ms |
8908 KB |
Output is correct |
10 |
Correct |
4 ms |
8884 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
4 ms |
8908 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
4 ms |
8928 KB |
Output is correct |
15 |
Correct |
4 ms |
8908 KB |
Output is correct |
16 |
Correct |
4 ms |
8928 KB |
Output is correct |
17 |
Correct |
4 ms |
8908 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
19 |
Correct |
4 ms |
8908 KB |
Output is correct |
20 |
Correct |
5 ms |
8908 KB |
Output is correct |
21 |
Correct |
4 ms |
8908 KB |
Output is correct |
22 |
Correct |
5 ms |
8960 KB |
Output is correct |
23 |
Correct |
5 ms |
9036 KB |
Output is correct |
24 |
Correct |
7 ms |
9036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8908 KB |
Output is correct |
2 |
Correct |
4 ms |
8908 KB |
Output is correct |
3 |
Correct |
4 ms |
8908 KB |
Output is correct |
4 |
Correct |
4 ms |
8908 KB |
Output is correct |
5 |
Correct |
203 ms |
27232 KB |
Output is correct |
6 |
Correct |
202 ms |
27312 KB |
Output is correct |
7 |
Correct |
211 ms |
27308 KB |
Output is correct |
8 |
Correct |
190 ms |
27312 KB |
Output is correct |
9 |
Correct |
4 ms |
8908 KB |
Output is correct |
10 |
Correct |
4 ms |
8912 KB |
Output is correct |
11 |
Correct |
4 ms |
8908 KB |
Output is correct |
12 |
Correct |
4 ms |
8908 KB |
Output is correct |
13 |
Correct |
4 ms |
8908 KB |
Output is correct |
14 |
Correct |
4 ms |
8864 KB |
Output is correct |
15 |
Correct |
4 ms |
8908 KB |
Output is correct |
16 |
Correct |
4 ms |
8908 KB |
Output is correct |
17 |
Correct |
4 ms |
8908 KB |
Output is correct |
18 |
Correct |
4 ms |
8884 KB |
Output is correct |
19 |
Correct |
5 ms |
8908 KB |
Output is correct |
20 |
Correct |
4 ms |
8908 KB |
Output is correct |
21 |
Correct |
5 ms |
8908 KB |
Output is correct |
22 |
Correct |
4 ms |
8928 KB |
Output is correct |
23 |
Correct |
4 ms |
8908 KB |
Output is correct |
24 |
Correct |
4 ms |
8928 KB |
Output is correct |
25 |
Correct |
4 ms |
8908 KB |
Output is correct |
26 |
Correct |
5 ms |
8908 KB |
Output is correct |
27 |
Correct |
4 ms |
8908 KB |
Output is correct |
28 |
Correct |
5 ms |
8908 KB |
Output is correct |
29 |
Correct |
4 ms |
8908 KB |
Output is correct |
30 |
Correct |
5 ms |
8960 KB |
Output is correct |
31 |
Correct |
5 ms |
9036 KB |
Output is correct |
32 |
Correct |
7 ms |
9036 KB |
Output is correct |
33 |
Correct |
127 ms |
12948 KB |
Output is correct |
34 |
Correct |
104 ms |
12740 KB |
Output is correct |
35 |
Correct |
107 ms |
12804 KB |
Output is correct |
36 |
Correct |
108 ms |
12808 KB |
Output is correct |
37 |
Correct |
21 ms |
9908 KB |
Output is correct |
38 |
Correct |
115 ms |
12868 KB |
Output is correct |
39 |
Correct |
12 ms |
9164 KB |
Output is correct |
40 |
Correct |
120 ms |
12904 KB |
Output is correct |
41 |
Correct |
54 ms |
13500 KB |
Output is correct |
42 |
Correct |
66 ms |
13520 KB |
Output is correct |
43 |
Correct |
152 ms |
27192 KB |
Output is correct |
44 |
Correct |
124 ms |
20464 KB |
Output is correct |