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

topsort做的,wa到不行了。求助高手

Posted by 0810311106 at 2010-08-12 18:38:53 on Problem 2491
#include"iostream"
#include"cstdio"
#include"string"
#include"map"
#include"queue"
#define M 4000
using namespace std;
int order[M];
typedef struct se
{
	int num;
	struct se *next;
}node;
typedef struct no
{
	int degree;
	node *first;
}TREE;
TREE N[M];
int visited[M];
void create_tree(int a,int b)
{
        node *p;
		p=(node*)malloc(sizeof(node));
		p->num=b;
		p->next=N[a].first;
		N[a].first=p;
        N[b].degree++;
}
int maax;
int flag;
void topsort()
{
       queue<int>x;
       int i,j;
	   for(i=1;i<=maax;i++)
	   {
		   if(N[i].degree==0&&visited[i]==0)
		   {
			   x.push(i);
			   visited[i]=1;
		   }
	   }
	   while(x.size()!=0)
	   {
		   int temp=x.front();
		   node *p;
		       x.pop();
			   order[flag++]=temp;
               p=N[temp].first;
			   while(p)
			   {
				   int j=p->num;
				   if(--N[j].degree==0&&visited[j]==0)
				   {
					   x.push(j);
					   visited[j]=1;
				   }
				   p=p->next;
			   }
	   }
}
int main()
{
	int n;
	int ni=0;
	int i;
	scanf("%d",&n);
	while(n--)
	{
		map<string,int>str;
        map<int,string>str1;
		memset(order,0,sizeof(order));
		memset(visited,0,sizeof(visited));
		maax=0;
		flag=0;
		ni++;
		scanf("%d",&maax);
		for(i=0;i<=M;i++)
			N[i].degree=0;
		int s=1;
		for(i=0;i<maax-1;i++)
		{
              string a,b;
			  cin>>a>>b;
              if(!str[a])
			  {
				  str[a]=s;
				  str1[s]=a;
				  s++;
			  }
			  if(!str[b])
			  {
				  str[b]=s;
				  str1[s]=b;
				  s++;
			  }
			  create_tree(str[a],str[b]);
		}
		topsort();
		cout<<"Scenario #"<<ni<<":"<<endl;
		for(i=0;i<flag;i++)
		{
			cout<<str1[order[i]]<<endl;
		}
		cout<<endl;
		str.clear();
		str1.clear();
	}
	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