心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0
题目大意:给出无向图每个顶点的度,试图构建一个符合条件的图。
见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;
}
posted on 2011-05-26 13:10 lee1r 阅读(163) 评论(0)  编辑 收藏 引用 所属分类: 题目分类:图论

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理