#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 = 2e5,mod = 1e12+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);
}
}
ll 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] = 1e12;
ll sum = me;
for(auto u:adj[nd])
{
if(u==par[nd])continue;
sum+=calc(u,0,me,rt,sp);
}
bool cmp = 0;
if(up)cmp = 1;
if(nd==root&&sp)cmp = 1;
if(nd==special&&rt)cmp = 1;
ll res = 1e12;
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;
}
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 = 1e12;
// cout<<root<<' '<<special<<endl;
memset(dp,-1,sizeof dp);
for(int rt = 0;rt<=1;rt++)
{
for(int sp = 0;sp<=1;sp++)
{
// cout<<calc(root,rt,0,rt,sp)<<' '<<endl;
// cout<<rt<<' '<<sp<<endl;
ans = min(ans,calc(root,rt,0,rt,sp));
}
}
if(ans==1e12)
cout<<-1<<endl;
else
cout<<ans<<endl;
return 0;
}
/*
*/
Compilation message
Main.cpp:13:29: warning: overflow in conversion from 'double' to 'int' changes value from '1.000000000007e+12' to '2147483647' [-Woverflow]
13 | const int N = 2e5,mod = 1e12+7;
| ~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
30036 KB |
Output is correct |
2 |
Correct |
16 ms |
30036 KB |
Output is correct |
3 |
Correct |
13 ms |
30036 KB |
Output is correct |
4 |
Correct |
13 ms |
30036 KB |
Output is correct |
5 |
Correct |
206 ms |
48176 KB |
Output is correct |
6 |
Correct |
189 ms |
48108 KB |
Output is correct |
7 |
Correct |
193 ms |
48020 KB |
Output is correct |
8 |
Correct |
192 ms |
48076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
30044 KB |
Output is correct |
2 |
Correct |
14 ms |
30036 KB |
Output is correct |
3 |
Correct |
13 ms |
29984 KB |
Output is correct |
4 |
Correct |
13 ms |
30036 KB |
Output is correct |
5 |
Correct |
13 ms |
29992 KB |
Output is correct |
6 |
Correct |
13 ms |
30036 KB |
Output is correct |
7 |
Correct |
14 ms |
30080 KB |
Output is correct |
8 |
Correct |
15 ms |
30004 KB |
Output is correct |
9 |
Correct |
13 ms |
30056 KB |
Output is correct |
10 |
Correct |
13 ms |
29972 KB |
Output is correct |
11 |
Correct |
14 ms |
30036 KB |
Output is correct |
12 |
Correct |
13 ms |
30076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
30044 KB |
Output is correct |
2 |
Correct |
14 ms |
30036 KB |
Output is correct |
3 |
Correct |
13 ms |
29984 KB |
Output is correct |
4 |
Correct |
13 ms |
30036 KB |
Output is correct |
5 |
Correct |
13 ms |
29992 KB |
Output is correct |
6 |
Correct |
13 ms |
30036 KB |
Output is correct |
7 |
Correct |
14 ms |
30080 KB |
Output is correct |
8 |
Correct |
15 ms |
30004 KB |
Output is correct |
9 |
Correct |
13 ms |
30056 KB |
Output is correct |
10 |
Correct |
13 ms |
29972 KB |
Output is correct |
11 |
Correct |
14 ms |
30036 KB |
Output is correct |
12 |
Correct |
13 ms |
30076 KB |
Output is correct |
13 |
Correct |
13 ms |
30036 KB |
Output is correct |
14 |
Correct |
14 ms |
30092 KB |
Output is correct |
15 |
Correct |
13 ms |
30036 KB |
Output is correct |
16 |
Correct |
14 ms |
30024 KB |
Output is correct |
17 |
Correct |
14 ms |
29996 KB |
Output is correct |
18 |
Correct |
14 ms |
30036 KB |
Output is correct |
19 |
Correct |
13 ms |
30036 KB |
Output is correct |
20 |
Correct |
14 ms |
30036 KB |
Output is correct |
21 |
Correct |
13 ms |
30080 KB |
Output is correct |
22 |
Correct |
13 ms |
30136 KB |
Output is correct |
23 |
Correct |
14 ms |
30164 KB |
Output is correct |
24 |
Correct |
13 ms |
30136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
30036 KB |
Output is correct |
2 |
Correct |
16 ms |
30036 KB |
Output is correct |
3 |
Correct |
13 ms |
30036 KB |
Output is correct |
4 |
Correct |
13 ms |
30036 KB |
Output is correct |
5 |
Correct |
206 ms |
48176 KB |
Output is correct |
6 |
Correct |
189 ms |
48108 KB |
Output is correct |
7 |
Correct |
193 ms |
48020 KB |
Output is correct |
8 |
Correct |
192 ms |
48076 KB |
Output is correct |
9 |
Correct |
13 ms |
30044 KB |
Output is correct |
10 |
Correct |
14 ms |
30036 KB |
Output is correct |
11 |
Correct |
13 ms |
29984 KB |
Output is correct |
12 |
Correct |
13 ms |
30036 KB |
Output is correct |
13 |
Correct |
13 ms |
29992 KB |
Output is correct |
14 |
Correct |
13 ms |
30036 KB |
Output is correct |
15 |
Correct |
14 ms |
30080 KB |
Output is correct |
16 |
Correct |
15 ms |
30004 KB |
Output is correct |
17 |
Correct |
13 ms |
30056 KB |
Output is correct |
18 |
Correct |
13 ms |
29972 KB |
Output is correct |
19 |
Correct |
14 ms |
30036 KB |
Output is correct |
20 |
Correct |
13 ms |
30076 KB |
Output is correct |
21 |
Correct |
13 ms |
30036 KB |
Output is correct |
22 |
Correct |
14 ms |
30092 KB |
Output is correct |
23 |
Correct |
13 ms |
30036 KB |
Output is correct |
24 |
Correct |
14 ms |
30024 KB |
Output is correct |
25 |
Correct |
14 ms |
29996 KB |
Output is correct |
26 |
Correct |
14 ms |
30036 KB |
Output is correct |
27 |
Correct |
13 ms |
30036 KB |
Output is correct |
28 |
Correct |
14 ms |
30036 KB |
Output is correct |
29 |
Correct |
13 ms |
30080 KB |
Output is correct |
30 |
Correct |
13 ms |
30136 KB |
Output is correct |
31 |
Correct |
14 ms |
30164 KB |
Output is correct |
32 |
Correct |
13 ms |
30136 KB |
Output is correct |
33 |
Correct |
119 ms |
33588 KB |
Output is correct |
34 |
Correct |
124 ms |
33596 KB |
Output is correct |
35 |
Correct |
117 ms |
33612 KB |
Output is correct |
36 |
Correct |
116 ms |
33512 KB |
Output is correct |
37 |
Correct |
34 ms |
30972 KB |
Output is correct |
38 |
Correct |
139 ms |
33688 KB |
Output is correct |
39 |
Correct |
19 ms |
30292 KB |
Output is correct |
40 |
Correct |
134 ms |
33580 KB |
Output is correct |
41 |
Correct |
76 ms |
34588 KB |
Output is correct |
42 |
Correct |
88 ms |
34404 KB |
Output is correct |
43 |
Correct |
168 ms |
47948 KB |
Output is correct |
44 |
Correct |
137 ms |
41160 KB |
Output is correct |