本题主要是考查给定一个图的度序列,判断该度序列是否可图。
解决办法:Havel-Hakimi定理 结构体排序
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
struct vertex
{
int degree;//顶点的度
int index;//顶点序号
}v[15];
bool struct_compare(const vertex v1,const vertex v2)
{
return v1.degree>=v2.degree;
}
int main()
{
//freopen("test.txt","r",stdin);
int t,n,i,j,k,d1,r;
int edge[15][15],flag;
cin>>t;
while(t--)
{
cin>>n;
for(i=0;i<n;i++)
{
cin>>v[i].degree;
v[i].index = i;
}
flag = 1;
k = 0;
memset(edge,0,sizeof(edge));
while(k<n && flag)
{
sort(v+k,v+n,struct_compare);
d1 = v[k].degree;
i = v[k].index;
for(r=1;r<=d1&&flag;r++)
{
j = v[k+r].index;
if(v[k+r].degree <= 0)
flag = 0;
v[k+r].degree--;
edge[i][j] = edge[j][i] = 1;
}
k++;
}
if(flag)
{
cout << "YES" << endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(j)
cout << ' ';
cout << edge[i][j];
}
cout << endl;
}
}
else
cout << "NO" << endl;
if(t)
cout << endl;
}
//cout << "Hello world!" << endl;
return 0;
}