Poj Solution 2567

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

#include"stdio.h"
#include"stdlib.h"
#include"memory.h"


int d[51],n;
bool e[51][51];

void creat( int f )
{
    int s;
    char t[10];

    scanf( "%[ ,0-9]", t );
    s = atoi( t );
    if( s > n ) n = s;

    if( f ) e[f][s] = e[s][f] = true;

    while( 1 )
    {
        scanf( "%1s", t );

        if( t[0] == ')' )
            return;
        else creat( s );
    }
}

int main()
{
    int i, j, k;
    char c;

    while( scanf( "%c" ,&c ) == 1 )
    {
        n = 0;
        memset( e, 0, sizeof(e) );
        creat(0);
    

        for( i=1; i<=n; i++ )
        {
            d[i] = 0;
            for( j=1; j<=n; j++ )
                d[i] += (int)e[i][j];
        }

        for( i=1; i<n; i++ )
        {
            for( j=1; j<=n; j++ )
                if( d[j] == 1 ) break;

            for( k=1; k<=n; k++ )
            if( e[j][k] )
            {
                printf( "%d", k );
                e[k][j] = false;
                d[k]--,d[j]--;
                break;
            }

            if( i<n-1 )printf( " " );
            
        }
        printf( "n" ); 
        scanf( "%c", &c );
    }
    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 *