hdu 6029 图论思维题 咋一看还以为是一般图匹配
2017-06-25 22:30
435 查看
题目:
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.
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.
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
==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;
}
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.
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.
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
==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;
}
相关文章推荐
- HDU 3551 Hard Problem (一般图匹配)
- hdu 4687 Boke and Tsukkomi(一般图匹配)
- HDU 4687 Boke and Tsukkomi 一般图匹配
- HDU 4687 Boke and Tsukkomi(一般图匹配+枚举)
- hdu 5636 &&bestcoder #74 1002 Shortest Path [floyd+松弛]【图论+思维】
- HDU 3279 2010ACM天津赛区现场赛 图论 最大匹配
- 理论: 图论(11): 二分图的最大权匹配(KM算法)HDU 2255
- 2013 Multi-University Training Contest 9(hdu 4686 - 4691)dp(好)+矩阵快速幂+一般图匹配带花树+后缀数组
- HDU-6105 Gameia - 2017 Multi-University Training Contest - Team 6(思维之找规律或二分图最大匹配)
- HDU 4687 Boke and Tsukkomi (一般图匹配带花树)
- HDU 2063(图论,二分匹配)
- HDU 4687 Boke and Tsukkomi 一般图匹配,带花树,思路,输出注意空行 难度:4
- [HDU 4687]Boke and Tsukkomi[一般图匹配][带花树]
- HDU 1083(图论,二分匹配)
- HDU 3446 daizhenyang's chess(一般图匹配)
- hdu 4687 带花树求一般图最大匹配(模板)
- HDU 1068(图论,二分匹配)
- HDU 4781 Assignment For Princess 图论,构造,思维
- hdu 6029 Graph Theory 思维
- hdu - 4687 Boke and Tsukkomi(一般图带花树匹配)