题目:
Graph Theory
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 448 Accepted Submission(s): 216
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
代码:
细节考虑不周WA了好几发....又一题背锅
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int maxn = 1e5+50;
int t,n,d[maxn];
int ans;
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d",&n);
d[1]=2;
ans=0;
for(int i=2;i<=n;++i){
scanf("%d",&d[i]);
if(d[i]==1) ans++;
}
if(n%2==1 || d[n]==2){
puts("No");
continue;
}
/* if(ans*2<n){
puts("No");
continue;
}*/
//cout<<"???"<<endl;
int a=0,b=0;
int f=1;
for(int i=n;i>=1;--i){
if(d[i]==1) a++;
if(d[i]==2) b++;
if(b>a) {
f=0;
break;
}
}
if(f) puts("Yes");
else puts("No");
/* int f;
if(ans*2==n){
f=0;
for(int i=1;i<=n/2;++i){
if(d[i]==1){
f=1;
break;
}
}
if(f==0){
puts("No");
continue;
}
}
//int j=-1,k=-1;
//int cnt=0,tmp;
f=1;
for(int i=1;i<=n;++i){
if(d[i]==1){
if(i+1<=n&&d[i+1]==2&&i+2<=n&&d[i+2]==2){
f=0;
break;
}
}
}
if(f){
puts("Yes");
}
else puts("No");*/
}
return 0;
}