POJ-1659 Frogs' Neighborhood---Havel-Hakimi定理

题目链接:

https://vjudge.net/problem/POJ-1659

题目大意:

给定度数列,判断是否可以建图

思路:

Havel-Hakimi定理

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #include<stack>
 8 #include<map>
 9 #include<set>
10 #include<sstream>
11 #include<functional>
12 using namespace std;
13 typedef long long ll;
14 const int maxn = 1e2 + 10;
15 const int INF = 1e9 + 7;
16 int T, n, m, cases;
17 int Map[maxn][maxn];
18 struct node
19 {
20     int x, id;
21     bool operator<(const node & a)const
22     {
23         return x > a.x;
24     }
25 }a[maxn];
26 
27 int main()
28 {
29     cin >> T;
30     while(T--)
31     {
32         cin >> n;
33         for(int i = 0; i < n; i++)
34         {
35             cin >> a[i].x;
36             a[i].id = i;
37         }
38         memset(Map, 0, sizeof(Map));
39         bool flag = 1;
40         for(int i = 0; i < n; i++)
41         {
42             sort(a + i, a + n);
43             if(a[i].x >= n - i)
44             {
45                 flag = 0;
46                 break;
47             }
48             for(int j = 1 + i; j <= a[i].x + i; j++)
49             {
50                 a[j].x--;
51                 if(a[j].x < 0)flag = 0;
52                 Map[a[i].id][a[j].id] = Map[a[j].id][a[i].id] = 1;
53             }
54         }
55         if(flag)
56         {
57             puts("YES");
58             for(int i = 0; i < n; i++)
59             {
60                 printf("%d", Map[i][0]);
61                 for(int j = 1; j < n; j++)
62                 {
63                     printf(" %d", Map[i][j]);
64                 }
65                 puts("");
66             }
67         }
68         else puts("NO");
69         if(T)cout<<endl;
70     }
71     return 0;
72 }

 

posted @ 2018-04-08 16:21  _努力努力再努力x  阅读(257)  评论(0编辑  收藏  举报