UVA 10720 Graph Construction(贪心 + Havel-Hakimi定理)
2013-08-18 20:54
375 查看
Problem C | Graph Construction |
Time Limit | 2 Seconds |
in computer. It can be represented by adjacency matrix or by adjacency list. There are some other ways to represent graph. One of them is to write the degrees (the numbers of edges that a vertex has) of each vertex. If there are n vertices
then n integers can represent that graph. In this problem we are talking about simple graph which does not have same endpoints for more than one edge, and also does not have edges with the same endpoint.
Any graph can be represented by n number of integers. But the reverse is not always true. If you are given n integers, you have to find out whether
this n numbers can represent the degrees of n vertices of a graph.
Input
Each line will start with the number n (≤ 10000). The next n integers will represent the degrees of n vertices of the graph. A 0 input for n will indicate end of input which should not be processed.
Output
If the n integers can represent a graph then print “Possible”. Otherwise print “Not possible”. Output for each test case should be on separate line.
Sample Input | Output for Sample Input |
4 3 3 3 3 6 2 4 5 5 2 1 5 3 2 3 2 1 0 | Possible Not possible Not possible |
Judge Solution: Mohammed Shamsul Alam
Special thanks to Tanveer Ahsan[/align]
题意:给定n个点。和每个点的度数,判断这些点能不能连成一个图。
思路:贪心中的Havel-Hakimi定理,详细参考http://sbp810050504.blog.51cto.com/2799422/883904
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n, v[10005];
int cmp(int a, int b) {
return a > b;
}
bool solve() {
while (v[0] != 0) {
for (int i = 1; i <= v[0]; i ++) {
v[i] --;
if (v[i] < 0)
return false;
}
v[0] = 0;
sort(v, v + n, cmp);
}
return true;
}
int main() {
while (~scanf("%d", &n) && n) {
memset(v, 0, sizeof(v));
for (int i = 0; i < n ; i ++)
scanf("%d", &v[i]);
sort(v, v + n, cmp);
if (solve()) printf("Possible\n");
else printf("Not possible\n");
}
return 0;
}
相关文章推荐
- uva 10720 - Graph Construction(贪心-Havel-Hakimi)
- [贪心]UVA10720 - Graph Construction
- uva10720 - Graph Construction(Havel-Hakimi定理)
- UVa 10720 - Graph Construction(Havel-Hakimi定理)
- UVA 10720 Graph Construction 贪心+优先队列
- Graph Construction uva+贪心+Havel-Hakimi定理
- UVA-10720 Graph Construction(Havel-Hakimi定理)
- Havel-Hakimi定理(判断一个序列是否可图) UVA 10720
- UVA - 10720 Graph Construction 公式+贪心
- uva10720(Graph Construction)贪心+havel定理
- uva 10720 Graph Construction(贪心)
- uva 10720(贪心)
- UVa:1611 Crane(贪心)
- uva 1610 Party Games (贪心+STL字符串)
- EDITOR NOTTOOBAD uva+贪心
- UVaLive 6698 Sightseeing Bus Drivers (水题,贪心)
- UVA - 10714 Ants(贪心)
- uvalive 2911(贪心)
- uva 10026 Shoemaker's Problem _贪心
- UVa 11134 Fabled Rooks(贪心+优先队列)