poj1659 Frogs' Neighborhood

链接:http://poj.org/problem?id=1659
分析:Havel-Hakimi定理的直接应用

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<bitset>

using namespace std;
int n;
int a[11][11];
struct node
{
    int x,y;
}b[11];
int cmp(node a,node b)
{
    return a.x<b.x;
}
int main()
{
    int T;
    cin>>T;
    while (T--)
    {
        cin>>n;
        for (int i=1;i<=n;i++)
            cin>>b[i].x,b[i].y=i;
        memset(a,0,sizeof(a));
        int flag=0;
        for (int i=1;i<=n-1;i++)
        {
            sort(b+1,b+1+n-i+1,cmp);
            int k=b[n-i+1].x;
            for (int j=n-i;k;j--)
            {
                b[j].x--;
                k--;
                a[b[n-i+1].y][b[j].y]=a[b[j].y][b[n-i+1].y]=1;
                if (j<1||b[j].x<0)
                {
                    flag=1;
                    break;
                }
            }
            if (flag) break;
        }
        if (b[1].x) flag=1;
        if (flag) printf("NO\n\n");
        else
        {
            printf("YES\n");
            for (int i=1;i<=n;i++)
            {
                for (int j=1;j<=n;j++)
                {
                    printf("%d",a[i][j]);
                    if (j!=n) printf(" ");
                    else printf("\n");
                }
            }
            printf("\n");
        }
    }
    return 0;
}

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