#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl '\n'
#define all(x) x.begin(),x.end()
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl
const int N = 1e5+10,mod = 1e9+7;
vector<int>adj[N];
int head[N],par[N];
int root,special;
ll dp[N][2][2][2][2];
int st(int x)
{
if(x==head[x])return x;
return head[x] = st(head[x]);
}
void dfs(int x,int pr)
{
par[x] = pr;
for(auto u:adj[x])
{
if(u == pr)continue;
dfs(u,x);
}
}
int calc(int nd,int me,int up,int rt,int sp)
{
if(dp[nd][me][up][rt][sp]!=-1)
return dp[nd][me][up][rt][sp];
bool tr = 1;
if(nd==special && rt &&up)tr = 0;
if(nd==root && me!=rt)tr = 0;
if(nd==special && me!=sp)tr = 0;
if(!tr)
return dp[nd][me][up][rt][sp] = mod;
bool cmp = 0;
if(up)cmp = 1;
if(nd==root&&sp)cmp = 1;
if(nd==special&&rt)cmp = 1;
ll sum = me;
for(auto u:adj[nd])
{
if(u==par[nd])continue;
sum+=calc(u,0,me,rt,sp);
}
ll res = mod;
if(cmp)
{
res = min(res,sum);
}
else
{
for(auto u:adj[nd])
{
if(u==par[nd])continue;
ll val = sum - calc(u,0,me,rt,sp) + calc(u,1,me,rt,sp);
res = min(res,val);
}
}
return dp[nd][me][up][rt][sp] = res;
}
void solve()
{
int sol = mod;
for (int rt = 0; rt <= 1; rt++) {
for (int sp = 0; sp <= 1; sp++) {
sol = min(sol, calc(root, rt, 0, rt, sp));
}
}
if (sol == mod) cout << -1; else cout << sol;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i = 0;i<=n;i++)
head[i] = i;
for(int i = 0;i<n;i++)
{
int a,b;
cin>>a>>b;
int pa = st(a);
int pb = st(b);
if(pa==pb)
{
root = a;
special = b;
}
else
{
head[pa] = pb;
adj[a].pb(b);
adj[b].pb(a);
}
}
dfs(root,0);
ll ans = mod;
memset(dp,-1,sizeof dp);
solve();
return 0;
}
/*
*/
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:119:8: warning: unused variable 'ans' [-Wunused-variable]
119 | ll ans = mod;
| ^~~
# |
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 |
6 ms |
15196 KB |
Output is correct |
4 |
Correct |
6 ms |
15104 KB |
Output is correct |
5 |
Correct |
189 ms |
33172 KB |
Output is correct |
6 |
Correct |
183 ms |
33148 KB |
Output is correct |
7 |
Correct |
202 ms |
33176 KB |
Output is correct |
8 |
Correct |
187 ms |
33176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15188 KB |
Output is correct |
2 |
Correct |
7 ms |
15192 KB |
Output is correct |
3 |
Correct |
7 ms |
15188 KB |
Output is correct |
4 |
Correct |
7 ms |
15188 KB |
Output is correct |
5 |
Correct |
7 ms |
15188 KB |
Output is correct |
6 |
Correct |
7 ms |
15180 KB |
Output is correct |
7 |
Correct |
7 ms |
15188 KB |
Output is correct |
8 |
Correct |
6 ms |
15188 KB |
Output is correct |
9 |
Correct |
7 ms |
15188 KB |
Output is correct |
10 |
Correct |
7 ms |
15188 KB |
Output is correct |
11 |
Correct |
6 ms |
15188 KB |
Output is correct |
12 |
Correct |
7 ms |
15188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15188 KB |
Output is correct |
2 |
Correct |
7 ms |
15192 KB |
Output is correct |
3 |
Correct |
7 ms |
15188 KB |
Output is correct |
4 |
Correct |
7 ms |
15188 KB |
Output is correct |
5 |
Correct |
7 ms |
15188 KB |
Output is correct |
6 |
Correct |
7 ms |
15180 KB |
Output is correct |
7 |
Correct |
7 ms |
15188 KB |
Output is correct |
8 |
Correct |
6 ms |
15188 KB |
Output is correct |
9 |
Correct |
7 ms |
15188 KB |
Output is correct |
10 |
Correct |
7 ms |
15188 KB |
Output is correct |
11 |
Correct |
6 ms |
15188 KB |
Output is correct |
12 |
Correct |
7 ms |
15188 KB |
Output is correct |
13 |
Correct |
7 ms |
15192 KB |
Output is correct |
14 |
Correct |
7 ms |
15236 KB |
Output is correct |
15 |
Correct |
8 ms |
15188 KB |
Output is correct |
16 |
Correct |
7 ms |
15188 KB |
Output is correct |
17 |
Correct |
8 ms |
15136 KB |
Output is correct |
18 |
Correct |
8 ms |
15188 KB |
Output is correct |
19 |
Correct |
7 ms |
15100 KB |
Output is correct |
20 |
Correct |
7 ms |
15240 KB |
Output is correct |
21 |
Correct |
9 ms |
15188 KB |
Output is correct |
22 |
Correct |
11 ms |
15188 KB |
Output is correct |
23 |
Correct |
8 ms |
15316 KB |
Output is correct |
24 |
Correct |
8 ms |
15188 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 |
6 ms |
15196 KB |
Output is correct |
4 |
Correct |
6 ms |
15104 KB |
Output is correct |
5 |
Correct |
189 ms |
33172 KB |
Output is correct |
6 |
Correct |
183 ms |
33148 KB |
Output is correct |
7 |
Correct |
202 ms |
33176 KB |
Output is correct |
8 |
Correct |
187 ms |
33176 KB |
Output is correct |
9 |
Correct |
7 ms |
15188 KB |
Output is correct |
10 |
Correct |
7 ms |
15192 KB |
Output is correct |
11 |
Correct |
7 ms |
15188 KB |
Output is correct |
12 |
Correct |
7 ms |
15188 KB |
Output is correct |
13 |
Correct |
7 ms |
15188 KB |
Output is correct |
14 |
Correct |
7 ms |
15180 KB |
Output is correct |
15 |
Correct |
7 ms |
15188 KB |
Output is correct |
16 |
Correct |
6 ms |
15188 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 |
15192 KB |
Output is correct |
22 |
Correct |
7 ms |
15236 KB |
Output is correct |
23 |
Correct |
8 ms |
15188 KB |
Output is correct |
24 |
Correct |
7 ms |
15188 KB |
Output is correct |
25 |
Correct |
8 ms |
15136 KB |
Output is correct |
26 |
Correct |
8 ms |
15188 KB |
Output is correct |
27 |
Correct |
7 ms |
15100 KB |
Output is correct |
28 |
Correct |
7 ms |
15240 KB |
Output is correct |
29 |
Correct |
9 ms |
15188 KB |
Output is correct |
30 |
Correct |
11 ms |
15188 KB |
Output is correct |
31 |
Correct |
8 ms |
15316 KB |
Output is correct |
32 |
Correct |
8 ms |
15188 KB |
Output is correct |
33 |
Correct |
110 ms |
18712 KB |
Output is correct |
34 |
Correct |
122 ms |
18716 KB |
Output is correct |
35 |
Correct |
115 ms |
18636 KB |
Output is correct |
36 |
Correct |
110 ms |
18588 KB |
Output is correct |
37 |
Correct |
27 ms |
16084 KB |
Output is correct |
38 |
Correct |
120 ms |
18788 KB |
Output is correct |
39 |
Correct |
14 ms |
15520 KB |
Output is correct |
40 |
Correct |
112 ms |
18700 KB |
Output is correct |
41 |
Correct |
72 ms |
19788 KB |
Output is correct |
42 |
Correct |
85 ms |
19404 KB |
Output is correct |
43 |
Correct |
146 ms |
33080 KB |
Output is correct |
44 |
Correct |
129 ms |
26304 KB |
Output is correct |