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

附代码

Posted by liumeng at 2012-03-02 14:24:51 on Problem 1094
//============================================================================
// Name        : sorting.cpp
// Author      : liumeng
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include<algorithm>
#include<fstream>
#include<queue>
using namespace std;
#define M 26
int a[M][M];
int d[M];
int dc[M];
int rd[M];
int r[M];
queue<int> q;
bool only;
bool cycle;
void compute(int n){
	only=true;
	cycle=false;
	for (int i = 0; i < n; ++i) {
		dc[i]=rd[i];
		if(dc[i]==0){
			q.push(i);
		}
	}
	int count=0;
	while(1){
		if(q.empty()){
			break;
		}
		if(q.size()>1){
			only=false;
		}
		int t=q.front();
		q.pop();
		r[count++]=t;

		for (int j = 0; j < d[t]; ++j) {
			dc[a[t][j]]--;
			if(dc[a[t][j]]==0){
				q.push(a[t][j]);
			}
		}

	}
	if(count<n){
		cycle=true;
	}







}




int main() {
//	cout << "Hello World!!!" << endl; // prints Hello World!!!
	int n,m;
//    ifstream cin("a");

	while(1){
		char buf[10];
		cin>>n>>m;
		if(n==0&&m==0){
			return 0;
		}
		for (int i = 0; i < n; ++i) {
			d[i]=0;
			rd[i]=0;
		}
		char A='A';

			int i;
			for (i = 1; i <=m; ++i) {
				char c,e,gb;
				cin>>c>>e>>gb;
				int u=c-A;
				int v=gb-A;

				a[u][d[u]++]=v;
				rd[v]++;
				compute(n);
				if(cycle){
					cout<<"Inconsistency found after "<<i<<" relations."<<endl;
					break;
				}
				else if(only){
					cout<<"Sorted sequence determined after "<<i<<" relations: ";
					for (int var = 0; var < n; ++var) {
						char c=A+r[var];
						cout<<c;
					}
					cout<<"."<<endl;
					break;
				}

			}
			if(i==m+1){
				cout<<"Sorted sequence cannot be determined."<<endl;

			}
			for (int var = i; var <=m; ++var) {
				cin.getline(buf,10);
			}




	}







	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