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

AC了,没AC的这里有代码。。。

Posted by 50573750 at 2011-07-05 23:07:58 on Problem 1094
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <sstream>
#include <queue>
#include <list>
#include <stack>

using namespace std;

vector<vector<int>> mat_g;
int n,m;
vector<int> v_g;

int topo_order(int n)
{
	vector<vector<int>> mat_g_b;
	mat_g_b = mat_g;

	bool b_re = true;
	v_g.clear();
	for(int cnt=0;cnt<n;cnt++)
	{
		vector<int> v_l;
		for(int cnt=0;cnt<n;cnt++)
		{
			int pos = 0;
			bool b_first = true;
			for(int cnt_in=0;cnt_in<n;cnt_in++)
			{
				if (mat_g[cnt_in][cnt] == 1)
				{
					b_first = false;
					break;
				}
			}

			if (b_first)
			{
				v_l.push_back(cnt);
			}
		}

		if (v_l.size() == v_g.size() + 1)
		{
			for(int cnt_l=0;cnt_l<v_l.size();cnt_l++)
			{
				bool b_out = true;
				for(int cnt_g=0;cnt_g<v_g.size();cnt_g++)
				{
					if (v_g[cnt_g] == v_l[cnt_l])
					{
						b_out = false;
						break;
					}
				}

				if (b_out)
				{
					v_g.push_back(v_l[cnt_l]);
					break;
				}
			}
		}
		else
		{
			b_re = false;
			for(int cnt_l=0;cnt_l<v_l.size();cnt_l++)
			{
				bool b_out = true;
				for(int cnt_g=0;cnt_g<v_g.size();cnt_g++)
				{
					if (v_g[cnt_g] == v_l[cnt_l])
					{
						b_out = false;
						break;
					}
				}

				if (b_out)
				{
					v_g.push_back(v_l[cnt_l]);
					break;
				}
			}
		}

		for(int cnt=0;cnt<v_l.size();cnt++)
		{
			for(int cnt_in=0;cnt_in<n;cnt_in++)
			{
				mat_g[v_l[cnt]][cnt_in] = 0;
				mat_g[cnt_in][v_l[cnt]] = 0;
			}
		}
	}

	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if (mat_g[i][j] == 1)
			{
				mat_g = mat_g_b;
				return 2;
			}
		}
	}

	if (v_g.size() == n && b_re == true)
	{
		mat_g = mat_g_b;
		return 4;
	}
	else
	{
		mat_g = mat_g_b;

		if (b_re == false)
			return 3;
		else
			return 1;
	}
}

int main()
{
	int num_case;
    while(cin>>n>>m)
	{
		vector<int> v_g_b;

		mat_g.clear();
		mat_g.resize(30);
		for(int cnt=0;cnt<30;cnt++)
		{
			mat_g[cnt].resize(30);
		}

		if (n==0 && m==0)
			break;

		int b_output = 700;
		for(int cnt=1;cnt<=m;cnt++)
		{
			string str;
			cin>>str;

			mat_g[str[0]-'A'][str[2] - 'A'] = 1;

			int re = topo_order(n);
			if (re == 2  && b_output == 700)
			{
				b_output =600;
				cout<<"Inconsistency found after "<<cnt<<" relations."<<endl;
			}
			else if (re == 4 && b_output > 600)
			{
				b_output = cnt;
				v_g_b = v_g;
			}
		}

		if (b_output == 700)
		{
			cout<<"Sorted sequence cannot be determined."<<endl;
		}
		else if (b_output < 600)
		{
				cout<<"Sorted sequence determined after "<<b_output<<" relations: ";
				for(int cnt=0;cnt<v_g_b.size();cnt++)
				{
					cout<<(char)(v_g_b[cnt]+'A');
				}
				cout<<"."<<endl;
		}
	}

	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