POJ 1659 Frogs' Neighborhood(度序列構圖),pojfrogs

來源:互聯網
上載者:User

POJ 1659 Frogs' Neighborhood(度序列構圖),pojfrogs

題意  中文

根據Havel-Hakimi定理構圖就行咯  先把頂點按度數從大到小排序  可圖的話  度數大的頂點與它後面的度數個頂點相連肯定是滿足的  出現了-1就說明不可圖了

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 20;int mat[N][N], ord[N];bool cmp(int i, int j){    return mat[i][0] > mat[j][0];}int main(){    int cas, i, j, k, t, n;    scanf("%d", &cas);    while(cas--)    {        memset(mat, 0, sizeof(mat));        scanf("%d", &n);        for(i = 1; i <= n; ++i)        {            scanf("%d", &mat[i][0]);            ord[i] = i;        }        for(i = 1; i <= n; ++i)        {            sort(ord + i, ord + n + 1, cmp);            t = ord[i];            if(mat[t][0] < 0) break;            for(j = 1; j <= mat[t][0]; ++j)            {                k = ord[i + j];                mat[t][k] = mat[k][t] = 1;                --mat[k][0];            }        }        if(i <= n) printf("NO\n");        else        {            printf("YES\n");            for(i = 1; i <= n; ++i)            {                for(int j = 1; j <= n; ++j)                    printf("%d ", mat[i][j]);                printf("\n");            }        }        if(cas) printf("\n");    }    return 0;}

Frogs' Neighborhood

Description

未名湖附近共有N個大小湖泊L1, L2, ..., Ln(其中包括未名湖),每個湖泊Li裡住著一隻青蛙Fi(1 ≤ i ≤ N)。如果湖泊LiLj之間有水路相連,則青蛙FiFj互稱為鄰居。現在已知每隻青蛙的鄰居數目x1, x2, ..., xn,請你給出每兩個湖泊之間的相連關係。

Input

第一行是測試資料的組數T(0 ≤ T ≤ 20)。每組資料包括兩行,第一行是整數N(2 < N < 10),第二行是N個整數,x1, x2,..., xn(0 ≤ xi ≤ N)。

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 

Source

POJ Monthly--2004.05.15 Alcyone@pku



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.