日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 6029(思维)

發(fā)布時(shí)間:2023/12/13 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 6029(思维) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

傳送門題面:

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(1T50), denoting the number of test cases.
In each test case, there is an integer n(2n100000) in the first line, denoting the number of vertices of the graph.
The following line contains n?1 integers a2,a3,...,an(1ai2), 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中國大學(xué)生程序設(shè)計(jì)競賽 - 女生專場

題目描述:一種所有結(jié)點(diǎn)都有邊與之相連的匹配叫做完美匹配?,F(xiàn)在你有N個(gè)結(jié)點(diǎn),對(duì)于n-1個(gè)結(jié)點(diǎn),你有兩種操作:

(1):將這個(gè)結(jié)點(diǎn)與之前的所有結(jié)點(diǎn)都連一條邊(2):不進(jìn)行操作;

問你組成的這張圖有沒有可能是完美匹配。題面分析:這是一道很有意思的思維題。首先要明確的一點(diǎn)是題目只要我們求是否可能是完美匹配,而不是讓我們判斷是否一定是完美匹配。對(duì)于每個(gè)結(jié)點(diǎn),當(dāng)我們要進(jìn)行第一種操作時(shí),即意味著我們這個(gè)結(jié)點(diǎn)可以與前面的任意一個(gè)結(jié)點(diǎn)進(jìn)行匹配;而當(dāng)我們進(jìn)行第二種操作的時(shí)候,意味著這個(gè)結(jié)點(diǎn)是完全孤立的。因?yàn)槊看尾僮?時(shí),當(dāng)前節(jié)點(diǎn)的匹配是任意的,考慮到這點(diǎn),我們可以考慮使用隊(duì)列去做,即當(dāng)進(jìn)行操作1的時(shí)候,隊(duì)列深度減1,當(dāng)操作為2時(shí),將隊(duì)列深度加1,最后判斷隊(duì)列是否非空即可。

而又考慮到這題中的結(jié)點(diǎn)對(duì)結(jié)果沒有影響,故直接開一個(gè)數(shù)進(jìn)行加一減一的模擬即可。#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; }

轉(zhuǎn)載于:https://www.cnblogs.com/Chen-Jr/p/11007311.html

總結(jié)

以上是生活随笔為你收集整理的HDU 6029(思维)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。