HDU 6029(思维)
题面:
Total Submission(s): 1220 Accepted Submission(s): 553
Graph Theory
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1220 Accepted Submission(s): 553
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.
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.
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
题目描述:一种所有结点都有边与之相连的匹配叫做完美匹配。现在你有N个结点,对于n-1个结点,你有两种操作:
(1):将这个结点与之前的所有结点都连一条边
(2):不进行操作;
问你组成的这张图有没有可能是完美匹配。
题面分析:这是一道很有意思的思维题。首先要明确的一点是题目只要我们求是否可能是完美匹配,而不是让我们判断是否一定是完美匹配。对于每个结点,当我们要进行第一种操作时,即意味着我们这个结点可以与前面的任意一个结点进行匹配;而当我们进行第二种操作的时候,意味着这个结点是完全孤立的。因为每次操作1时,当前节点的匹配是任意的,考虑到这点,我们可以考虑使用队列去做,即当进行操作1的时候,队列深度减1,当操作为2时,将队列深度加1,最后判断队列是否非空即可。
而又考虑到这题中的结点对结果没有影响,故直接开一个数进行加一减一的模拟即可。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
int n,num;
cin>>n;
if(n%2==1){
for(int i=1;i<n;i++){
cin>>num;
}
puts("No");
continue;
}
int que=1;
for(int i=1;i<n;i++){
cin>>num;
if((num==1&&que==0)||num==2){
que++;
}
else que--;
}
if(que==0){
puts("Yes");
}
else puts("No");
}
return 0;
}