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

Why wa?痛苦ing...

Posted by xiaomi at 2003-07-20 16:18:26 on Problem 1094
In Reply To:读不懂了 Posted by:xiaomi at 2003-07-20 15:59:23
#include <iostream.h>
int i,j,m,n,p;
char str[3];
bool found,inc;
bool **c;
char *route;
void judge()
{
	int i,j,k,len=0;
	int flag;
	bool seq,**d,*del;
	d=new bool* [n];
	del=new bool [n];
	for (i=0;i<n;i++)
	{
		d[i]=new bool [n];
		del[i]=0;
		for (j=0;j<n;j++)
			d[i][j]=c[i][j];
	}
	seq=true;
	while (len<n)
	{
		flag=0;
		for (i=0;i<n;i++)
		{
			if (del[i])
				continue;
			for (j=0;j<n;j++)
				if (d[j][i]==1)
					break;
			if (j==n)
			{
				flag++;
				k=i;
			}
			if (flag>1)
				break;
		}
		if (flag==0)
		{
			inc=true;
			break;
		}
		else
		{
			del[k]=1;
			if (flag>1)
				seq=false;
			route[len]=k+65;
			len++;
			for (j=0;j<n;j++)
				d[k][j]=0;
		}
	}
	if (seq)
		found=true;
	delete[] del;
	delete[] d;
}
int main()
{
	cin>>n>>m;
	while (n!=0)
	{
		c=new bool* [n];
		route=new char [n];
		for (i=0;i<n;i++)
		{
			c[i]=new bool [n];	
			for (j=0;j<n;j++)
				c[i][j]=0;
		}
		found=inc=false;
		for (i=0;i<m;i++)
		{
			cin>>str;
			c[str[0]-65][str[2]-65]=1;
			if (!inc&&!found)
			{
				judge();
				if (inc)
					cout<<"Inconsistency found after "<<i+1<<" relations."<<endl;
				else if (found)
				{
					cout<<"Sorted sequence determined after "<<i+1<<" relations: ";
					for (j=0;j<n;j++)
						cout<<route[j];
					cout<<"."<<endl;
				}
			}
		}
		if (!inc&&!found)
			cout<<"Sorted sequence cannot be determined."<<endl;
		delete[] c;
		cin>>n>>m;
	}
	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