(HDU 6029 女生专场)Graph Theory 思维题
来源:互联网 发布:python 获取网页图片 编辑:程序博客网 时间:2024/06/06 01:00
Graph Theory
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 346 Accepted Submission(s): 167
Problem Description
Little Q loves playing with different kinds of graphs very much. One day he thought about an interesting category of graphs called “Cool Graph”, which are generated in the following way:
Let the set of vertices be {1, 2, 3, …, n}. You have to consider every vertice from left to right (i.e. from vertice 2 to n). At vertice i, you must make one of the following two decisions:
(1) Add edges between this vertex and all the previous vertices (i.e. from vertex 1 to i−1).
(2) Not add any edge between this vertex and any of the previous vertices.
In the mathematical discipline of graph theory, a matching in a graph is a set of edges without common vertices. A perfect matching is a matching that each vertice is covered by an edge in the set.
Now Little Q is interested in checking whether a ”Cool Graph” has perfect matching. Please write a program to help him.
Input
The first line of the input contains an integer T(1≤T≤50), denoting the number of test cases.
In each test case, there is an integer n(2≤n≤100000) in the first line, denoting the number of vertices of the graph.
The following line contains n−1 integers a2,a3,…,an(1≤ai≤2), denoting the decision on each vertice.
Output
For each test case, output a string in the first line. If the graph has perfect matching, output ”Yes”, otherwise output ”No”.
Sample Input
3
2
1
2
2
4
1 1 2
Sample Output
Yes
No
No
Source
2017中国大学生程序设计竞赛 - 女生专场
题意:
给你一个有n个节点的图,编号为1,2,..,n。图的边的信息有两种情况:
若节点i的信息是1:表示i和1~i-1的所有的节点都有一条边
若节点i的信息是2:表示i和1~i-1的所有的节点都没有
一个匹配:在一个图的边的集合中没有相同的节点
一个完美匹配:所有的节点都在这个集合中
现在问你,这个图是否是完美匹配的
分析:
由于一个节点,要么和前的节点都有边,要么都没有,所以对于所有后面为1的节点,表示它可以和前面的任意节点进行匹配,而后面为2的节点,要想完美匹配它一定会和它后面为1的节点进行匹配。
相当于后面为1的节点,给你所有节点一个匹配的机会,而后面为2的节点,要占用一次机会。
所有我们用sum记录机会的次数,当遇到1时sum++遇到2时sum–.只要满足在这个过程中sum>=0 ,并且最后sum >=1 (第一个节点为2)即可。
注意:n为奇数时一定不行
由于我判断n为奇数后就直接出了结果,没有进行读取数据了导致wa了几次。。。
AC代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;const int maxn = 100010;int a[maxn];int main(){ int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<n;i++) { scanf("%d",&a[i]); if(a[i] == 2) a[i] = -1; } if((n%2) == 1) { printf("No\n"); continue; } int sum = 0; int f = 1; for(int i=n-1;i>=1;i--) { sum += a[i]; if(sum < 0) { f = 0; break; } } if(f == 0 || sum < 1) printf("No\n"); else printf("Yes\n"); } return 0;}
- (HDU 6029 女生专场)Graph Theory 思维题
- hdu 6029 Graph Theory 思维
- HDU 6029 Graph Theory(思路题)
- 2017CCPC女生赛 hdu 6029 Graph Theory
- HDU 6029 Graph Theory
- HDU 6029 Graph Theory
- HDU 6029 Graph Theory
- HDU 6029 Graph Theory
- hdu 6029 Graph Theory
- HDU 6029 Graph Theory
- HDU 6029 Graph Theory【水题】
- hdu 6029 Graph Theory(贪心)
- 图论-思维题-hdu6029-Graph Theory
- hdu 6029 Graph Theory 【直接连线】
- Graph Theory HDU
- 2017女生赛 1007 Graph Theory【】
- hdu6029 Graph Theory (2017女生赛)
- "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现) HDU 5702
- QIODevice类
- java.lang.UnsatisfiedLinkError: Couldn't load XXX
- iOS AES加密解密的使用
- 斜率优化 BZOJ 1010 HNOI 2008玩具装箱 详细解析。
- Java设计模式-单例设计模式(饿汉、饱汉)详细解析
- (HDU 6029 女生专场)Graph Theory 思维题
- 史上最全设计模式导学目录(完整版)
- php+windows扩展curl问题
- 有限元法知识点概述
- Python: sklearn库中数据预处理函数fit_transform()和transform()的区别
- Html5入门
- 高级系统架构师培训公开课举行
- mysql 免安装版配置
- AIX+oracle+rac+asm