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

用Garbow无限wa,求大牛不吝赐教

Posted by hataksumo at 2012-08-22 09:30:37 on Problem 1904
好像我注释里的数据都没过,但不知道错哪了
#include<cstdio>
#include<vector>
using namespace std;
#define MN 4100
#define ME 210000
struct  
{
	int pre;
	int to;
}edges[ME];
int box[MN];
int len;
void iniPreLinkList(int n)
{
	memset(box,-1,sizeof(box));
	len=0;
}
void addDirectedEdge(int frm,int to)
{
	edges[len].to=to;
	edges[len].pre=box[frm];
	box[frm]=len++;
}
void readData(int n)
{
	int i,num,to;
	iniPreLinkList(n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&num);
		while(num--)
		{
			scanf("%d",&to);
			addDirectedEdge(i,n+to);
		}
	}
	for(i=1;i<=n;i++)
	{
		scanf("%d",&to);
		addDirectedEdge(n+to,i);
	}
}
int stk1[MN],stk2[MN],stklen1,stklen2,low[MN],blng[MN];
void garbowdfs(int cur,int lay,int &scc_num)
{
	int ads,ct;
	stk1[++stklen1]=cur;stk2[++stklen2]=cur;
	low[cur]=++lay;
	for(ads=box[cur];~ads;ads=edges[ads].pre)
	{
		ct=edges[ads].to;
		if(!low[ct])
			garbowdfs(ct,lay,scc_num);
		else if(!blng[ct])
			while(low[stk2[stklen2]]>low[ct])stklen2--;
	}
	if(stk2[stklen2]==cur)
	{
		stklen2--;
		scc_num++;
		do 
		{
			blng[stk1[stklen1]]=scc_num;
		} while (stk1[stklen1--]!=cur);
	}
}
int garbow(int n)
{
	int scc_num=0,lay=0,i;
	memset(blng,0,sizeof(blng));
	memset(low,0,sizeof(low));
	stklen1=stklen2=0;
	for(i=1;i<=n<<1;i++)
	{
		if(low[i]==0) garbowdfs(i,lay,scc_num);
	}
	return scc_num;
}
vector<int> result[MN];
vector<int>::iterator it;
void solve(int n)
{
	int i;
	for(i=0;i<=n<<1;i++)
		result[i].clear();
	for(i=n+1;i<=n+n;i++)
	{
		printf("%d ",blng[i]);
	}
	printf("\n");
	for(i=n+1;i<=n+n;i++)
	{
		result[blng[i]].push_back(i-n);
	}
	for(i=1;i<=n;i++)
	{
		printf("%d",result[blng[i]].size());
		for(it = result[blng[i]].begin();it!=result[blng[i]].end();it++)
		{
			printf(" %d",*it);
		}
		printf("\n");
	}
}
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		readData(n);
		garbow(n);
		solve(n);
	}
	return 0;
}
/*
5
3 3 4 5
2 4 5
1 3
3 1 2 3
3 1 4 5
5 4 3 2 1
*/

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