RT,一个简单的树形DP,就是分成根与他的环上邻点的选取情况分了4种情况讨论做4次DP。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef pair<int,int> PII;
const int N=2e5+10;
const int INF=1e6+10;
int n,rt,le,br;
int he[N],to[N<<1],ne[N<<1],tot=1;
int fat[N],dfo[N];
__int128 ans=1e18,f[N][2][3],cnt;
void addedge(int x,int y){
to[++tot]=y;
ne[tot]=he[x];
he[x]=tot;
}
void get(int x,int f){
dfo[x]=++cnt;
for(int i=he[x];i;i=ne[i]){
int v=to[i];
if(v==f||i==br||i==(br^1)){
continue;
}
if(dfo[v]&&!br){
br=i;
}
get(v,x);
}
}
void dp1(int x,int fa){
f[x][1][1]=f[x][0][1]=INF;
fat[x]=fa;
for(int i=he[x];i;i=ne[i]){
int v=to[i];
if(v==fat[x]||i==br||i==(br^1)){
continue;
}
dp1(v,x);
f[x][1][0]+=f[v][0][0];
f[x][0][0]+=f[v][0][1];
}
f[x][1][0]++;
for(int i=he[x];i;i=ne[i]){
int v=to[i];
if(v==fat[x]||i==br||i==(br^1)){
continue;
}
f[x][1][1]=min(f[x][1][1],f[x][1][0]-f[v][0][0]+f[v][1][0]);
f[x][0][1]=min(f[x][0][1],f[x][0][0]-f[v][0][1]+f[v][1][1]);
}
if(x==le){
f[x][0][0]=f[x][0][1]=INF;
}
}
void dp2(int x){
f[x][1][1]=f[x][0][1]=INF;
for(int i=he[x];i;i=ne[i]){
int v=to[i];
if(v==fat[x]||i==br||i==(br^1)){
continue;
}
dp2(v);
f[x][0][0]+=f[v][0][1];
f[x][1][0]+=f[v][0][0];
}
f[x][1][0]++;
for(int i=he[x];i;i=ne[i]){
int v=to[i];
if(v==fat[x]||i==br||i==(br^1)){
continue;
}
f[x][0][1]=min(f[x][0][1],f[x][0][0]-f[v][0][1]+f[v][1][1]);
f[x][1][1]=min(f[x][1][1],f[x][1][0]-f[v][0][0]+f[v][1][0]);
}
if(x==le){
f[x][1][0]=f[x][1][1]=INF;
}
}
void dp3(int x){
bool yet=false;
f[x][1][1]=f[x][0][1]=INF;
for(int i=he[x];i;i=ne[i]){
int v=to[i];
if(v==fat[x]||i==br||i==(br^1)){
continue;
}
dp3(v);
if(v==le){
f[x][1][0]+=f[v][0][0];
f[x][0][0]+=f[v][1][2];
yet=true;
}
f[x][1][0]+=f[v][0][0];
f[x][0][0]+=f[v][0][1];
f[x][1][2]+=f[v][0][0];
}
f[x][1][0]++;
f[x][1][2]++;
if(yet==true){
f[x][0][1]=f[x][0][0];
f[x][0][0]=f[x][1][0]=f[x][1][0]=INF;
return;
}
for(int i=he[x];i;i=ne[i]){
int v=to[i];
if(v==fat[x]||i==br||i==(br^1)){
continue;
}
f[x][1][1]=min(f[x][1][1],f[x][1][0]-f[v][0][0]+f[v][1][0]);
f[x][0][1]=min(f[x][0][1],f[x][0][0]-f[v][0][1]+f[v][1][1]);
}
if(x==le){
f[x][1][1]=f[x][0][0]=f[x][0][1]=f[x][1][0]=INF;
}
}
void dp4(int x){
f[x][1][1]=f[x][0][1]=INF;
for(int i=he[x];i;i=ne[i]){
int v=to[i];
if(v==fat[x]||i==br||i==(br^1)){
continue;
}
dp4(v);
if(v==le){
f[x][0][0]+=f[v][0][2];
f[x][1][0]+=f[v][0][0];
}
else{
f[x][1][0]+=f[v][0][0];
f[x][0][0]+=f[v][0][1];
}
f[x][0][2]+=f[v][0][1];
}
f[x][1][0]++;
for(int i=he[x];i;i=ne[i]){
int v=to[i];
if(v==fat[x]||i==br||i==(br^1)){
continue;
}
if(v==le){
continue;
}
f[x][1][1]=min(f[x][1][1],f[x][1][0]-f[v][0][0]+f[v][1][0]);
f[x][0][1]=min(f[x][0][1],f[x][0][0]-f[v][0][1]+f[v][1][1]);
}
if(x==le){
f[x][1][0]=f[x][1][1]=f[x][0][1]=f[x][0][0]=INF;
}
}
int main(){
scanf("%d",&n);
int x,y;
for(int i=1;i<=n;i++){
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
get(1,0);
rt=to[br],le=to[br^1];
dp1(rt,0);
ans=f[rt][0][0];
memset(f,0,sizeof f);
dp2(rt);
ans=min(ans,f[rt][0][1]);
memset(f,0,sizeof f);
dp3(rt);
ans=min(ans,f[rt][1][0]);
memset(f,0,sizeof f);
dp4(rt);
ans=min(ans,f[rt][1][1]);
memset(f,0,sizeof f);
long long res=ans;
if(ans>=INF){
printf("-1\n");
}
else{
printf("%lld\n",res);
}
}