This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e6+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
struct st{
ll m[2][2];
};
int t[N],n,x;
st dp[N],dp2[N];
vi graf[N],c,__curr;
bitset<N>vis;
void dfs(int v,int o){
if(vis[v]){
if(c.size()) return;
while(__curr[__curr.size()-1]!=v) c.push_back(__curr[__curr.size()-1]),__curr.pop_back();
x=v;
return;
}
vis[v]=1;
__curr.push_back(v);
for(auto u:graf[v]){
if(u==o) continue;
dfs(u,v);
}
__curr.pop_back();
}
void dfst(int v,int o){
ll naj1=INFi,naj2=INFi,il=0;
for(auto u:graf[v]){
if(u==o or vis[u]) continue;
il=1;
dfst(u,v);
dp[v].m[1][1]+=dp[u].m[0][1];
dp[v].m[0][1]+=dp[u].m[0][0];
dp[v].m[0][0]+=dp[u].m[0][0];
dp[v].m[1][0]+=dp[u].m[0][1];
naj1=min(naj1,dp[u].m[1][0]-dp[u].m[0][0]),naj2=min(naj2,dp[u].m[1][1]-dp[u].m[0][1]);
}
if(!il){
dp[v].m[0][0]=INFi,dp[v].m[0][1]=0,dp[v].m[1][0]=INFi,dp[v].m[1][1]=1;
return;
}
dp[v].m[0][0]+=naj1,dp[v].m[1][0]+=naj2;
dp[v].m[1][0]++,dp[v].m[1][1]++;
}
ll cnt(vector<st>curr){
dp2[0]=curr[0];
for(int i=1;i<curr.size();i++){
dp2[i].m[0][0]=min(dp2[i-1].m[0][0]+curr[i].m[0][0],dp2[i-1].m[1][0]+curr[i].m[0][1]);
dp2[i].m[0][1]=curr[i].m[0][1]+dp2[i-1].m[0][0];
dp2[i].m[1][0]=min(dp2[i-1].m[1][1]+curr[i].m[1][1],dp2[i-1].m[0][1]+curr[i].m[1][0]);
dp2[i].m[1][1]=curr[i].m[1][1]+dp2[i-1].m[0][1];
}
return min(dp2[curr.size()-1].m[0][0],dp2[curr.size()-1].m[1][0]);
}
void solve(){
cin>>n;
for(int a,b,i=1;i<=n;i++){
cin>>a>>b;
graf[a].push_back(b),graf[b].push_back(a);
}
dfs(1,1);
vis.reset();
for(int i=0;i<c.size();i++){
int pop=(i==0?x:c[i-1]),nas=(i==c.size()-1?x:c[i+1]);
vis[pop]=1,vis[nas]=1;
dfst(c[i],c[i]);
vis[pop]=0,vis[nas]=0;
}
vis[c[0]]=1,vis[c[c.size()-1]]=1;
dfst(x,x);
vis[c[0]]=0,vis[c[c.size()-1]]=0;
vector<st>curr;
for(auto u:c) curr.push_back(dp[u]);
st *f=&curr[0];
st *l=&curr[curr.size()-1];
ll res=INFi;
//x=0 0
f->m[1][0]=INFi,f->m[1][1]=INFi,l->m[1][0]=INFi,l->m[1][1]=INFi;
res=min(res,cnt(curr)+dp[x].m[0][0]);
for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
//x=1 0
f->m[0][0]=f->m[0][1],f->m[1][0]=INFi,f->m[0][1]=INFi,f->m[1][1]=INFi;
l->m[0][0]=l->m[0][1],l->m[1][0]=INFi,l->m[0][1]=INFi,l->m[1][1]=INFi;
res=min(res,cnt(curr)+dp[x].m[1][0]);
for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
//x=0 1
//p1 f=1
f->m[0][1]=INFi,f->m[0][0]=INFi;
l->m[1][0]=INFi,l->m[1][1]=INFi;
res=min(res,cnt(curr)+dp[x].m[0][1]);
for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
//p2 l=1
f->m[1][0]=INFi,f->m[1][1]=INFi;
l->m[0][1]=INFi,l->m[0][0]=INFi;
res=min(res,cnt(curr)+dp[x].m[0][1]);
for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
//x=1 1
//p1 f=1
f->m[0][1]=INFi,f->m[0][0]=INFi,f->m[1][0]=f->m[1][1],f->m[1][1]=INFi;
l->m[1][0]=INFi,l->m[1][1]=INFi,l->m[0][0]=l->m[0][1],l->m[0][1]=INFi;
res=min(res,cnt(curr)+dp[x].m[1][1]);
for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
//p2 l=1
l->m[0][1]=INFi,l->m[0][0]=INFi,l->m[1][0]=l->m[1][1],l->m[1][1]=INFi;
f->m[1][0]=INFi,f->m[1][1]=INFi,f->m[0][0]=f->m[0][1],f->m[0][1]=INFi;
res=min(res,cnt(curr)+dp[x].m[1][1]);
cout<<(res==INFi?-1:res)<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
while(tt--) solve();
}
Compilation message (stderr)
Main.cpp: In function 'll cnt(std::vector<st>)':
Main.cpp:63:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i=1;i<curr.size();i++){
| ~^~~~~~~~~~~~
Main.cpp: In function 'void solve()':
Main.cpp:79:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int i=0;i<c.size();i++){
| ~^~~~~~~~~
Main.cpp:80:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | int pop=(i==0?x:c[i-1]),nas=(i==c.size()-1?x:c[i+1]);
| ~^~~~~~~~~~~~
Main.cpp:96:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
| ~^~~~~~~~~~~~
Main.cpp:101:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
| ~^~~~~~~~~~~~
Main.cpp:107:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
| ~^~~~~~~~~~~~
Main.cpp:112:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
| ~^~~~~~~~~~~~
Main.cpp:118:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |