POJ 1659 Frogs' Neighborhood

POJ 1659 Frogs' Neighborhood
题目大意:给出无向图每个顶点的度,试图构建一个符合条件的图。
见Havel-Hakimi定理。
以下是我的代码:
#include < algorithm >
#include
< cstdio >
#include
< cstring >
using   namespace  std;
const   int  kMaxn( 17 );

struct  Type
{
    
int  num,degree;
};
bool   operator < ( const  Type  & a, const  Type  & b)
{
    
return  (a.degree > b.degree  ||  (a.degree == b.degree  &&  a.num < b.num));
}

int  main()
{
    
/*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    //
*/

    
int  T;
    scanf(
" %d " , & T);
    
for ( int  case_num = 1 ;case_num <= T;case_num ++ )
    {
        
int  n;
        Type r[kMaxn];
        scanf(
" %d " , & n);
        
for ( int  i = 1 ;i <= n;i ++ )
        {
            r[i].num
= i;
            scanf(
" %d " , & r[i].degree);
        }

        
bool  success( true );
        
int  g[kMaxn][kMaxn];
        memset(g,
0 ,kMaxn * kMaxn * sizeof ( int ));
        
for ( int  i = 1 ;i <= n;i ++ )
        {
            sort(r
+ i,r + n + 1 );
            
if (r[i].degree < 0 )
            {
                success
= false ;
                
break ;
            }
            
for ( int  j = i + 1 ;j <= &&  j <= i + r[i].degree;j ++ )
            {
                g[r[i].num][r[j].num]
= g[r[j].num][r[i].num] = 1 ;
                r[j].degree
-- ;
            }
        }

        
if (case_num != 1 )
            printf(
" \n " );
        
if ( ! success)
            printf(
" NO\n " );
        
else
        {
            printf(
" YES\n " );
            
for ( int  i = 1 ;i <= n;i ++ )
            {
                
for ( int  j = 1 ;j <= n;j ++ )
                {
                    
if (j != 1 )
                        printf(
"   " );
                    printf(
" %d " ,g[i][j]);
                }
                printf(
" \n " );
            }
        }
    }

    
return   0 ;
}

你可能感兴趣的:(POJ 1659 Frogs' Neighborhood)