P1347 排序

P1347 排序
floyed判环,水过。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<cstring>
  8 #define inf 2147483647
  9 #define For(i,a,b) for(register int i=a;i<=b;i++)
 10 #define p(a) putchar(a)
 11 #define g() getchar()
 12 //by war
 13 //2017.10.25
 14 using namespace std;
 15 int n,m,x;
 16 char a[4];
 17 bool f[1000][1000];
 18 char ans[1000];
 19 void in(int &x)
 20 {
 21     int y=1;
 22     char c=g();x=0;
 23     while(c<'0'||c>'9')
 24     {
 25     if(c=='-')
 26     y=-1;
 27     c=g();
 28     }
 29     while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
 30     x*=y;
 31 }
 32 void o(int x)
 33 {
 34     if(x<0)
 35     {
 36         p('-');
 37         x=-x;
 38     }
 39     if(x>9)o(x/10);
 40     p(x%10+'0');
 41 }
 42 
 43 int floyed()
 44 {
 45     int sum=0;
 46     For(k,'A','A'+n-1)
 47       For(i,'A','A'+n-1)
 48         For(j,'A','A'+n-1)
 49           if(f[i][k]&&f[k][j])
 50             f[i][j]=true;
 51     For(i,'A',n+'A'-1)
 52     if(f[i][i])
 53     return 0;
 54     bool flag=false;
 55     For(k,1,n)
 56     {
 57         sum=0;
 58         flag=false;
 59         For(i,'A','A'+n-1)
 60          {
 61            sum=0;
 62            For(j,'A','A'+n-1)
 63             if(f[i][j])
 64             sum++;
 65             
 66               if(sum==n-k)
 67               {
 68               ans[k]=(char)i;
 69               flag=true;
 70               break;    
 71               }
 72         }
 73         if(!flag)
 74         return 2;
 75     }
 76     return 1;
 77 }
 78 
 79 int main()
 80 {
 81     in(n),in(m);
 82     For(i,1,m)
 83     {
 84         cin>>a;
 85         f[a[0]][a[2]]=true;
 86         x=floyed();
 87         if(x==0)
 88         {
 89         printf("Inconsistency found after %d relations.",i);
 90         exit(0);    
 91         }
 92         else
 93         if(x==1)
 94         {
 95         printf("Sorted sequence determined after %d relations: ",i);
 96         For(j,1,n)
 97         printf("%c",ans[j]);
 98         printf(".");
 99         exit(0);    
100         }
101     }
102     printf("Sorted sequence cannot be determined.");
103      return 0;
104 }

 

posted @ 2017-10-25 09:09  WeiAR  阅读(257)  评论(0编辑  收藏  举报