Shirlies
宁静专注认真的程序媛~

 Havel-Hakimi定理

http://blog.sina.com.cn/s/blog_818d3d930100wdv1.html

http://hi.baidu.com/krdefndrsbbekmd/item/a7c58810f40156f8dceecab7

View Code
 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 struct node
 7 {
 8     int u;
 9     int dg;
10 }p[15];
11 int N;
12 int map[15][15];
13 
14 bool cmp(const node& a,const node& b)
15 {
16     return a.dg > b.dg;
17 }
18 
19 int main()
20 {
21    int cases;
22    cin >> cases;
23    while(cases --)
24    {
25        cin >> N;
26        memset(map,0,sizeof(map));
27        for(int i = 0;i < N;i ++)
28        {
29            cin >> p[i].dg;
30            p[i].u = i;
31        }
32 
33        bool flag = false;
34        while(true)
35        {
36            sort(p,p+N,cmp);
37            int t = 0;
38            for(int i = 1;i <= p[0].dg;i ++)
39            {
40                p[i].dg -= 1;
41                if(p[i].dg < 0)
42                {
43                    flag = true;
44                    break;
45                }
46                map[p[0].u][p[i].u] = 1;
47                map[p[i].u][p[0].u] = 1;
48                t = t | p[i].dg;
49            }
50            if(flag)
51            {
52                break;
53            }
54            for(int i = p[0].dg + 1;i < N;i ++)
55            {
56                t = t | p[i].dg;
57            }
58            p[0].dg = 0;
59            if(t == 0)
60            {
61                break;
62            }
63        }
64 
65        if(flag)
66        {
67            cout << "NO" << endl;
68        }
69        else
70        {
71            cout << "YES" << endl;
72            for(int i = 0;i < N;i ++)
73            {
74                cout << map[i][0];
75                for(int j = 1;j < N;j ++)
76                {
77                    cout << " " << map[i][j];
78                }
79                cout << endl;
80            }
81        }
82         if(cases) cout << endl;
83    }
84 
85    return 0;
86 }
posted on 2012-09-05 09:43  Shirlies  阅读(228)  评论(0编辑  收藏  举报