Frogs' Neighborhood

Frogs' Neighborhood
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 7920   Accepted: 3392   Special Judge

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

3
7
4 3 1 5 4 2 1 
6
4 3 1 4 2 0 
6
2 3 1 1 2 1 

Sample Output

YES
0 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 

NO

YES
0 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 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <iomanip>
13 using namespace std;
14 const int INF=0x5fffffff;
15 const int MS=15;
16 const double EXP=1e-8;
17 struct node
18 {
19     int degree;
20     int id;
21     bool operator <(const node &b) const
22     {
23         return degree>b.degree;
24     }
25 }nodes[MS];
26 
27 int edges[MS][MS];
28 
29 int main()
30 {
31     int i,j,k;
32     int n,T;
33     cin>>T;
34     while(T--)
35     {
36         cin>>n;
37         for(i=0;i<n;i++)
38         {
39             cin>>nodes[i].degree;
40             nodes[i].id=i;
41         }
42         memset(edges,0,sizeof(edges));
43         int flag=1;
44         for(k=0;k<n&&flag;k++)
45         {
46             sort(nodes+k,nodes+n);
47             i=nodes[k].id;
48             int du=nodes[k].degree;
49             if(du>n-k-1)
50                 flag=0;
51             for(int s=1;s<=du&&flag;s++)
52             {
53                 j=nodes[s+k].id;
54                 if(nodes[s+k].degree<=0)
55                     flag=0;
56                 nodes[s+k].degree--;
57                 edges[i][j]=edges[j][i]=1;
58 
59             }
60         }
61         if(flag)
62         {
63             cout<<"YES"<<endl;
64             for(i=0;i<n;i++)
65             {
66                 for(j=0;j<n;j++)
67                 {
68                     if(j)
69                         cout<<" ";
70                     cout<<edges[i][j];
71                 }
72                 cout<<endl;
73             }
74         }
75         else
76             cout<<"NO"<<endl;
77         if(T)
78             cout<<endl;
79     }
80     return 0;
81 }

原文地址:https://www.cnblogs.com/767355675hutaishi/p/4301004.html