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

"
); else { printf("YES
"
); 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("
"
); } } printf("
"
); } } return 0; }

좋은 웹페이지 즐겨찾기