hdu-6029 Graph Theory

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 i1

).
(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.
InputThe first line of the input contains an integer T(1T5, denoting the number of test cases.
In each test case, there is an integer n(2n100000) in the first line, denoting the number of vertices of the graph.
The following line contains n1 integers a2,a3,...,an(1ai2), denoting the decision on each vertice.OutputFor 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

OJ-ID:
hdu-6029
author:
Caution_X

date of submission:
20191017

tags:
思维

description modelling:
给定n个点,除第一个点外每一个点有两种操作:
操作1:选择一个比该点小的点匹配
操作2:该点之前的点不得与该点匹配
问:最终能否达到完美匹配

major steps to solve it:
(1).显然n为奇数不可能,当n为偶数时进入(2)
(2).从后往前遍历,保证操作1的个数始终大于操作2的个数即可

AC code:

#include<bits/stdc++.h>
using namespace std;
int a[100005];
int main()
{
    //freopen("input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--) {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n-1;i++) {
            scanf("%d",&a[i]);
        }
        if(n%2!=0) {
            printf("No\n");
            continue;
        }
        int p=0;
        bool f=false;
        for(int i=n-2;i>=0;i--) {
            if(a[i]==1)    p++;
            else p--;
            if(p<0) {
                f=true;
                break;
            }
        }
        if(!f)    printf("Yes\n");
        else printf("No\n");
    }
    return 0; 
}

猜你喜欢

转载自www.cnblogs.com/cautx/p/11716509.html