UVa 10720 - Graph Construction(Havel-Hakimi定理)

题目链接: 传送门

Graph Construction

Time Limit: 3000MS     Memory Limit: 65536K

Description

Graph is a collection of edges E and vertices V. Graph has a wide variety of applications in computer. There are different ways to represent graph 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

4 3 3 3 3 6 2 4 5 5 2 1 5 3 2 3 2 1 0

Sample Output

Possible
Not possible 
Not possible
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

bool cmp(int x,int y)
{
	return x>y;
}


int main()
{
	int N;
	while (~scanf("%d",&N) && N)
	{
		int ans[10005] = {0};
		bool flag = true;
		for (int i = 0;i < N;i++)
		{
			scanf("%d",&ans[i]);
		}
		while (flag)
		{
			sort(ans,ans+N,cmp);
			int tmp = ans[0];
			if (tmp == 0)
			{
				break;
			}
			for (int i = 1;i <= tmp;i++)
			{
				ans[i]--;
				if (ans[i] < 0)
				{
					flag = false;
					break;
				}
			}
			ans[0] = 0;
			if (!flag)
			{
				break;
			}
		}
		if (!flag)
		{
			printf("Not possible\n");
		}
		else
		{
			printf("Possible\n");
		}
	}
	return 0;
}
posted @   zxzhang  阅读(272)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示

目录导航