#include <bits/stdc++.h>
#define pb push_back
#define ctoi(x) int(x-'0')
#define cdv(x,y) (((x)+(y)-1)/(y))
#define LOOP(n) for(int rp=0;rp<(n);rp++)
#define sz(x) int(x.size())
#define dbg(x) cout << (#x) << " : " << x << endl;
#define sq(x) ((x)*(x))
using namespace std;
typedef long long ll;
typedef long double dl;
const int SZ=2e5+7;
int n,dp[SZ][2][2][2][2],X,Y;
bool vs[SZ];
vector<int>G[SZ],g[SZ];
void dfs(int x,int pr=-1){
vs[x]=1;
for(auto&i:G[x]){
if(i==pr)continue;
if(vs[i])X=x,Y=i;
else dfs(i,x),g[x].pb(i),g[i].pb(x);
}
}
int solve(int nd,int pr,int b,int c,int u,int e){
if(nd==Y){
if(e+b!=1)return 1e9;
if(c+u>1)return 1e9;
c+=u;
}
if(pr!=-1&&sz(g[nd])==1){
if(c==0)return 1e9;
return b;
}
if(dp[nd][b][c][u][e]!=-1)return dp[nd][b][c][u][e];
ll s=0,mn=2e9;
for(auto&i:g[nd]){
if(i==pr)continue;
s+=ll(solve(i,nd,0,b,u,e));
mn=min(mn,ll(solve(i,nd,1,b,u,e)-solve(i,nd,0,b,u,e)));
}
ll s2=1e9;
if(nd==X){
s2=0;
for(auto&i:g[nd]){
if(i==pr)continue;
s2+=ll(solve(i,nd,0,b,u,0));
}
}
return dp[nd][b][c][u][e]=min(ll(1e9),min(c?s:s+mn,s2)+b);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int tt=1;
//cin >> tt;
LOOP(tt){
cin >> n;
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++){
int x,y;cin >> x >> y;
G[x].pb(y),G[y].pb(x);
}
dfs(1);
int ans=min(solve(X,-1,0,0,0,1),solve(X,-1,1,0,1,1));
if(ans==1e9)ans=-1;
cout << ans;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22364 KB |
Output is correct |
2 |
Correct |
4 ms |
22360 KB |
Output is correct |
3 |
Correct |
4 ms |
22616 KB |
Output is correct |
4 |
Correct |
4 ms |
22436 KB |
Output is correct |
5 |
Correct |
156 ms |
44052 KB |
Output is correct |
6 |
Correct |
149 ms |
43968 KB |
Output is correct |
7 |
Correct |
136 ms |
44032 KB |
Output is correct |
8 |
Correct |
142 ms |
44044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22420 KB |
Output is correct |
2 |
Correct |
4 ms |
22360 KB |
Output is correct |
3 |
Correct |
5 ms |
22360 KB |
Output is correct |
4 |
Correct |
5 ms |
22364 KB |
Output is correct |
5 |
Correct |
5 ms |
22452 KB |
Output is correct |
6 |
Correct |
5 ms |
22424 KB |
Output is correct |
7 |
Correct |
4 ms |
22364 KB |
Output is correct |
8 |
Correct |
5 ms |
22364 KB |
Output is correct |
9 |
Correct |
4 ms |
22328 KB |
Output is correct |
10 |
Correct |
4 ms |
22364 KB |
Output is correct |
11 |
Correct |
5 ms |
22540 KB |
Output is correct |
12 |
Correct |
5 ms |
22364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22420 KB |
Output is correct |
2 |
Correct |
4 ms |
22360 KB |
Output is correct |
3 |
Correct |
5 ms |
22360 KB |
Output is correct |
4 |
Correct |
5 ms |
22364 KB |
Output is correct |
5 |
Correct |
5 ms |
22452 KB |
Output is correct |
6 |
Correct |
5 ms |
22424 KB |
Output is correct |
7 |
Correct |
4 ms |
22364 KB |
Output is correct |
8 |
Correct |
5 ms |
22364 KB |
Output is correct |
9 |
Correct |
4 ms |
22328 KB |
Output is correct |
10 |
Correct |
4 ms |
22364 KB |
Output is correct |
11 |
Correct |
5 ms |
22540 KB |
Output is correct |
12 |
Correct |
5 ms |
22364 KB |
Output is correct |
13 |
Correct |
6 ms |
22620 KB |
Output is correct |
14 |
Correct |
5 ms |
22616 KB |
Output is correct |
15 |
Correct |
5 ms |
22456 KB |
Output is correct |
16 |
Correct |
5 ms |
22620 KB |
Output is correct |
17 |
Correct |
5 ms |
22364 KB |
Output is correct |
18 |
Correct |
6 ms |
22620 KB |
Output is correct |
19 |
Correct |
5 ms |
22616 KB |
Output is correct |
20 |
Correct |
5 ms |
22620 KB |
Output is correct |
21 |
Correct |
6 ms |
22424 KB |
Output is correct |
22 |
Correct |
5 ms |
22424 KB |
Output is correct |
23 |
Correct |
5 ms |
22620 KB |
Output is correct |
24 |
Correct |
5 ms |
22632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22364 KB |
Output is correct |
2 |
Correct |
4 ms |
22360 KB |
Output is correct |
3 |
Correct |
4 ms |
22616 KB |
Output is correct |
4 |
Correct |
4 ms |
22436 KB |
Output is correct |
5 |
Correct |
156 ms |
44052 KB |
Output is correct |
6 |
Correct |
149 ms |
43968 KB |
Output is correct |
7 |
Correct |
136 ms |
44032 KB |
Output is correct |
8 |
Correct |
142 ms |
44044 KB |
Output is correct |
9 |
Correct |
5 ms |
22420 KB |
Output is correct |
10 |
Correct |
4 ms |
22360 KB |
Output is correct |
11 |
Correct |
5 ms |
22360 KB |
Output is correct |
12 |
Correct |
5 ms |
22364 KB |
Output is correct |
13 |
Correct |
5 ms |
22452 KB |
Output is correct |
14 |
Correct |
5 ms |
22424 KB |
Output is correct |
15 |
Correct |
4 ms |
22364 KB |
Output is correct |
16 |
Correct |
5 ms |
22364 KB |
Output is correct |
17 |
Correct |
4 ms |
22328 KB |
Output is correct |
18 |
Correct |
4 ms |
22364 KB |
Output is correct |
19 |
Correct |
5 ms |
22540 KB |
Output is correct |
20 |
Correct |
5 ms |
22364 KB |
Output is correct |
21 |
Correct |
6 ms |
22620 KB |
Output is correct |
22 |
Correct |
5 ms |
22616 KB |
Output is correct |
23 |
Correct |
5 ms |
22456 KB |
Output is correct |
24 |
Correct |
5 ms |
22620 KB |
Output is correct |
25 |
Correct |
5 ms |
22364 KB |
Output is correct |
26 |
Correct |
6 ms |
22620 KB |
Output is correct |
27 |
Correct |
5 ms |
22616 KB |
Output is correct |
28 |
Correct |
5 ms |
22620 KB |
Output is correct |
29 |
Correct |
6 ms |
22424 KB |
Output is correct |
30 |
Correct |
5 ms |
22424 KB |
Output is correct |
31 |
Correct |
5 ms |
22620 KB |
Output is correct |
32 |
Correct |
5 ms |
22632 KB |
Output is correct |
33 |
Correct |
95 ms |
29160 KB |
Output is correct |
34 |
Correct |
84 ms |
29016 KB |
Output is correct |
35 |
Correct |
73 ms |
29236 KB |
Output is correct |
36 |
Correct |
74 ms |
29236 KB |
Output is correct |
37 |
Correct |
21 ms |
24256 KB |
Output is correct |
38 |
Correct |
72 ms |
29260 KB |
Output is correct |
39 |
Correct |
12 ms |
23224 KB |
Output is correct |
40 |
Correct |
83 ms |
29216 KB |
Output is correct |
41 |
Correct |
56 ms |
30668 KB |
Output is correct |
42 |
Correct |
45 ms |
30552 KB |
Output is correct |
43 |
Correct |
145 ms |
44004 KB |
Output is correct |
44 |
Correct |
114 ms |
37216 KB |
Output is correct |