pku 1659(判断可图化Havel-Hakiwi定理的应用)

/*

  Name: Havel-Hakiwi定理的应用 

  Copyright: 

  Author: Try86

  Date: 13/04/12 23:16

  Description: 

*/



#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <iostream>



using namespace std;



int map[15][15], flag;

struct node {

    int deg; //顶点度数 

    int ver; //顶点编号 

}d[15];





int cmp(const void *a, const void *b) {

    return ((node *)b)->deg - ((node *)a)->deg;

}



void Havel_Hakiwi(int n) {

    for (int i=0; i<n; ++i) {

        qsort(d, n, sizeof(node), cmp);

        int s = d[0].deg;

        d[0].deg = 0;

        if (s > n-i-1) { 

            flag = 0;

            return ;

        }

        for (int j=1; j<=s; ++j) {

            --d[j].deg;

            if (d[j].deg < 0) {

                flag = 0;

                return ;

            }

            map[d[0].ver][d[j].ver] = map[d[j].ver][d[0].ver] = 1;

        }

        if (!flag) return ;

    }

    return ;

}



int main() {

    int t;

    scanf ("%d", &t);

    while (t--) {

        int n;

        scanf ("%d", &n);

        memset(map, 0, sizeof(map));

        for (int i=0; i<n; ++i) {

            scanf ("%d", &d[i].deg);

            d[i].ver = i;

        }

        flag = 1;

        Havel_Hakiwi(n);

        if (flag) {

            printf ("YES\n");

            for (int i=0; i<n; ++i) {

                for (int j=0; j<n; ++j) {

                    if (j) printf (" %d", map[i][j]);

                    else printf ("%d", map[i][j]);

                }

                printf ("\n");

            }

        }

        else printf ("NO\n");

        printf ("\n"); //注意输出一空行,不然判WA,不是PE 

    }

    return 0;    

}

 

你可能感兴趣的:(pku)