PKU1659+havel-hakimi定理

http://www.docin.com/p-540438846.html
该定理链接。
 1 /*

 2 havel-hakimi定理

 3 http://www.docin.com/p-540438846.html

 4 */

 5 #include<stdio.h>

 6 #include<string.h>

 7 #include<stdlib.h>

 8 #include<algorithm>

 9 #include<iostream>

10 #include<queue>

11 #include<map>

12 #include<math.h>

13 using namespace std;

14 typedef long long ll;

15 //typedef __int64 int64;

16 const int maxn = 24;

17 const int inf = 0x7fffffff;

18 const double pi=acos(-1.0);

19 const double eps = 1e-8;

20 struct node{

21     int num,d;

22 }a[ maxn ];

23 int mat[ maxn ][ maxn ];

24 int cmp( node a,node b ){

25     return a.d>b.d;

26 }

27 bool flag;

28 

29 int Judge( int n ){

30     for( int i=0;i<n;i++ ){

31         if( a[i].d<0 ){

32             flag = false;

33             return 0;

34         }

35     }

36     for( int i=0;i<n;i++ ){

37         if( a[i].d>0 ){

38             return 1;

39         }

40     }

41     flag = true;

42     return 0;

43 }

44  

45 int main(){

46     int ca;

47     scanf("%d",&ca);

48     int cc = 0;

49     while( ca-- ){

50         int n;

51         scanf("%d",&n);

52         memset( mat,0,sizeof( mat ) );

53         for( int i=0;i<n;i++ ){

54             scanf("%d",&a[ i ].d);

55             a[ i ].num = i;

56         }

57         if( cc!=0 ){

58             printf("\n");

59         }

60         cc++; 

61         flag = true;

62         while( 1 ){

63             if( Judge( n )==0 ) break;

64             sort( a,a+n,cmp );

65             for( int i=1;i<=a[0].d;i++ ){

66                 a[ i ].d--;

67                 mat[ a[0].num ][ a[ i ].num ] = mat[ a[ i ].num ][ a[ 0 ].num ] = 1;

68             }

69             a[ 0 ].d = 0;

70         //    if( Judge( n )==0 ) break;

71         }

72         if( flag==false ) printf("NO\n");

73         else {

74             printf("YES\n");

75             for( int i=0;i<n;i++ ){

76                 for( int j=0;j<n;j++ ){

77                     if( j==0 ) printf("%d",mat[i][j]);

78                     else printf(" %d",mat[i][j]);

79                 }

80                 printf("\n");

81             }

82         }

83     }

84     return 0;

85 }

86         

87     
View Code

 

你可能感兴趣的:(pku)