zoukankan      html  css  js  c++  java
  • Graph Theory

    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(1leq Tleq50)$, denoting the number of test cases. 
    In each test case, there is an integer $n(2leq nleq 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(1leq a_ileq 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。
     
     
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 using namespace std;
     5 int main()
     6 {
     7     int t,n,i,count;
     8     int a[100010];
     9     scanf("%d",&t);
    10     while(t--)
    11     {
    12         scanf("%d",&n);
    13         count=1;
    14         for(i=1; i<n; i++)
    15         {
    16             scanf("%d",&a[i]);
    17         }
    18         if(n%2==1)
    19         {
    20             printf("No
    ");///奇数不可能配对
    21         }
    22         else
    23         {
    24             for(i=1; i<n; i++)
    25             {
    26                 if(a[i]==1)
    27                 {
    28                     if(count==0)
    29                     {
    30                         count=1;
    31                     }
    32                     else
    33                     {
    34                         count--;
    35                     }
    36                 }
    37                 else
    38                 {
    39                     count++;
    40                 }
    41             }
    42             if(count==0)
    43             {
    44                 printf("Yes
    ");
    45             }
    46             else
    47             {
    48                 printf("No
    ");
    49             }
    50         }
    51     }
    52     return 0;
    53 }
     看见有大佬写出了这样很简单的代码,我也学习一下:
     
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 using namespace std;
     5 int main()
     6 {
     7     int t,n,i,j,a,count;
     8     scanf("%d",&t);
     9     while(t--)
    10     {
    11         scanf("%d",&n);
    12         count=1;
    13         for(i=1; i<n; i++)
    14         {
    15             scanf("%d",&a);
    16             if(a==2||count==0)
    17             {
    18                 count++;
    19             }
    20             else
    21             {
    22                 count--;
    23             }
    24         }
    25         if(count==0)
    26         {
    27             printf("Yes
    ");
    28         }
    29         else
    30         {
    31             printf("No
    ");
    32         }
    33 
    34     }
    35     return 0;
    36 }
     思路本质上是一样的。。。。。。
     
     
  • 相关阅读:
    Linear Regression Example
    三角函数画图(Python)
    机器学习算法笔记系列之深入理解主成分分析PCA-原理篇
    Boosted Trees 介绍
    Jacobian矩阵和Hessian矩阵
    使用插件pagehelper在mybatis中实现分页查询
    git常用操作
    Python远程视频监控
    FPGA选型
    英文Datasheet没那么难读
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9037236.html
Copyright © 2011-2022 走看看