poj 1659 Frogs' Neighborhood

简单的Havel-Hakimi定理(建图)的应用。

 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn = 15;
int ee[maxn][maxn];
struct abc{ int f, node; }dt[maxn];
bool cmp(const abc&a, const abc&b){ return a.f > b.f; }
int main()
{
    int sb;
    scanf("%d", &sb);
    while (sb--)
    {
        int n, i, j;
        scanf("%d", &n);
        int ff = 0;
        memset(ee, 0, sizeof(ee));
        for (i = 1; i <= n; i++){ scanf("%d", &dt[i].f); dt[i].node = i; }
        for (i = 1; i <= n; i++)
        {
            sort(dt + i, dt + n + 1, cmp);
            for (j = i + 1; j <= dt[i].f + i; j++)
            {
                dt[j].f--;
                if (dt[j].f < 0){ ff = 1; break; }
                ee[dt[i].node][dt[j].node] = 1;
                ee[dt[j].node][dt[i].node] = 1;
            }
            if (ff == 1) break;
        }
        if (ff == 1) printf("NO\n");
        else
        {
            printf("YES\n");
            for (i = 1; i <= n; i++)
            {
                for (j = 1; j <= n; j++)
                {
                    if (j < n) printf("%d ", ee[i][j]);
                    else printf("%d\n", ee[i][j]);
                }
            }
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2015-04-29 21:27  Fighting_Heart  阅读(134)  评论(0编辑  收藏  举报