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\leq T\leq50)$, denoting the number of test cases. 
In each test case, there is an integer $n(2\leq n\leq 100000)$ in the first line, denoting the number of vertices of the graph. 
The following line contains $n-1$ integers $a_2,a_3,...,a_n(1\leq a_i\leq 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
 
 
 
题目意思:有n个点,这里给出了n-1个数(第0个点没有操作,所以不用)表示每个点的操作状态,操作1表示当前点与之前出现的所有的点连成一条边,操作2代表什么也不做,问最后是否每一个点都有一个点与其配对(两两配对)。
 
解题思路:英语水平确实太差了,上来看到graph,以为是图论,因为图论的内容没有学习,很打怵,不过榜单上和我水平差不多的队友有做出来的,就明白这不是一道难题,其实这应该算是一道找规律的题,我们很容易知道当n为奇数的时候是不可能出现两两匹配的。当n为偶数时,用count表示前面有多少个未配对的点,如果前面有未配对的点则,若操作为1,,则count--,若操作为2则count++。如果前面所有的点都匹对成功则,若操作为1,,则count=1(因为前面没有点与其配对),若操作为2则count++,最后如果count=0,则说明完美匹配perfect matching。
 
 
 #include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int t,n,i,count;
int a[];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
count=;
for(i=; i<n; i++)
{
scanf("%d",&a[i]);
}
if(n%==)
{
printf("No\n");///奇数不可能配对
}
else
{
for(i=; i<n; i++)
{
if(a[i]==)
{
if(count==)
{
count=;
}
else
{
count--;
}
}
else
{
count++;
}
}
if(count==)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
}
return ;
}
 看见有大佬写出了这样很简单的代码,我也学习一下:
 
 #include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int t,n,i,j,a,count;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
count=;
for(i=; i<n; i++)
{
scanf("%d",&a);
if(a==||count==)
{
count++;
}
else
{
count--;
}
}
if(count==)
{
printf("Yes\n");
}
else
{
printf("No\n");
} }
return ;
}
 思路本质上是一样的。。。。。。
 
 

Graph Theory的更多相关文章

  1. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  2. The Beginning of the Graph Theory

    The Beginning of the Graph Theory 是的,这不是一道题.最近数论刷的实在是太多了,我要开始我的图论与树的假期生活了. 祝愿我吧??!ShuraK...... poj18 ...

  3. Codeforces 1109D Sasha and Interesting Fact from Graph Theory (看题解) 组合数学

    Sasha and Interesting Fact from Graph Theory n 个 点形成 m 个有标号森林的方案数为 F(n, m) = m * n ^ {n - 1 - m} 然后就 ...

  4. CF1109D Sasha and Interesting Fact from Graph Theory

    CF1109D Sasha and Interesting Fact from Graph Theory 这个 \(D\) 题比赛切掉的人基本上是 \(C\) 题的 \(5,6\) 倍...果然数学计 ...

  5. HDU6029 Graph Theory 2017-05-07 19:04 40人阅读 评论(0) 收藏

    Graph Theory                                                                 Time Limit: 2000/1000 M ...

  6. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

    Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...

  7. 2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000 ...

  8. 2017中国大学生程序设计竞赛 - 女生专场(Graph Theory)

    Graph Theory Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  9. HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)

    6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...

随机推荐

  1. Redis Sentinel 介绍

    Redis Sentinel   sentinel的功能: 监控:sentinel节点定期检测redis数据节点,其余sentinel节点是否可达. 通知:sentinel 节点会将故障转移结果通知给 ...

  2. 文件后缀与Mime类型对照表

    以下是一些文件后缀(扩展名)对应的MIME类型的一个对照表,方便iis中或其他服务器对相应的文件进行解析.有些文件的后缀名没有默认解析就出现上传后无法访问或者下载的问题,这个时候就要设置文件后缀对应的 ...

  3. xp sp3安装.Net 4.0提示严重错误,0x80070643,解决办法2017版

    客户电脑上要装金税开票软件,需要.net 4.0.30319.1,电脑环境是xp sp3,已经安装了.net 2, .net 3.5sp1,安装.net 4.0的时候提示错误0x80070643 因为 ...

  4. yii学习笔记(3),自定义全局工具函数

    在平时开发是经常需要打印数据来调试 常见的打印方式有print_r和var_dump,但是这样打印出来格式很乱不好浏览 在打印函数前后加上<pre></pre>就可以将内容原样 ...

  5. Hive(4)-Hive的数据类型

    一. 基本数据类型 Hive数据类型 Java数据类型 长度 例子 TINYINT byte 1byte有符号整数 20 SMALINT short 2byte有符号整数 20 INT int 4by ...

  6. spark2.2 从入门到精通 视频教程 百度云网盘下载地址

    spark2.2 从入门到精通 视频教程 百度云网盘下载地址 链接:https://pan.baidu.com/s/1sm2Jdmt 密码:rdea

  7. java-IO处理类的序列化与反序列化

    package TestIo; import java.io.*; /** * 序列化 * * * 对象序列化 * * 一 创建对象 需要说明,想序列化的对象一定要是实现Serivalizable接口 ...

  8. CakePHP Model中( 获取Session)使用Component的方法

    有时候我们需要在Model中使用Session,大家知道CakePHP把操作Session的方法封装为了一个Component, 在Model中正常读取Session的方法: 在 "app_ ...

  9. oracle 建立一个视图,然后授权其他用户访问

    grant select on V_LIC_ENTRY_HZ_STATUS to ielicr2013; create or replace view dept_sum_vw (name,minsal ...

  10. 转 Cobar使用文档(可用作MySQL大型集群解决方案)

    转自:http://blog.csdn.net/shagoo/article/details/8191346 最近好不容易抽空研究了下Cobar,感觉这个产品确实很不错(在文档方面比Amoeba强多了 ...