2454 Degree Sequence of Graph G

题目讲述了王海阳遇到的一个图论问题,他将港口和航线转化为图论中的节点和边,从而形成一个图。问题核心是判断给定的度数序列是否能构成一个没有平行边和环的简单图。根据Havel定理,可以通过排序和递归检查度数序列来确定其是否可简单图化。题目提供了样例输入和输出,并推荐了几个学习编程的资源。
摘要由CSDN通过智能技术生成

题目详情:

Degree Sequence of Graph G

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4540    Accepted Submission(s): 2003

Problem Description
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 Knoigsberg, 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?
 

Input

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.
 
 
Output
For each case, the answer should be "yes"or "no" indicating this case is "draw-possible" or "non-draw-possible"
 
 
Sample Input
2
6 4 4 3 3 2 2
4 2 1 1 1
 
Sample Output
yes
no
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2448 2452 2451 2453 2455 

题目大意:

给定度数,判断是否可以构成图

解题思路:

Havel定理:

给定一个非负整数序列{dn},若存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化。进一步,若图为简单图,则称此序列可简单图化

可图化的判定:d1+d2+……dn=0(mod 2)。关于具体图的构造,我们可以简单地把奇数度的点配对,剩下的全部搞成自环。

可简单图化的判定(Havel定理):把序列排成不增序,即d1≥d2≥……≥dn,则d可简单图化当且仅当d’={d2-1,d3-1,……d(d1+1)-1, d(d1+2),d(d1+3),……dn}可简单图化。

简单的说,把d排序后,找出度最大的点(设度为d1),把它与度次大的d1个点之间连边,然后这个点就可以不管了,一直继续这个过程,直到建出完整的图,或出现负度等明显不合理的情况。

AC代码:

#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
    return a>b;
}
int main(){
    int t;
    cin>>t;
    int x;
    vector<int> n;
    for(int i=0;i<t;++i){//t个测试样例
        int sign=0;//标记
        cin>>x;//存放到数组
        n.push_back(x);
        vector<int> degree;//存放度数
        for(int j=0;j<n[i];++j){
            cin>>x;
            degree.push_back(x);
        }
        if(degree.size()==1){//个数为1单独处理
            if(degree[0]<0)  cout<<"no"<<endl;
            else cout<<"yes"<<endl;
            break;
        }
        while(degree.size()){//个数大于1
            if(sign!=0) break;
            sort(degree.begin(),degree.end(),cmp);
            int head=degree[0];
            degree.erase(degree.begin());
            for(int j=0;j<head;++j){
                --degree[j];
            }
            for(int j=0;j<degree.size();++j){//查找是否有小于0的元素
                if(degree[j]<0){
                    cout<<"no"<<endl;
                    ++sign;
                    break;
                }
            }
        }
        if(sign==0) cout<<"yes"<<endl;
    }
    return 0;
}

 推荐几款学习编程的网站

免费在线开发平台(LTPP在线开发平台 | LTPP宇宙文档

        探索编程世界的新天地,为学生和开发者精心打造的编程平台,现已盛大开启!这个平台汇集了近4000道精心设计的编程题目,覆盖了C、C++、JavaScript、TypeScript、Go、Rust、PHP、Java、Ruby、Python3以及C#等众多编程语言,为您的编程学习之旅提供了一个全面而丰富的实践环境。

        在这里,您不仅可以查看自己的代码记录,还能轻松地在云端保存和运行代码,让编程变得更加便捷。平台还提供了私聊和群聊功能,让您可以与同行们无障碍交流,分享文件,共同进步。不仅如此,您还可以通过阅读文章、参与问答板块和在线商店,进一步拓展您的知识边界。

        为了提升您的编程技能,平台还设有每日一题、精选题单以及激动人心的编程竞赛,这些都是备考编程考试的绝佳资源。更令人兴奋的是,您还可以自定义系统UI,选择视频或图片作为背景,打造一个完全个性化的编码环境,让您的编程之旅既有趣又充满挑战。

免费公益服务器(LTPP公益LINUX服务器分享 | LTPP宇宙文档

        作为开发者或学生,您是否为搭建和维护编程环境而困扰?现在,有一款免费的公共服务器,内置多种编程语言的编程环境,并且配备了在线版VS Code。让您可以随时随地在线写代码,无需复杂配置,专注于开发和学习。(PS:毕竟是免费公共的服务器,任何人都能够使用,为了数据隐私和安全,请勿上传重要数据,仅用于学习)

免费公益MYSQL(LTPP公益MYSQL分享 | LTPP宇宙文档

        作为一名开发者或学生,您是否常常为数据库环境的搭建而烦恼?是否因为预算有限而无法使用高性能的数据库服务?现在,有一款免费的MySQL服务器,专为开发者和学生量身打造,让你轻松无忧地进行开发和学习!内置在线phpmyadmin管理面板,方便用户查看数据。(PS:毕竟是免费公共的MYSQL,任何人都能够使用,为了数据隐私和安全,请勿上传重要数据,仅用于学习)

免费在线WEB代码编辑器(LTPP在线WEB编辑器 | LTPP宇宙文档

        无论你是开发者还是学生,编程环境的搭建和管理可能会占用你宝贵的时间和精力。现在,有一款强大的免费在线代码编辑器,支持多种编程语言,让您可以随时随地编写、调试和运行代码,提升编程效率,专注于创意和开发。

免费二维码生成器(LTPP二维码生成器 | LTPP宇宙文档

        无论是企业宣传、活动推广,还是个人信息分享,二维码都是一种快速、高效的信息传递方式。现在,有一款功能强大的二维码生成器,不仅易于使用,还具备多种便捷功能,帮助您更轻松地生成和管理二维码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WA-自动机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值