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);
}
* 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);
}
相关文章推荐
- pku1659 Frogs' Neighborhood(havel定理)
- pku 1659 (Havel定理)
- PKU1659(Frogs' Neighborhood)-简单图的判定+构造,Havel定理
- 【Havel-Hakimi定理】PKU-1659-Frogs' Neighborhood
- pku 1659(判断可图化Havel-Hakiwi定理的应用)
- pku1659 Frogs' Neighborhood
- 从点的度推出邻接图(贪心解)——pku1659
- PKU 1659 Frogs' Neighborhood
- PKU 1659 Frogs' Neighborhood - 贪心 Havel定理
- PKU1659+havel-hakimi定理
- SSL1659 栈练习括弧匹配检验
- PJOI PKU Campus 2011 B:A Problem about Tree LCA 求随意点x为根的y的父节点
- PKU 3667 Hotel (线段树,区间合并,最长连续区间)
- bzoj1467: Pku3243 clever Y
- bzoj1895: Pku3580 supermemo
- 【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS
- 【BZOJ】1954: Pku3764 The xor-longest Path
- pku1204 Word Puzzles AC自动机 二维字符串矩阵8个方向找模式串的起点坐标以及方向 挺好的!
- PKU 2195 Going Home
- Frogs' Neighborhood POJ - 1659 havel定理