D - Graph Theory

D - Graph Theory
Time Limit:1000MS    Memory Limit:131072KB    64bit IO Format:%I64d & %I64u
use MathJax to parse formulas

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
 

题意:输入t组样例 每组样例先输入一个n 表示下面有n-1个数据 表示第2个到第n个点 每个数据只能是1或2 1代表可以和前面的任意一个点相连 2代表不可以和前面的相连 如果最终每两个点之间有且只有一条线 则输出yes 否则输出n:o
代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string.h>
using namespace std ;
int a [ 100005 ];
int main ()
{
   
int t , n , b , ans ;
    cin
>> t ;

   
while ( t --)
   
{
       
memset ( a , 0 , sizeof ( a ));     
  cin >> n ;
       
for ( int i = 0 ; i < n -1 ; i ++)
          
scanf ( "%d" ,& a [ i ]);
        ans
= 1 ; //ans赋值为1表示此时没有相连的只有第一个
       
for ( int i = 0 ; i < n -1 ; i ++)
       
{  
         if ( ans == 0 )
                ans
= 1 ; //新的这个没有连上
           
else
           
{
            
               
if ( a [ i ]== 1 )
                    ans
--; //和前面的一个相连
               
else
                    ans
++; //如果a[i]是2 表示没有连上的又多了一个
  
           
}
          

       
}
       
if ( ans == 0 )
           
printf ( "Yes \n " );
       
else
           
printf ( "No \n " );
   
}





   
return 0 ;
}
Graph Theory Authors: Adrian Bondy, U.S.R Murty Publisher: Springer; 3rd Corrected Printing edition (Aug 14 2008) ISBN-10: 1846289696 ISBN-13: 978-1846289699 = Product Description = The primary aim of this book is to present a coherent introduction to graph theory, suitable as a textbook for advanced undergraduate and beginning graduate students in mathematics and computer science. It provides a systematic treatment of the theory of graphs without sacrificing its intuitive and aesthetic appeal. Commonly used proof techniques are described and illustrated. The book also serves as an introduction to research in graph theory. = Table of Contents = Graphs - Subgraphs - Connected Graphs - Trees - Separable and Nonseparable Graphs - Tree-Search Algorithms - Flows in Networks - Complexity of Algorithms - Connectivity - Planar Graphs - The Four-Colour Problem - Stable Sets and Cliques - The Probabilistic Method - Vertex Colourings - Colourings of Maps - Matchings - Edge Colourings - Hamilton Cycles - Coverings and Packings in Directed Graphs - Electrical Networks - Integer Flows and Coverings - Unsolved Problems - References - Glossary - Index = Reviews = For more than three decades, the authors' Graph Theory with Applications (1976) has served as perhaps the classic introduction to graph theory. With happy shock, the reader learns that Bondy and Murty have updated the book, doubling its size. Three decades would count as a long time in the life of any active scientific pursuit, but the original year of publication saw the solution to the four-color conjecture that catalyzed a vast revitalization of graph theory. Graph theory, moreover, now has intimate interactions with computer science, practical and theoretical: three decades ago, computer networks barely existed and the founding papers of complexity theory had just appeared. Connections between graph theory and probability have also undergone a revolution. In short, the passage of time has transformed this subject in these and other ways, and the authors have transformed their book accordingly. They do, by choice, largely omit the theory of graph minors (developed by Paul Seymour and Neil Robertson and followers), which certainly now deserves a monographic treatment of its own. Summing up: Recommended. Lower-division undergraduate through professional collections. CHOICE This book is a follow-on to the authors' 1976 text, Graphs with Applications. What began as a revision has evolved into a modern, first-class, graduate-level textbook reflecting changes in the discipline over the past thirty years... This text hits the mark by appearing in Springer’s Graduate Texts in Mathematics series, as it is a very rigorous treatment, compactly presented, with an assumption of a very complete undergraduate preparation in all of the standard topics. While the book could ably serve as a reference for many of the most important topics in graph theory, it fulfills the promise of being an effective textbook. The plentiful exercises in each subsection are divided into two groups, with the second group deemed "more challenging". Any exercises necessary for a complete understanding of the text have also been marked as such. There is plenty here to keep a graduate student busy, and any student would learn much in tackling a selection of the exercises... Not only is the content of this book exceptional, so too is its production. The high quality of its manufacture, the crisp and detailed illustrations, and the uncluttered design complement the attention to the typography and layout. Even in simple black and white with line art, it is a beautiful book. SIAM Book Reviews "A text which is designed to be usable both for a basic graph theory course … but also to be usable as an introduction to research in graph theory, by including more advanced topics in each chapter. There are a large number of exercises in the book … . The text contains drawings of many standard interesting graphs, which are listed at the end." (David B. Penman, Zentralblatt MATH, Vol. 1134 (12), 2008) MathSciNet Reviews "The present volume is intended to serve as a text for "advanced undergraduate and beginning graduate students in mathematics and computer science" (p. viii). It is well suited for this purpose. The writing is fully accessible to the stated groups of students, and indeed is not merely readable but is engaging… Even a complete listing of the chapters does not fully convey the breadth of this book… For researchers in graph theory, this book offers features which parallel the first Bondy and Murty book: it provides well-chosen terminology and notation, a multitude of especially interesting graphs, and a substantial unsolved problems section…One-hundred unsolved problems are listed in Appendix A, a treasure trove of problems worthy of study… (In short) this rewrite of a classic in graph theory stands a good chance of becoming a classic itself." "The present volume is intended to serve as a text for ‘advanced undergraduate and beginning graduate students in mathematics and computer science’ … . The writing is fully accessible to the stated groups of students, and indeed is not merely readable but is engaging. The book has many exercise sets, each containing problems … ." (Arthur M. Hobbs, Mathematical Reviews, Issue 2009 C) "A couple of fantastic features: Proof techniques: I love these nutshelled essences highlighted in bordered frames. They look like pictures on the wall and grab the view of the reader. Exercises: Their style, depth and logic remind me of Lovász’ classical exercise book. Also the fact that the name of the author is bracketed after the exercise…Figures: Extremely precise and high-tech…The book contains very recent results and ideas. It is clearly an up-to-date collection of fundamental results of graph theory…All-in-all, it is a marvelous book." (János Barát, Acta Scientiarum Mathematicarum, Vol. 75, 2009)
GrTheory - Graph Theory Toolbox. 内含40个图论问题的matlab代码,包括最短径问题等。对数学建模,2012美赛ICM的帮助尤其大。欢迎下载。 Functions: grBase - find all bases of digraph; grCoBase - find all contrabases of digraph; grCoCycleBasis - find all independent cut-sets for a connected graph; grColEdge - solve the color problem for graph edges; grColVer - solve the color problem for graph vertexes; grComp - find all components of graph; grCycleBasis - find all independent cycles for a connected graph; grDecOrd - solve the problem about decomposition of the digraph to the sections with mutually accessed vertexes (strongly connected components); grDistances - find the distances between any vertexes of graph; grEccentricity - find the (weighted) eccentricity of all vertexes, radius, diameter, center vertexes and the periphery vertexes; grIsEulerian - find the Eulerian cycle of graph; grIsomorph - solve the problem about isomorphism for two graphs; grMaxComSu - solve the maximal complete sugraph problem for the graph; grMaxFlows - solve the maximal flow problem for the digraph; grMaxMatch - solve the maximal matching problem for the graph; grMaxStabSet - solve the maximal stable set problem for the graph; grMinAbsEdgeSet - solve the minimal absorbant set problem for the graph edges; grMinAbsVerSet - solve the minimal absorbant set problem for the graph vertexes; grMinCutSet - solve the minimal cut-set problem for the digraph; grMinEdgeCover - solve the minimal edge cover problem for the graph; grMinSpanTree - solve the minimal spanning tree problem for the graph; grMinVerCover - solve the minimal vertex cover problem for the graph; grPERT - solve the project evaluation research task; grPlot - draw the plot of the graph (digraph); grShortPath - solve the shortest path problem for the digraph; grShortVerPath - for digraph with weighted vertexes solve the problem about the path with minimal weight of verticies; grTranClos - built the transitive closure for the digraph; grTravSale - solve
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值