边双连通分量模板

#include 

#define MAXN 10005

using namespace std;

struct Tarjan
{
	struct edge
	{
		int u,v;
		edge(int uu=0,int vv=0):u(uu),v(vv){}
	};
	
	int n;	//点数
	
	vector e[MAXN];	//邻接表
	int DFN[MAXN],LOW[MAXN];
	int index;	
	
	int stk[MAXN];	 
	int top;
	
	vector > Ans;	//点集¸ 
	vector ecut;	//割边
	
	void init(int N)	//初始化
	{
		n=N;
		
		Ans.clear();
		for(int i=1;i<=n;i++)
			e[i].clear();
		top=0;
		index=0;
		
		ecut.clear();
		
		memset(DFN,-1,sizeof(DFN));
	}
	
	void AddEdge(int u,int v)	
	{
		e[u].push_back(v);
		e[v].push_back(u);
	}
	
	void DFS(int u,int prt)	
	{
		DFN[u]=LOW[u]=++index;
		
		stk[top++]=u;
		
		for(int i=0;i=DFN[u])continue;
			
			if(DFN[v]==-1)
			{
				DFS(v,u);
				LOW[u]=min(LOW[u],LOW[v]);
				
				if(LOW[v]>DFN[u])
				{
					vector ans;
					while(stk[top-1]!=v)
					{
						ans.push_back(stk[top-1]);
						top--;
					}
					
					ecut.push_back(edge(u,v));
                    ans.push_back(v);
					top--;
					
					Ans.push_back(ans);
				}
			
			}
			else LOW[u]=min(LOW[u],DFN[v]);
		}
		if(prt==-1&&top!=0)
		{
			vector ans;
			while(top)
			{ 
				ans.push_back(stk[top-1]);
				top--;
			}
			Ans.push_back(ans);
		}
	}
	
	void Solve()
	{
		for(int i=1;i<=n;i++)
			if(DFN[i]==-1)DFS(i,-1);
	}
}T;

int main()
{
	int n;
	while(scanf("%d",&n)==1)
	{
		T.init(n);
		int m;
		scanf("%d",&m);
		for(int i=0;i

你可能感兴趣的:(图论,模板)