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. 第四模块MySQL50题作业,以及由作业引申出来的一些高端玩法

    一.表关系 先参照如下表结构创建7张表格,并创建相关约束                 班级表:class       学生表:student       cid caption grade_id ...

  2. Vmware文件类型

    ### vmx ###> 虚拟机启动的配置文件+ 包含`.encoding`.`displayName`.`memsize`等基本配置信息,还包括一些链接文件的位置如`nvram`(非易变RAM ...

  3. 日常工作之Zabbix源码编译,兼容mysql5.6

    原文链接:http://www.leleblog.top/daily/more?id=6 Zabbix源码编译 环境: centOS7.mysql5.6.21(已存在). 任务简述: 服务器搭建zab ...

  4. layui sleect获取value值

    <div class="layui-form-item"> <label for="username" class="layui-f ...

  5. 使用PHPExcel 对表格进行,读取和写入的操作。。。。

    下面的代码是使用PHPExcel 对多个表格数据进行读取, 然后整合的写入新的表格的方法!!!代码有点粗糙 , 多多保函!!! 这里有些地方注意下,如果你的表格数据过大, 一定要记得修改php.ini ...

  6. windows 10 安装node.js

    第一步:下载软件 nodejs的中文官网http://nodejs.cn/download/ 选择 windows  系统 msi 安装版本. 下载完成之后,直接打开下一步安装就可以. 安装完成 打开 ...

  7. 视频网站数据MapReduce清洗及Hive数据分析

    一.需求描述 利用MapReduce清洗视频网站的原数据,用Hive统计出各种TopN常规指标: 视频观看数 Top10 视频类别热度 Top10 视频观看数 Top20 所属类别包含这 Top20 ...

  8. Linux的数据传输

    1. sz 与 rz sz:将选定的文件从本地发送(send)到远端机器 rz:运行该命令会弹出一个文件选择窗口,从本地选择文件夹,接收(receive)从远端的文件 mac 下使用 brew 安装: ...

  9. Redis系列化方式有哪些?哪个系列化性能最好?

    Redis系列化方式有JDK系列化.JSON系列化.XML系列化等多种.我专门测试过,在我的笔记本电脑上保存5万条User对象到Redis,JDK系列化方式平均要15秒,JSON系列化方式只要13秒多 ...

  10. break和continue使用

    前面讲的循环,这里就是控制循环的东西 break其实在我们学习switch判断的时候就是用到了 break:代表跳出整个循环 continue和break的用法差不多 continue:代表只跳出当前 ...