特例---可图化

来源:互联网 发布:软件著作权 增值税 编辑:程序博客网 时间:2024/06/14 11:55
特例---可图化特例---可图化
 
A - Frogs' Neighborhood
Time Limit:5000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
SubmitStatus

Description

未名湖附近共有N个大小湖泊L1,L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ iN)。如果湖泊LiLj之间有水路相连,则青蛙FiFj互称为邻居。现在已知每只青蛙的邻居数目x1,x2, ..., xn,请你给出每两个湖泊之间的相连关系。

Input

第一行是测试数据的组数T(0 ≤ T ≤ 20)。每组数据包括两行,第一行是整数N(2 < N < 10),第二行是N个整数,x1,x2,..., xn(0 ≤xiN)。

Output

对输入的每组测试数据,如果不存在可能的相连关系,输出"NO"。否则输出"YES",并用N×N的矩阵表示湖泊间的相邻关系,即如果湖泊i与湖泊j之间有水路相连,则第i行的第j个数字为1,否则为0。每两个数字之间输出一个空格。如果存在多种可能,只需给出一种符合条件的情形。相邻两组测试数据之间输出一个空行。

Sample Input

374 3 1 5 4 2 1 64 3 1 4 2 0 62 3 1 1 2 1

Sample Output

YES0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 0 1 1 0 1 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 NOYES0 1 0 0 1 0 1 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0
 MicrosoftInternetExplorer402DocumentNotSpecified7.8Normal0

(可图性定理)

可图性定理的一个应用,不会这个定理的话,也可以直接模拟。

 

#include 

#include 

#include 

#include 

using namespace std;

 

int g[12][12];

int n;

 

struct node

{

    int pos, degree;

}a[12];

 

int cmp(const node &x, const node &y)

{

    return x.degree y.degree;

}

 

int havel_hakimi()

{

    for (int i=0; i

    {

        sort(a+i, a+n, cmp);

        if (i+a[i].degree >= n)

            return 0;

        for (int j=i+1; j<=i+a[i].degree; j++)

        {

            a[j].degree --;

            if (a[j].degree 0)

                return 0;

            g[a[i].pos][a[j].pos] 1;

            g[a[j].pos][a[i].pos] 1;

        }

    }

    if (a[n-1].degree != 0)

        return 0;

 

    return 1;

}

 

int main()

{

    int t, temp;

    cin >> t;

    while (cin >> n)

    {

        memset(g, 0, sizeof(g));

        for (int i=0; i

        {

            scanf("%d", &temp);

            a[i].degree temp;

            a[i].pos i;

        }

        if (!havel_hakimi())

            cout << "NO" << endl;

        else

        {

            cout << "YES" << endl;

            for (int i=0; i

            {

                for (int j=0; j

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

                cout << endl;

            }

        }

        cout << endl;

    }

    return 0;

}

特例---可图化
0 0
原创粉丝点击