poj 1094 Sorting It All Out(图论)

2014-11-24 00:33:21 · 作者: · 浏览: 2

这一题,看了个大牛的解题报告,思路变得非常的清晰:

1,先利用floyd_warshall算法求出图的传递闭包

2,再判断是不是存在唯一的拓扑排序,利用出度和入度是不是相加为n-1

3,利用拓扑排序求出当前的图形的唯一的拓扑排序


一开始我的思路跟上述的差不多,但是没有利用floyd_warshall算法求出传递闭包,准备着利用拓扑排序求出是不是存在着有环回路,我觉得我的这个思路也是可以的。等一下我会将我的这个思路给写成程序。在我的百度云中有这个程序的测试数据(来自poj)

#include 
//#include 
using namespace std;
#define MAX 30
/*396K	16MS*/
//var
int a[MAX][MAX];
int n;
int flag1,flag2;  //falg1代表的是当前有环的错误,即存在错误的排序 
char s[MAX];     //存放最后的结果 
//fstream fin;
//function
bool transition();
bool judge();
void toposort();

//main函数 
int main()
{
    //fin.open("1094.txt",ios::in);
    int m;
    while(cin>>n>>m)
    {
         if(n==0&&m==0)  break;
         memset(a,0,sizeof(a));
         int count=1;
         flag1=flag2=false;
         for(int i=0;i>b1>>b2>>b2;
            if(flag1||flag2) continue; 
            if(a[b1-'A'][b2-'A']==0)
            {
                a[b1-'A'][b2-'A']=1;
                //求传递闭包,判断是不是有环,这样就知道是不是存在着错误的答案 
                if(!transition()){flag1=true;continue;}
                //判断是不是存在着唯一的拓扑排序 
                else if(judge())
                {
                     toposort();
                     flag2=true;
                     continue;
                } 
            }
            ++count;
         }
         
         if(flag1)
            cout<<"Inconsistency found after "<