# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x << " = " << x << endl;
# define pll pair <ll, ll>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 1e5 + 25;
const ll inf = 2e9 + 0;
const ll mod = 1e9 + 123;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
ll n, par[maxn];
vector <ll> g[maxn];
ll root, spec;
ll dp[maxn][2][2][2][2];
ll find_set (ll v)
{
if (v == par[v]) return v;
return par[v] = find_set(par[v]);
}
void build (ll v, ll pa)
{
par[v] = pa;
for (ll to : g[v])
{
if (to == pa) continue;
build(to, v);
}
}
ll solve (ll v, bool col_me, bool col_par, bool col_root, bool col_spec)
{
if (dp[v][col_me][col_par][col_root][col_spec] != -1) return dp[v][col_me][col_par][col_root][col_spec];
if (v == root && col_me != col_root) return dp[v][col_me][col_par][col_root][col_spec] = inf;
if (v == spec && col_me != col_spec) return dp[v][col_me][col_par][col_root][col_spec] = inf;
if (v == spec && col_root && col_par) return dp[v][col_me][col_par][col_root][col_spec] = inf;
bool boo = 0;
if (col_par) boo = 1;
if (v == root && col_spec) boo = 1;
if (v == spec && col_root) boo = 1;
ll sum = (ll)col_me;
for (ll to : g[v])
{
if (to == par[v]) continue;
sum += solve(to, 0, col_me, col_root, col_spec);
}
ll val = inf;
if (boo)
{
val = sum;
}
else
{
for (ll to : g[v])
{
if (to == par[v]) continue;
ll cur = sum - solve(to, 0, col_me, col_root, col_spec) + solve(to, 1, col_me, col_root, col_spec);
val = min(val, cur);
}
}
return dp[v][col_me][col_par][col_root][col_spec] = val;
}
void ma1n (/* SABR */)
{
cin >> n;
for (ll i = 1; i <= n; ++i)
{
par[i] = i;
}
memset(dp, -1, sizeof(dp));
for (ll i = 1, u, v; i <= n; ++i)
{
cin >> u >> v;
ll cmp1 = find_set(u);
ll cmp2 = find_set(v);
if (cmp1 == cmp2)
{
root = u;
spec = v;
}
else
{
par[cmp2] = cmp1;
g[u].pb(v);
g[v].pb(u);
}
}
build(root, root);
ll ans = inf;
ans = min(ans, solve(root, 0, 0, 0, 0));
ans = min(ans, solve(root, 1, 0, 1, 0));
ans = min(ans, solve(root, 0, 0, 0, 1));
ans = min(ans, solve(root, 1, 0, 1, 1));
if (ans == inf) ans = -1;
cout << ans << nl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("spainting.in", "r", stdin);
// freopen("spainting.out", "w", stdout);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << ' ';
ma1n();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15188 KB |
Output is correct |
2 |
Correct |
7 ms |
15140 KB |
Output is correct |
3 |
Correct |
7 ms |
15204 KB |
Output is correct |
4 |
Correct |
9 ms |
15092 KB |
Output is correct |
5 |
Correct |
209 ms |
31984 KB |
Output is correct |
6 |
Correct |
201 ms |
31968 KB |
Output is correct |
7 |
Correct |
202 ms |
32016 KB |
Output is correct |
8 |
Correct |
220 ms |
31984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15188 KB |
Output is correct |
2 |
Correct |
7 ms |
15188 KB |
Output is correct |
3 |
Correct |
7 ms |
15188 KB |
Output is correct |
4 |
Correct |
7 ms |
15188 KB |
Output is correct |
5 |
Correct |
6 ms |
15188 KB |
Output is correct |
6 |
Correct |
6 ms |
15188 KB |
Output is correct |
7 |
Correct |
8 ms |
15188 KB |
Output is correct |
8 |
Correct |
7 ms |
15188 KB |
Output is correct |
9 |
Correct |
7 ms |
15120 KB |
Output is correct |
10 |
Correct |
6 ms |
15188 KB |
Output is correct |
11 |
Correct |
7 ms |
15188 KB |
Output is correct |
12 |
Correct |
8 ms |
15088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15188 KB |
Output is correct |
2 |
Correct |
7 ms |
15188 KB |
Output is correct |
3 |
Correct |
7 ms |
15188 KB |
Output is correct |
4 |
Correct |
7 ms |
15188 KB |
Output is correct |
5 |
Correct |
6 ms |
15188 KB |
Output is correct |
6 |
Correct |
6 ms |
15188 KB |
Output is correct |
7 |
Correct |
8 ms |
15188 KB |
Output is correct |
8 |
Correct |
7 ms |
15188 KB |
Output is correct |
9 |
Correct |
7 ms |
15120 KB |
Output is correct |
10 |
Correct |
6 ms |
15188 KB |
Output is correct |
11 |
Correct |
7 ms |
15188 KB |
Output is correct |
12 |
Correct |
8 ms |
15088 KB |
Output is correct |
13 |
Correct |
7 ms |
15228 KB |
Output is correct |
14 |
Correct |
7 ms |
15216 KB |
Output is correct |
15 |
Correct |
7 ms |
15188 KB |
Output is correct |
16 |
Correct |
7 ms |
15208 KB |
Output is correct |
17 |
Correct |
7 ms |
15188 KB |
Output is correct |
18 |
Correct |
7 ms |
15188 KB |
Output is correct |
19 |
Correct |
6 ms |
15188 KB |
Output is correct |
20 |
Correct |
7 ms |
15188 KB |
Output is correct |
21 |
Correct |
7 ms |
15188 KB |
Output is correct |
22 |
Correct |
10 ms |
15232 KB |
Output is correct |
23 |
Correct |
7 ms |
15444 KB |
Output is correct |
24 |
Correct |
8 ms |
15228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15188 KB |
Output is correct |
2 |
Correct |
7 ms |
15140 KB |
Output is correct |
3 |
Correct |
7 ms |
15204 KB |
Output is correct |
4 |
Correct |
9 ms |
15092 KB |
Output is correct |
5 |
Correct |
209 ms |
31984 KB |
Output is correct |
6 |
Correct |
201 ms |
31968 KB |
Output is correct |
7 |
Correct |
202 ms |
32016 KB |
Output is correct |
8 |
Correct |
220 ms |
31984 KB |
Output is correct |
9 |
Correct |
7 ms |
15188 KB |
Output is correct |
10 |
Correct |
7 ms |
15188 KB |
Output is correct |
11 |
Correct |
7 ms |
15188 KB |
Output is correct |
12 |
Correct |
7 ms |
15188 KB |
Output is correct |
13 |
Correct |
6 ms |
15188 KB |
Output is correct |
14 |
Correct |
6 ms |
15188 KB |
Output is correct |
15 |
Correct |
8 ms |
15188 KB |
Output is correct |
16 |
Correct |
7 ms |
15188 KB |
Output is correct |
17 |
Correct |
7 ms |
15120 KB |
Output is correct |
18 |
Correct |
6 ms |
15188 KB |
Output is correct |
19 |
Correct |
7 ms |
15188 KB |
Output is correct |
20 |
Correct |
8 ms |
15088 KB |
Output is correct |
21 |
Correct |
7 ms |
15228 KB |
Output is correct |
22 |
Correct |
7 ms |
15216 KB |
Output is correct |
23 |
Correct |
7 ms |
15188 KB |
Output is correct |
24 |
Correct |
7 ms |
15208 KB |
Output is correct |
25 |
Correct |
7 ms |
15188 KB |
Output is correct |
26 |
Correct |
7 ms |
15188 KB |
Output is correct |
27 |
Correct |
6 ms |
15188 KB |
Output is correct |
28 |
Correct |
7 ms |
15188 KB |
Output is correct |
29 |
Correct |
7 ms |
15188 KB |
Output is correct |
30 |
Correct |
10 ms |
15232 KB |
Output is correct |
31 |
Correct |
7 ms |
15444 KB |
Output is correct |
32 |
Correct |
8 ms |
15228 KB |
Output is correct |
33 |
Correct |
142 ms |
20076 KB |
Output is correct |
34 |
Correct |
121 ms |
20144 KB |
Output is correct |
35 |
Correct |
133 ms |
20148 KB |
Output is correct |
36 |
Correct |
127 ms |
20152 KB |
Output is correct |
37 |
Correct |
28 ms |
16340 KB |
Output is correct |
38 |
Correct |
121 ms |
20156 KB |
Output is correct |
39 |
Correct |
14 ms |
15624 KB |
Output is correct |
40 |
Correct |
118 ms |
20052 KB |
Output is correct |
41 |
Correct |
69 ms |
21128 KB |
Output is correct |
42 |
Correct |
125 ms |
20956 KB |
Output is correct |
43 |
Correct |
158 ms |
32760 KB |
Output is correct |
44 |
Correct |
127 ms |
26820 KB |
Output is correct |