POJ 1094 floyd + topological Sequence

Source: Internet
Author: User

This question is really tangled ~ I remember that I ran straight to this question after I completed the expensive hiring ceremony that day, but the description of my question was hard to let me re-write the code I wrote that day. In fact, I did it more than once, it should be twice ~~ Indeed, there are still some problems in my code writing. The speed is not fast enough and there are too many failures. I can only press the return key every time. It seems that the keyboard is quickly tapped, but it is just venting. The POJ server cannot be mounted. I searched for a video program and felt that I had nothing wrong. The answer was the same.

------------------------------------------------- Split line ----------------------------------------------------

Guo shenniu is no longer here, and she is very lost... I am still far from xiaoyuan. She has three questions every day and I have three days. This gap ~~~

------------------------------------------------- Dual split line ------------------------------------------------

This question is written by floyd and can't be viewed by using this algorithm directly. This forms a complete topological sequence. How can this problem be solved? In fact, this fully topological sequence will find a law when you think about it. There is a small arithmetic difference sequence in it, which can simplify the computation. For example, there are 26 letters which have been arranged, there must be a letter smaller than the other 25 letters, and a letter larger than the other 25 letters. This is 1 + 2 + 3 + 4 + .... + 25, so as long as the sum of n letters is equal to the sum of the First n terms of such a sequence, it means that they are fully arranged. In addition, when determining the contradiction, when there is a letter with an input level of 0, it will be shown, and then the letter level corresponding to the output level will be cut out, and then the cycle will be repeated, in this way, the zero degree is not equal to n letters. If none of the above conditions are met, it cannot be determined ~

The code hasn't been handed in yet ......

#include<iostream>#define MAXN 27using namespace std;int n,m;bool map[MAXN][MAXN];int table[MAXN];int ans1,ans2;int inp[MAXN];void floyd(){     int i,j,k;     for( i=1;i<=n;i++ )          for( j=1;j<=n;j++ )               for( k=1;k<=n;k++ )                    if( map[i][k]&&map[k][j] )                        map[i][j]=true;}char anslist[MAXN];void makeans(){     memset( anslist,0,sizeof(anslist) );     int cnt=0,i,j,k;     bool visited[MAXN];     memset( visited,0,sizeof(visited) );     for( i=1;i<=n;i++ )          for( j=1;j<=n;j++ )          {               if( inp[j]==0 && !visited[j] )               {                   visited[j]=true;                   for( k=1;k<=n;k++ )                        if( map[j][k] ) inp[k]--;                   cnt++;                   anslist[cnt]=char(j-1+'A');               }          }     return ;}void judge( int pre ){     floyd();     int i,j,k;     int cnt=0;          memset( inp,0,sizeof(inp) );     for( i=1;i<=n;i++ )          for( j=1;j<=n;j++ )               if( map[i][j] ){                   inp[j]++;cnt++;               }                    if( table[n]==cnt )     {              makeans();          ans1=pre;return ;     }     cnt=0;     bool visited[MAXN];     memset( visited,0,sizeof(visited) );     for( i=1;i<=n;i++ )          for( j=1;j<=n;j++ )          {               if( inp[j]==0&&!visited[j] )               {                   visited[j]=true;                   for( k=1;k<=n;k++ )                        if( map[j][k] )                            inp[k]--;                   cnt++;               }          }     if( cnt!=n )         ans2=pre;}int main(){    freopen( "in.txt","r",stdin );    freopen( "myfc.txt","w",stdout );    int i,j,k;    table[0]=0;    for( i=1;i<=26;i++ )         table[i]=table[i-1]+i-1;    while( scanf( "%d %d",&n,&m )!=EOF )    {           if( n==0 && m==0 ) break;           ans1=ans2=0;           memset( map,0,sizeof( map) );           int index=0;           char a,b,c;           for( i=1;i<=m;i++ )           {                scanf( "\n%c%c%c",&a,&c,&b );                map[a-'A'+1][b-'A'+1]=true;                if( ans1==0 && ans2==0 )                    judge(i);           }           if( ans1 )           {                    printf( "Sorted sequence determined after %d relations: ",ans1 );               for( k=1;k<=n;k++ )                    printf( "%c",anslist[k] );               printf( ".\n" );            }           else if( ans2 )                printf( "Inconsistency found after %d relations.\n",ans2 );           else               printf( "Sorted sequence cannot be determined.\n" );    }    return 0;   }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.