#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define sz(x) ((int)(x).size())
constexpr int N = 1e5 + 5;
constexpr int INF = 1e9;
int dp[N][2][2][2][2];
vector<int> v[N];
int par[N];
int u,w;
int find(int a){
if(par[a]==a) return a;
return par[a]=find(par[a]);
}
bool unite(int a,int b){
a=find(a),b=find(b);
if(a==b) return 0;
par[a]=b;
return 1;
}
int dfs(int c,int p,int fl1,int fl2,int rt,int sp){
if(c==u && fl1!=rt) return INF;
if(c==w && fl1!=sp) return INF;
if(c==w && fl2 && rt) return INF;
if(dp[c][fl1][fl2][rt][sp]!=-1) return dp[c][fl1][fl2][rt][sp];
int hm = fl2 + (c==u && sp) + (c==w && rt);
if(hm>=2) return INF;
int cur = 0;
for(int x:v[c]){
if(x==p) continue;
cur+=dfs(x,c,0,fl1,rt,sp);
}
if(hm==1){
return dp[c][fl1][fl2][rt][sp]=cur+fl1;
}
else{
int lol = INF;
for(int x:v[c]){
if(x==p) continue;
int xd = cur - dfs(x,c,0,fl1,rt,sp);
xd+=dfs(x,c,1,fl1,rt,sp);
lol=min(lol,xd);
}
return dp[c][fl1][fl2][rt][sp]=lol+fl1;
}
}
void solve(){
int n;
cin >> n;
memset(dp,-1,sizeof(dp));
iota(par,par+N,0LL);
for(int i=1;i<=n;i++){
int a,b;
cin >> a >> b;
v[a].pb(b);
v[b].pb(a);
if(!unite(a,b)){
u=a;
w=b;
}
}
for(int i=0;i<sz(v[u]);i++){
if(v[u][i]==w) v[u].erase(v[u].begin()+i);
}
for(int i=0;i<sz(v[w]);i++){
if(v[w][i]==u) v[w].erase(v[w].begin()+i);
}
int ans = INF;
ans=min(ans,dfs(u,u,1,0,1,0));
ans=min(ans,dfs(u,u,1,0,1,1));
ans=min(ans,dfs(u,u,0,0,0,0));
ans=min(ans,dfs(u,u,0,0,0,1));
if(ans>n) cout << -1 << endl;
else cout << ans << endl;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;//cin >> t;
while(t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15960 KB |
Output is correct |
2 |
Correct |
3 ms |
15964 KB |
Output is correct |
3 |
Correct |
3 ms |
15964 KB |
Output is correct |
4 |
Correct |
3 ms |
15964 KB |
Output is correct |
5 |
Correct |
144 ms |
30036 KB |
Output is correct |
6 |
Correct |
186 ms |
30036 KB |
Output is correct |
7 |
Correct |
143 ms |
30176 KB |
Output is correct |
8 |
Correct |
157 ms |
30176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15964 KB |
Output is correct |
2 |
Correct |
3 ms |
16092 KB |
Output is correct |
3 |
Correct |
3 ms |
15964 KB |
Output is correct |
4 |
Correct |
3 ms |
15960 KB |
Output is correct |
5 |
Correct |
3 ms |
15960 KB |
Output is correct |
6 |
Correct |
3 ms |
15964 KB |
Output is correct |
7 |
Correct |
3 ms |
15964 KB |
Output is correct |
8 |
Correct |
3 ms |
15988 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
3 ms |
15964 KB |
Output is correct |
11 |
Correct |
3 ms |
15960 KB |
Output is correct |
12 |
Correct |
3 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15964 KB |
Output is correct |
2 |
Correct |
3 ms |
16092 KB |
Output is correct |
3 |
Correct |
3 ms |
15964 KB |
Output is correct |
4 |
Correct |
3 ms |
15960 KB |
Output is correct |
5 |
Correct |
3 ms |
15960 KB |
Output is correct |
6 |
Correct |
3 ms |
15964 KB |
Output is correct |
7 |
Correct |
3 ms |
15964 KB |
Output is correct |
8 |
Correct |
3 ms |
15988 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
3 ms |
15964 KB |
Output is correct |
11 |
Correct |
3 ms |
15960 KB |
Output is correct |
12 |
Correct |
3 ms |
15964 KB |
Output is correct |
13 |
Correct |
5 ms |
15964 KB |
Output is correct |
14 |
Correct |
5 ms |
15960 KB |
Output is correct |
15 |
Correct |
3 ms |
15964 KB |
Output is correct |
16 |
Correct |
3 ms |
15964 KB |
Output is correct |
17 |
Correct |
3 ms |
15964 KB |
Output is correct |
18 |
Correct |
4 ms |
15964 KB |
Output is correct |
19 |
Correct |
3 ms |
15964 KB |
Output is correct |
20 |
Correct |
3 ms |
15960 KB |
Output is correct |
21 |
Correct |
3 ms |
15964 KB |
Output is correct |
22 |
Correct |
3 ms |
15964 KB |
Output is correct |
23 |
Correct |
4 ms |
16232 KB |
Output is correct |
24 |
Correct |
3 ms |
16204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15960 KB |
Output is correct |
2 |
Correct |
3 ms |
15964 KB |
Output is correct |
3 |
Correct |
3 ms |
15964 KB |
Output is correct |
4 |
Correct |
3 ms |
15964 KB |
Output is correct |
5 |
Correct |
144 ms |
30036 KB |
Output is correct |
6 |
Correct |
186 ms |
30036 KB |
Output is correct |
7 |
Correct |
143 ms |
30176 KB |
Output is correct |
8 |
Correct |
157 ms |
30176 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
3 ms |
16092 KB |
Output is correct |
11 |
Correct |
3 ms |
15964 KB |
Output is correct |
12 |
Correct |
3 ms |
15960 KB |
Output is correct |
13 |
Correct |
3 ms |
15960 KB |
Output is correct |
14 |
Correct |
3 ms |
15964 KB |
Output is correct |
15 |
Correct |
3 ms |
15964 KB |
Output is correct |
16 |
Correct |
3 ms |
15988 KB |
Output is correct |
17 |
Correct |
3 ms |
15964 KB |
Output is correct |
18 |
Correct |
3 ms |
15964 KB |
Output is correct |
19 |
Correct |
3 ms |
15960 KB |
Output is correct |
20 |
Correct |
3 ms |
15964 KB |
Output is correct |
21 |
Correct |
5 ms |
15964 KB |
Output is correct |
22 |
Correct |
5 ms |
15960 KB |
Output is correct |
23 |
Correct |
3 ms |
15964 KB |
Output is correct |
24 |
Correct |
3 ms |
15964 KB |
Output is correct |
25 |
Correct |
3 ms |
15964 KB |
Output is correct |
26 |
Correct |
4 ms |
15964 KB |
Output is correct |
27 |
Correct |
3 ms |
15964 KB |
Output is correct |
28 |
Correct |
3 ms |
15960 KB |
Output is correct |
29 |
Correct |
3 ms |
15964 KB |
Output is correct |
30 |
Correct |
3 ms |
15964 KB |
Output is correct |
31 |
Correct |
4 ms |
16232 KB |
Output is correct |
32 |
Correct |
3 ms |
16204 KB |
Output is correct |
33 |
Correct |
108 ms |
20292 KB |
Output is correct |
34 |
Correct |
77 ms |
20316 KB |
Output is correct |
35 |
Correct |
72 ms |
20312 KB |
Output is correct |
36 |
Correct |
94 ms |
20348 KB |
Output is correct |
37 |
Correct |
20 ms |
16988 KB |
Output is correct |
38 |
Correct |
83 ms |
20432 KB |
Output is correct |
39 |
Correct |
8 ms |
16476 KB |
Output is correct |
40 |
Correct |
73 ms |
20268 KB |
Output is correct |
41 |
Correct |
53 ms |
21196 KB |
Output is correct |
42 |
Correct |
67 ms |
21068 KB |
Output is correct |
43 |
Correct |
95 ms |
31312 KB |
Output is correct |
44 |
Correct |
94 ms |
26072 KB |
Output is correct |