Degree Sequence of Graph G【模拟】

该博客讨论了图论中的度序列问题,通过模拟建图过程来判断一个非负整数序列是否能构成一个简单图。王海洋,一个对科学问题充满好奇心的年轻人,提出了这个问题:非负整数序列是否总能对应于一个简单图的度序列?博客提供了一个解决方案,通过直接模拟连接各个点来检查是否所有点的度都能变为0,从而确定序列是否可行。
摘要由CSDN通过智能技术生成

Degree Sequence of Graph G
时间限制: 1 Sec 内存限制: 128 MB
提交: 362 解决: 92
[提交] [状态] [命题人:admin]
题目描述
Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep love and yearns for the boundless oceans. After graduation, he came to a coastal city and got a job in a marine transportation company. There, he held a position as a navigator in a freighter and began his new life.

The cargo vessel, Wang Haiyang worked on, sails among 6 ports between which exist 9 routes. At the first sight of his navigation chart, the 6 ports and 9 routes on it reminded him of Graph Theory that he studied in class at university. In the way that Leonhard Euler solved The Seven Bridges of Königsberg, Wang Haiyang regarded the navigation chart as a graph of Graph Theory. He considered the 6 ports as 6 nodes and 9 routes as 9 edges of the graph. The graph is illustrated as below.
在这里插入图片描述

According to Graph Theory, the number of edges related to a node is defined as Degree number of this node.

Wang Haiyang looked at the graph and thought, “If arranged, the Degree numbers of all nodes of graph G can form such a sequence: 4, 4, 3,3,2,2, which is called the degree sequence of the graph. Of course, the degree sequence of any simple graph (according to Graph Theory, a graph without any parallel edge or ring is a simple graph) is a non-negative integer sequence”

Wang Haiyang is a thoughtful person and tends to think deeply over any scientific problem that grabs his interest. So as usual, he also gave this problem further thought, “as we know, any a simple graph always corresponds with a non-negative integer sequence. But whether a non-negative integer sequence always corresponds with the degree sequence of a simple graph? That is, if given a non-negative integer sequence, are we sure that we can draw a simple graph according to it.”

Let’s put forward such a definition: provided that a non-negative integer sequence is the degree sequence of a graph without any parallel edge or ring, that is, a simple graph, the sequence is draw-possible, otherwise, non-draw-possible. Now the problem faced with Wang Haiyang is how to test whether a non-negative integer sequence is draw-possible or not. Since Wang Haiyang hasn’t studied Algorithm Design course, it is difficult for him to solve such a problem. Can you help him?

输入
The first line of input contains an integer T, indicates the number of test cases. In each case, there are n+1 numbers; first is an integer n (n<1000), which indicates there are n integers in the sequence; then follow n integers, which indicate the numbers of the degree sequence.

输出
For each case, the answer should be “yes” or “no”, indicating this case is “draw-possible” or “non-draw-possible”.

样例输入
复制样例数据
2
6 4 4 3 3 2 2
4 2 1 1 1
样例输出
yes
no

题目大意:
先输入一个数字 T T T,代表有 T T T组测试数据,对每组测试数据,输入每个点的入度和出度之和,问其是否能构成一个简单图(无环也无平行边)。

解题思路:
由于要构成一个简单图,所以我们可以直接模拟建图的过程即可,对于每个点,将其与任意 a r r [ i ] arr[i] arr[i]个点相连, a r r [ i ] arr[i] arr[i]为这个点的度,并相应减去对应点得度,最后观察是否会使得所有点的度均为0即可。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int arr[1200];
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    //ios::sync_with_stdio(0),cin.tie(0);
    int T;
    scanf("%d",&T);
    while(T--) {
    	int n;
    	scanf("%d",&n);
    	bool ju=false;
    	rep(i,1,n) {
    		scanf("%d",&arr[i]);
    	}
    	bool flag = true;
    	sort(arr+1,arr+1+n,greater<int> ());
    	rep(i,1,n){
    		rep(j,2,arr[1]+1){
    			if(arr[j]>0)arr[j]--;
    			else{
    			 	flag = false;
    			 	break;
    			}
    		}
    		arr[1] = 0;
    		sort(arr+1,arr+1+n,greater<int> ());
    		if(!flag)break;
    	}
    	if(arr[1]>0)flag = false;
    	if(flag)printf("yes\n");
    	else printf("no\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值