特例---可圖化

特例---可圖化特例---可圖化
 
A - Frogs' Neighborhood
Time Limit:5000MS     Memory Limit:10000KB    64bit IO Format:%I64d & %I64u

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;

}

特例---可圖化
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章