Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

第一次写强联通,的确WA了不少次

Posted by suhang at 2009-12-17 19:11:07 on Problem 2186
#include <cstdio>
#include <cstring>

const int maxm=1000111,maxn=100010;

int n,m;
int lis[maxm],next[maxm],e[maxm];
int sign=0,tot=0,number;
int bian[maxm][3],stack[maxn],dfn[maxn],lowlink[maxn],block[maxn]/*强连通块*/;
bool instack[maxn];
bool du[maxn];
bool ling=false;
int weizhi;

int min(int x,int y)
{
	if (x<y) return x;
	else return y;
}

void dfs(int v)
{
	sign++;
	dfn[v]=sign;
	lowlink[v]=sign;
	tot++;
	stack[tot]=v;
	instack[v]=true;
	for (int u=lis[v];u!=0;u=next[u])
	{
		if (!dfn[e[u]])
		{
			dfs(e[u]);
			lowlink[v]=min(lowlink[v],lowlink[e[u]]);
		}
		else if (instack[e[u]])
		{
			lowlink[v]=min(lowlink[v],dfn[e[u]]);
		}
	}
	if ( lowlink[v]==dfn[v] )
	{
		number++;
		while ( stack[tot+1]!=v)
		{
			block[stack[tot]]=number;
			instack[stack[tot]]=false;
			tot--;
		}
	}
}

int main()
{
	freopen("p2186.in","r",stdin);
	freopen("p2186.out","w",stdout);
	
	scanf("%d%d",&n,&m);
	int l=0;
	for (int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		l++;
		e[l]=y;
		next[l]=lis[x];
		lis[x]=l;
		bian[l][1]=x;
		bian[l][2]=y;
	}
	for (int i=1;i<=n;i++)
	if (!dfn[i]) dfs(i);//这个地方十分重要 
	memset(du,false,sizeof(du));
	for (int i=1;i<=l;i++)
	{
		if (block[bian[i][1]]!=block[bian[i][2]])
		{
			du[block[bian[i][1]]]=true;
		}
	}
	
	for (int i=1;i<=number;i++)
	if (du[i]==false)
	{
		if (ling)
		{
			printf("0\n");
			return 0;
		}
		else
		{
			ling=true;
			weizhi=i;
		}
	}
	
	int ans=0;
	for (int i=1;i<=n;i++)
	if (block[i]==weizhi) ans++;
	printf("%d\n",ans);
	
	return 0;
}


我也贴代码纪念

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator