一、仙人掌的最大独立集(就是拆分为多棵基环树来做的):
注意:使用Tarjan算法时,注意题目要求需不需要处理重边。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#define ll long long
#define pr make_pair
#define pb push_back
//#define lc (p<<1)
//#define rc (p<<1|1)
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const double dnf=1e15;
const int mod=1e9+7;
const int maxn=120100;
int head[maxn],ver[maxn],nt[maxn];
int dfn[maxn],low[maxn],fa[maxn];
int dp[maxn][2],tot=1,cnt=0,n,m;
void add(int x,int y)
{
ver[++tot]=y,nt[tot]=head[x],head[x]=tot;
}
void toDp(int x,int y)
{
int t0,t1,f0=0,f1=0;
for(int i=y;i!=x;i=fa[i])
{
t0=f0+dp[i][0];//当前这个不选
t1=f1+dp[i][1];//当前这个选
f0=max(t0,t1);//当前这个点随意能到达的最大值
f1=t0;//当前这个点不选的最大值
}
dp[x][0]+=f0;
f0=0,f1=-inf;
for(int i=y;i!=x;i=fa[i])
{
t0=f0+dp[i][0];
t1=f1+dp[i][1];
f0=max(t0,t1);
f1=t0;
}
dp[x][1]+=f1;
}
void dfs(int x,int ff)
{
fa[x]=ff;
dfn[x]=low[x]=++cnt;
dp[x][0]=0,dp[x][1]=1;
for(int i=head[x];i;i=nt[i])
{
int y=ver[i];
if(!dfn[y])
{
dfs(y,x);
low[x]=min(low[x],low[y]);
}
else if(y!=ff)low[x]=min(low[x],dfn[y]);//不用考虑重边
if(low[y]>dfn[x])
{
dp[x][0]+=max(dp[y][0],dp[y][1]);
dp[x][1]+=dp[y][0