您的位置:首页 > 其它

pku1659

2010-05-14 11:37 134 查看
/*
* File: pku1659.cpp
* 题意:给定一些点的度数,问根据这些度数能否构成一个图。
* 根据大牛说的Havel定理进行贪心。
* Author: chenjiang
*
* Created on 2010年5月13日, 下午9:24
*/

#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int mapmap[11][11];

struct node {
int id;
int degree;
} v[11];

int cmp(const node&a, const node&b) {
return a.degree > b.degree;
}

/*
*
*/
int main(int argc, char** argv) {

int i, j, T, n;
int s;
//freopen("a.a", "r", stdin);
cin >> T;
for (s = 1; s <= T; s++) {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> v[i].degree;
v[i].id = i;
}
memset(mapmap, 0, sizeof (mapmap));
bool flag = 1;
for (i = 1; i <= n; i++) {
sort(v + 1, v + n + 1, cmp);//根据度从大到小排序
int next = v[1].degree;//next保存最大的度
if (next <= 0)break;
v[1].degree = 0;//最大的度置0
for (j = 2; j <= next + 1; j++) {//这个地方wa了我很久,没注意要+1,囧
v[j].degree--;//以后每个点的度减1
if (v[j].degree < 0) {//如果某个点的度出现了负数,则不能构成一个图
flag = 0;
break;
}
mapmap[v[1].id][v[j].id] = 1;//连边
mapmap[v[j].id][v[1].id] = 1;
}
if (flag == 0)break;
}
for (i = 1; i <= n; i++) {
if (v[i].degree != 0) {//如果还存在非零的度,则不能构成一个图
flag = 0;
break;
}
}
if (flag) {
cout << "YES" << endl;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (j != n) {
cout << mapmap[i][j] << " ";
} else {
cout << mapmap[i][j] << endl;
}
}
}
if (s != T)cout << endl;
} else {
cout << "NO" << endl;
if (s != T)cout << endl;
}
}
return (EXIT_SUCCESS);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: