Degree Sequence of Graph G【模拟】

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

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

解题思路:
由于要构成一个简单图,所以我们可以直接模拟建图的过程即可,对于每个点,将其与任意arr[i]arr[i]arr[i]个点相连,arr[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_JUDGEfreopen("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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536270.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【动态规划】魔法石矿

【动态规划】魔法石矿 时间限制: 1 Sec 内存限制: 64 MB 提交: 116 解决: 27 [提交] [状态] [命题人:admin] 题目描述 为了找到回家的路&#xff0c;张琪曼施展魔法&#xff0c;从高维空间召唤出了一种叫作“读者”的生物&#xff0c;据说“读者”这种生物无所不能&#xff0c;…

简单类及成员实例【C#】

简单类及成员实例&#xff08;C#&#xff09; 题目描述 简单类及成员实例。定义了如下图所示类Student&#xff0c;根据下图和给出代码&#xff0c;补写缺失的代码。 using System; namespace sample{ class Student { public string studentid;//学号 p…

c#随机数的产生与输出【C#】

c#随机数的产生与输出 题目描述 编写一个实例方法Method01。该方法使用Random类随机产生n个3位数字&#xff08;如636&#xff09;的随机正整数&#xff0c;并把产生的随机数存入数组中并输出该数组int num Convert.ToInt32(Console.ReadLine()); using System; using System…

C# teacher类【C#】

C# teacher类 题目描述 定义一个教师类Teacher&#xff0c;具体要求如下&#xff1a; 1、私有字段工号no&#xff08;string&#xff09;、姓名name&#xff08;string&#xff09;、出生日期birthday&#xff08;DateTime&#xff09;、性别sex&#xff08;SexFlag&#xff0…

接口实例(C#,IShape)【C#】

接口实例&#xff08;C#,IShape&#xff09; 题目描述 接口实例。接口和类如下图所示&#xff0c;根据给出代码&#xff0c;补写缺失的代码&#xff0c;然后在Program类的静态Main方法中验证所实现的类。 using System; namespace Myinterface { public interface IShape…

1439: 2.4.5 Fractions to Decimals 分数化小数

1439: 2.4.5 Fractions to Decimals 分数化小数 时间限制: 1 Sec 内存限制: 64 MB提交: 194 解决: 13题目描述 写一个程序&#xff0c;输入一个形如N/D的分数(N是分子&#xff0c;D是分母)&#xff0c;输出它的小数形式。 如果小数有循环节的话&#xff0c;把循环节放在一对圆…

Problem B: 求各位数字之和

#include <stdio.h> #include <stdlib.h> int main() { int n,sum0,m; while(~scanf("%d",&n)) { while(n>0) { mn%10; nn/10; summ; } printf("%d\n",sum); sum0; } return 0; }

Problem C: 判断字符串是否为回文

#include <stdio.h> #include <stdlib.h> int main() { int i,j,n; char str[10]; gets(str); nstrlen(str); for(i0,jn-1;i<j;i,j--) { if(str[i]!str[j]) { printf("No\n"); break; } } if(i>j)printf("Yes\n"); return 0; }

Problem A: 童年生活二三事

斐波那契数列:F(n)F(n-1)F(n-2) #include <stdio.h> #include <stdlib.h> int f(int n) {int b;if(n1)b1;if(n2)b2;if(n>2)bf(n-1)f(n-2);return b; }int main() {int a,n;while(~scanf("%d",&n)&&n!0){af(n);printf("%d\n",a…

Problem C: 01字串

#include <stdio.h> #include <stdlib.h>int main() {int i,j,n0,m0;char a[129][8];for(i0;i<128;i){for(j0;j<7;j){a[i][j]n%2;nn/2;}m;nm;}for(i0;i<128;i){for(j6;j>0;j--)printf("%d",a[i][j]);printf("\n");}return 0; }

汉诺塔III

#include <stdio.h> #include <stdlib.h> int main() {int fx(int );int n,m;while(~scanf("%d",&n)){mfx(n);printf("%d\n",m);}return 0; } int fx(int n) {int m;if(n1)m2;if(n>1)m3*fx(n-1)2;return m; }

骨牌铺方格

骨牌铺方格 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 46495 Accepted Submission(s): 22470 Problem Description在2n的一个长方形方格中,用一个1 2的骨牌铺满方格,输入n ,输出铺放方案的总数.例如n3时…

递归思想完成n皇后问题

已经很长时间不敲代码了&#xff0c;感觉自己越来与颓废&#xff0c;所以现在又想做回一名苦逼的程序员&#xff0c;开启自己的代码之路。 我是根据视频敲的&#xff0c;没有题目&#xff0c;先看个四皇后问题吧。 所谓4皇后问题就是求解如何在44的棋盘上无冲突的摆放4个皇后棋…

virtualbox的USB识别

VirtualBox识别USB教程 作者&#xff1a;Vincent June 13, 2017 在Virtualbox虚拟机配置面板中打开USB设备选项&#xff0c;分别勾选上“启动USB控制器”“启用usb2.0控制器”选项&#xff0c;如果有错误去https://www.virtualbox.org/wiki/Downloads 下载相应版本的插件包&a…

修改win10我的文档下载等移动别处

win10移动我的文档&#xff0c;下载等到其他盘符办法 解决办法 1.选择我的文档&#xff0c;鼠标右键选择属性&#xff0c;在工具栏选择位置&#xff0c;然后选择想移动到哪里的盘符即可&#xff0c;如图&#xff1a;2.操作完后选择应用->确定&#xff0c;就这么简单。

Ubuntu下的提示信息彩色显示

【问题】 虽然已经折腾过了&#xff1a; 【已解决】Ubuntu中让终端只显示当前路径&#xff0c;而不显示绝对路径 但是&#xff0c;终端中的prompt提示信息&#xff0c;不是彩色的&#xff0c;导致的结果是&#xff1a; 当终端中输出信息很多时&#xff1a; 【已解决】Ubun…

Qt自定义QML模块

自定义QML模块 含义为将常用风格的Button&#xff0c;Text,RadioButton,或者自定义的控件作为一个控件进行使用&#xff0c;节省代码。 优点&#xff1a; 代码简洁&#xff0c;减少重复代码自定义的控件进行封装重复使用可以与QML自带的库区别开来优化项目结构 一、创建模块…

如何查询资料

如何查询资料技术资料及问题查询查询方法分类查找提取关键字GitHub项目优先使用Google搜索引擎Copy Paste论文查找询问主管 测试修改使用总结分享 公司信息查询国内公司国外公司 如何查询资料 技术资料及问题查询 查询方法 资料与解决办法的查询大致分为7大类。 1.分类查…

Ubuntu 14.04 下 Virtual Judge 的搭建

前期准备工作 1.1 一个Linux系统 因为现场赛的缘故&#xff0c;我一直使用的都是ubuntu。 这里我测试用的是Ubuntu14.04 Desktop 64bit ,当然选择Server会更好一些. 系统的安装不再赘述&#xff0c;作为服务器请选用Server版本。1.2 更新源 在搭建环境之前&#xff0c;请确保…

QML Profiler性能优化教程

QML Profiler 2018年1月26日 vincent 对于一个程序的开发&#xff0c;性能优化是开发中的一个重要步骤。 我们肯定不希望开发出来的程序表现出卡顿&#xff0c;最好是处处流畅&#xff0c;丝滑般的体验。 对于C程序&#xff0c;我们有很多方法可以做性能优化&#xff0c;例如…