Poj Solution 2568

http://poj.org/problem?id=2568

#include<iostream>
#include"stdio.h"
#include"stdlib.h"
#include"memory.h"
using namespace std;
bool e[51][51];
bool s[51]; 
int a[51],n;
void print( int f )
{
    int i;
    printf( "(%d", f );
    
    for( i=1; i<=n; i++ )
    if( e[f][i] )
    {
        printf( " " );
        print( i );
    }

    printf( ")" );
}
int main()
{
    int i, j;
    char c;
    while( 1 )
    {
        n = 1;
        while( 1 )
        {
            if( cin.peek() == 'n' )
            {
                c = cin.get();
                break;
            }
            cin>>a[n++];
            if( cin.fail() ) return 0;
        }        
        memset( e, 0, sizeof( e ) );
        memset( s, 0, sizeof( s ) ); 
        s[ n ] = true;
        for( i=n-1; i; i-- )
        {
            if( i>1 && !s[ a[i-1] ] )
            {
                e[ a[i] ][ a[i-1] ] = true;
                s[ a[i-1] ] = true;
            }
            else
            {
                for( j=n-1; j; j-- )
                if( !s[ j ] )
                {
                    e[ a[i] ][ j ] = true;
                    s[ j ] = true;
                    break;
                }
            }
        }
        print(n);
        printf( "n" );
    }
    return 0;
}
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *