HDU 6029(思维)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                HDU 6029(思维)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            傳送門題面:
Total Submission(s): 1220????Accepted Submission(s): 553
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 Input3 2 1 2 2 4 1 1 2
Sample OutputYes No No
Source 2017中國大學生程序設計競賽 - 女生專場
題目描述:一種所有結點都有邊與之相連的匹配叫做完美匹配。現在你有N個結點,對于n-1個結點,你有兩種操作:(1):將這個結點與之前的所有結點都連一條邊(2):不進行操作;問你組成的這張圖有沒有可能是完美匹配。題面分析:這是一道很有意思的思維題。首先要明確的一點是題目只要我們求是否可能是完美匹配,而不是讓我們判斷是否一定是完美匹配。對于每個結點,當我們要進行第一種操作時,即意味著我們這個結點可以與前面的任意一個結點進行匹配;而當我們進行第二種操作的時候,意味著這個結點是完全孤立的。因為每次操作1時,當前節點的匹配是任意的,考慮到這點,我們可以考慮使用隊列去做,即當進行操作1的時候,隊列深度減1,當操作為2時,將隊列深度加1,最后判斷隊列是否非空即可。而又考慮到這題中的結點對結果沒有影響,故直接開一個數進行加一減一的模擬即可。#include <bits/stdc++.h> using namespace std; int main() {int t;cin>>t;while(t--){int n,num;cin>>n;if(n%2==1){for(int i=1;i<n;i++){cin>>num;}puts("No");continue;}int que=1;for(int i=1;i<n;i++){cin>>num;if((num==1&&que==0)||num==2){que++;}else que--;}if(que==0){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): 1220????Accepted Submission(s): 553
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 Input3 2 1 2 2 4 1 1 2
Sample OutputYes No No
Source 2017中國大學生程序設計競賽 - 女生專場
題目描述:一種所有結點都有邊與之相連的匹配叫做完美匹配。現在你有N個結點,對于n-1個結點,你有兩種操作:(1):將這個結點與之前的所有結點都連一條邊(2):不進行操作;問你組成的這張圖有沒有可能是完美匹配。題面分析:這是一道很有意思的思維題。首先要明確的一點是題目只要我們求是否可能是完美匹配,而不是讓我們判斷是否一定是完美匹配。對于每個結點,當我們要進行第一種操作時,即意味著我們這個結點可以與前面的任意一個結點進行匹配;而當我們進行第二種操作的時候,意味著這個結點是完全孤立的。因為每次操作1時,當前節點的匹配是任意的,考慮到這點,我們可以考慮使用隊列去做,即當進行操作1的時候,隊列深度減1,當操作為2時,將隊列深度加1,最后判斷隊列是否非空即可。而又考慮到這題中的結點對結果沒有影響,故直接開一個數進行加一減一的模擬即可。#include <bits/stdc++.h> using namespace std; int main() {int t;cin>>t;while(t--){int n,num;cin>>n;if(n%2==1){for(int i=1;i<n;i++){cin>>num;}puts("No");continue;}int que=1;for(int i=1;i<n;i++){cin>>num;if((num==1&&que==0)||num==2){que++;}else que--;}if(que==0){puts("Yes");}else puts("No");}return 0; }
轉載于:https://www.cnblogs.com/Chen-Jr/p/11007311.html
總結
以上是生活随笔為你收集整理的HDU 6029(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 微信亲属卡支付失败是怎么回事?支付失败原
 - 下一篇: Jackson序列化和反序列化