#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 3e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
struct DSU{
vector<int> lab;
DSU(){}
DSU(int _n)
{
lab.resize(_n + 7 , -1);
}
int root(int u) {return lab[u] < 0 ? u : lab[u] = root(lab[u]);};
bool union_root(int u , int v)
{
u = root(u);
v = root(v);
if(u == v) return false;
if(lab[u] > lab[v]) swap(u , v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
} dsu;
int n;
vector<int> adj[mxN];
pair<int , int> edges[mxN];
bool marked[mxN];
int root;
int spec;
int dp[mxN][2][2][2][2];
int par[mxN];
void dfs(int u , int pa)
{
for(int v : adj[u]) {
if(v == pa) continue;
par[v] = u;
dfs(v , u);
}
}
int sol(int u , int now , int pr , int c1 , int c2)
{
if(u == root && now != c1) return inf;
if(u == spec && now != c2) return inf;
if(u == spec && pr == true && c1 == true) return inf;
int &res = dp[u][now][pr][c1][c2];
if(res != -1) return res;
res = inf;
bool ok = false;
if(pr == true) ok = true;
if(u == root && c2 == true) ok = true;
if(u == spec && c1 == true) ok = true;
ll f1 = now;
ll f2 = inf;
for(int v : adj[u]) {
if(v == par[u]) continue;
f1 += sol(v , 0 , now , c1 , c2);
f2 = min(f2 , 1LL * sol(v , 1 , now , c1 , c2) - sol(v , 0 , now , c1 , c2));
}
if(ok == true) minimize(res , f1);
else minimize(res , f1 + f2);
return res;
}
void solve()
{
cin >> n;
for(int i = 1 ; i <= n ; i++) cin >> edges[i].fi >> edges[i].se;
dsu = DSU(n);
for(int i = 1 ; i <= n ; i++) {
if(dsu.union_root(edges[i].fi , edges[i].se) == true) {
marked[i] = true;
adj[edges[i].fi].pb(edges[i].se);
adj[edges[i].se].pb(edges[i].fi);
}
}
for(int i = 1 ; i <= n ; i++) {
if(marked[i] == false) {
root = edges[i].fi;
spec = edges[i].se;
}
}
dfs(root , -1);
memset(dp , -1 , sizeof(dp));
int ans = inf;
for(int c1 = 0 ; c1 <= 1 ; c1++)
for(int c2 = 0 ; c2 <= 1 ; c2++) minimize(ans , sol(root , c1 , 0 , c1 , c2));
if(ans == inf) cout << -1;
else cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen(TASK".in" , "r" , stdin);
//freopen(TASK".out" , "w" , stdout);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
26116 KB |
Output is correct |
2 |
Correct |
13 ms |
26064 KB |
Output is correct |
3 |
Correct |
11 ms |
26060 KB |
Output is correct |
4 |
Correct |
12 ms |
26056 KB |
Output is correct |
5 |
Correct |
217 ms |
46160 KB |
Output is correct |
6 |
Correct |
201 ms |
46136 KB |
Output is correct |
7 |
Correct |
193 ms |
46208 KB |
Output is correct |
8 |
Correct |
196 ms |
46144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
26052 KB |
Output is correct |
2 |
Correct |
11 ms |
26056 KB |
Output is correct |
3 |
Correct |
12 ms |
26052 KB |
Output is correct |
4 |
Correct |
14 ms |
26056 KB |
Output is correct |
5 |
Correct |
10 ms |
26188 KB |
Output is correct |
6 |
Correct |
11 ms |
26188 KB |
Output is correct |
7 |
Correct |
12 ms |
26052 KB |
Output is correct |
8 |
Correct |
12 ms |
26060 KB |
Output is correct |
9 |
Correct |
12 ms |
26188 KB |
Output is correct |
10 |
Correct |
12 ms |
26060 KB |
Output is correct |
11 |
Correct |
12 ms |
26076 KB |
Output is correct |
12 |
Correct |
13 ms |
26084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
26052 KB |
Output is correct |
2 |
Correct |
11 ms |
26056 KB |
Output is correct |
3 |
Correct |
12 ms |
26052 KB |
Output is correct |
4 |
Correct |
14 ms |
26056 KB |
Output is correct |
5 |
Correct |
10 ms |
26188 KB |
Output is correct |
6 |
Correct |
11 ms |
26188 KB |
Output is correct |
7 |
Correct |
12 ms |
26052 KB |
Output is correct |
8 |
Correct |
12 ms |
26060 KB |
Output is correct |
9 |
Correct |
12 ms |
26188 KB |
Output is correct |
10 |
Correct |
12 ms |
26060 KB |
Output is correct |
11 |
Correct |
12 ms |
26076 KB |
Output is correct |
12 |
Correct |
13 ms |
26084 KB |
Output is correct |
13 |
Correct |
13 ms |
26188 KB |
Output is correct |
14 |
Correct |
12 ms |
26192 KB |
Output is correct |
15 |
Correct |
12 ms |
26192 KB |
Output is correct |
16 |
Correct |
12 ms |
26220 KB |
Output is correct |
17 |
Correct |
15 ms |
26188 KB |
Output is correct |
18 |
Correct |
12 ms |
26192 KB |
Output is correct |
19 |
Correct |
11 ms |
26192 KB |
Output is correct |
20 |
Correct |
14 ms |
26192 KB |
Output is correct |
21 |
Correct |
12 ms |
26184 KB |
Output is correct |
22 |
Correct |
12 ms |
26192 KB |
Output is correct |
23 |
Correct |
12 ms |
26380 KB |
Output is correct |
24 |
Correct |
12 ms |
26192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
26116 KB |
Output is correct |
2 |
Correct |
13 ms |
26064 KB |
Output is correct |
3 |
Correct |
11 ms |
26060 KB |
Output is correct |
4 |
Correct |
12 ms |
26056 KB |
Output is correct |
5 |
Correct |
217 ms |
46160 KB |
Output is correct |
6 |
Correct |
201 ms |
46136 KB |
Output is correct |
7 |
Correct |
193 ms |
46208 KB |
Output is correct |
8 |
Correct |
196 ms |
46144 KB |
Output is correct |
9 |
Correct |
11 ms |
26052 KB |
Output is correct |
10 |
Correct |
11 ms |
26056 KB |
Output is correct |
11 |
Correct |
12 ms |
26052 KB |
Output is correct |
12 |
Correct |
14 ms |
26056 KB |
Output is correct |
13 |
Correct |
10 ms |
26188 KB |
Output is correct |
14 |
Correct |
11 ms |
26188 KB |
Output is correct |
15 |
Correct |
12 ms |
26052 KB |
Output is correct |
16 |
Correct |
12 ms |
26060 KB |
Output is correct |
17 |
Correct |
12 ms |
26188 KB |
Output is correct |
18 |
Correct |
12 ms |
26060 KB |
Output is correct |
19 |
Correct |
12 ms |
26076 KB |
Output is correct |
20 |
Correct |
13 ms |
26084 KB |
Output is correct |
21 |
Correct |
13 ms |
26188 KB |
Output is correct |
22 |
Correct |
12 ms |
26192 KB |
Output is correct |
23 |
Correct |
12 ms |
26192 KB |
Output is correct |
24 |
Correct |
12 ms |
26220 KB |
Output is correct |
25 |
Correct |
15 ms |
26188 KB |
Output is correct |
26 |
Correct |
12 ms |
26192 KB |
Output is correct |
27 |
Correct |
11 ms |
26192 KB |
Output is correct |
28 |
Correct |
14 ms |
26192 KB |
Output is correct |
29 |
Correct |
12 ms |
26184 KB |
Output is correct |
30 |
Correct |
12 ms |
26192 KB |
Output is correct |
31 |
Correct |
12 ms |
26380 KB |
Output is correct |
32 |
Correct |
12 ms |
26192 KB |
Output is correct |
33 |
Correct |
120 ms |
31536 KB |
Output is correct |
34 |
Correct |
148 ms |
31476 KB |
Output is correct |
35 |
Correct |
149 ms |
31448 KB |
Output is correct |
36 |
Correct |
120 ms |
31420 KB |
Output is correct |
37 |
Correct |
36 ms |
27488 KB |
Output is correct |
38 |
Correct |
128 ms |
31500 KB |
Output is correct |
39 |
Correct |
18 ms |
26592 KB |
Output is correct |
40 |
Correct |
133 ms |
31356 KB |
Output is correct |
41 |
Correct |
80 ms |
32416 KB |
Output is correct |
42 |
Correct |
97 ms |
32500 KB |
Output is correct |
43 |
Correct |
225 ms |
46204 KB |
Output is correct |
44 |
Correct |
143 ms |
39232 KB |
Output is correct |