10129 Play on Words-----並查集+尤拉路!!!
題意:判斷所有字串是不是可以連在一起,實際上就是判斷所構造的有向圖是否存在歐拉回路或者尤拉圖;
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<string> #include <algorithm> #define N 100010 using namespace std; char c[N][2]; int al[123]; int in[27],out[27]; int f[27],ccount; bool yo[26]; int tou(int x) { if(f[x]!=x) { f[x]=tou(f[x]); } return f[x]; } void build(int x,int y) { int toux=tou(x),touy=tou(y); if(touy!=toux) { ++ccount; f[toux]=touy; } } int main() { int t,n; scanf("%d",&t); while(t--) { memset(al,0,sizeof(al)); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(yo,0,sizeof(yo)); memset(f,0,sizeof(f)); scanf("%d%*c",&n); int geshu=0; for(int i=0; i<n; i++) { char cc,ccc; scanf("%c",&c[i][0]); if(!yo[c[i][0]-'a']) { yo[c[i][0]-'a']=1; ++geshu; } while(scanf("%c",&cc)&&cc!='\n') { ccc=cc; } c[i][1]=ccc; if(!yo[c[i][1]-'a']) { yo[c[i][1]-'a']=1; ++geshu; } in[c[i][1]-'a']++; out[c[i][0]-'a']++; } for(int i=0; i<26; i++) f[i]=i; ccount=0; for(int i=0; i<n; i++) build(c[i][0]-'a',c[i][1]-'a'); int count=0,flag=1; if(ccount+1!=geshu) flag=0; if(flag) for(int i=0; i<26; i++) { if(in[i]!=out[i]) { count++; if(abs(in[i]-out[i])!=1||count>2) { flag=0; break; } } } if(!flag) printf("The door cannot be opened.\n"); else printf("Ordering is possible.\n"); } return 0; }
相關推薦
10129 Play on Words-----並查集+尤拉路!!!
題意:判斷所有字串是不是可以連在一起,實際上就是判斷所構造的有向圖是否存在歐拉回路或者尤拉圖; #include<iostream> #include<cstdio> #include<cmath> #include<cstri
HDU-1116-Play on Words (並查集 +歐拉回路)
原題連結: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Some of the secret doors contain a very interesting word puzzle. The team of archaeologists
HDU 1116.Play on Words【並查集+尤拉圖】【5月16】
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7111 Accepte
poj1386 Play on Words有向圖尤拉通路判定
Description Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because
Play on words 之並查集+歐拉回路解題報告
Play on Words Time Limit: 10000/5000 MS(Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3754 Accepted S
uva 10129 Play On Words(單詞)求歐拉回路或尤拉通路
題目:輸入n個單詞,是否可以把這些所有單詞排成一個序列,使得每個單詞的第一個字母和上一個單詞的最後一個字母想同。 有向圖歐拉回路:1,圖連通.2,所有定點入度等於出度 有向圖尤拉通路:1,圖連通.2,僅有兩個奇度定點,其中一個入度比出度大1,另一個定點出度比入度大1 思路
UVA 10129 Play on Words
bool seq lis rest ble u+ contains con sync Some of the secret doors contain a very interesting word puzzle. The team of archaeologists ha
樹上統計treecnt(dsu on tree 並查集 正難則反)
problem freopen space script type 題目 每次 分割 pos 題目鏈接 \(Description\) 給定一棵\(n(n\leq 10^5)\)個點的樹。 定義\(Tree[L,R]\)表示為了使得\(L\sim R\)號點兩兩連通,最少需
UVa-10129 Play on Words
1 #include <bits/stdc++.h> 2 3 const int maxn = 28; 4 using namespace std; 5 6 int G[maxn][maxn]; 7 int tmpG[maxn][maxn];
POJ 1386 Play on Words(有向歐拉通路 連通圖)
tput mouse char s tdi 任務 input using 否則 表示 題意 見下方中文翻譯 每一個單詞能夠看成首尾兩個字母相連的一條邊 然後就是輸入m條邊 推斷是否能構成有向歐拉通路了 有向圖存在歐拉通路的充要條件: 1. 有向圖的基圖連通;
hdu1272小希的迷宮(並查集判斷回路和是否連通)
show set onclick spa 分享 return print 聯通 兩個 傳送門 迷宮中不能有回路,還要連通 如果最後集合數是一個那就是連通,否則不聯通 要合並的兩個頂點在相同集合內,表示出現了回路 輸入時註意一下 1 #include<
杭電Problem1878 歐拉回路 並查集 + 歐拉回路
問題地址 歐拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11814 Accepted Su
HDU:1878 歐拉回路(並查集+歐拉回路)
1 0 題義就是在給定的圖中判定是否存在歐拉回路。 圖G的一個迴路,若它恰通過G中每條邊一次,則稱該回路為尤拉(Euler)迴路。 具有歐拉回路的圖稱為尤拉圖(簡稱E圖)。 這裡總結下各種圖的判定的方法: 1.無向圖中:所給定的圖為連通圖,且所有節點的度為偶數; 2.有向圖中:所給定的圖
並查集(路徑壓縮 && 啟發式合併!!!)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> usin
【CF878E】Numbers on the blackboard 並查集
是什麽 first pan const gpo 最終 efi 是否 strong 【CF878E】Numbers on the blackboard 題意:給你一個長度為n個數列,你每次可以進行如下操作: 選取兩個相鄰的數x,y(x在y左面),然後將這兩個數去掉,用x+
Play on Words UVA-10129
問題描述 Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors
CCF 習題 201512-4 送貨 (並查集 + DFS 找尤拉道路)
大體題意: 要求從1號出發,一筆畫經過所有的路,問是否有解,並列印字典序最小的解? 思路: 顯然是無向圖的尤拉道路! 先判連通,直接用並查集了,不連通直接-1了 連通的話,在看看每個點的度數,當奇點的個數不是0 並且不是2 肯定是-1 如果是2 的話,1號結點是偶數度
尤拉路徑,歐拉回路,並查集
1.1 定義 對於圖G,若存在一條路徑,經過G中每條邊有且僅有一次,稱這條路為尤拉路徑;如果存在一條迴路經過G每條邊有且僅有一次,稱這條迴路為歐拉回路。具有歐拉回路的圖成為尤拉圖。 1.2 判斷尤拉路徑是否存在的方法 有向圖:圖連通,且只有一個頂點出度大入度1,有一
Play on Words(尤拉通路)
【題意】 輸入n(n<=100000)個單詞,是否可以把所有這些單詞排成一個序列,使得每個單詞的第一個字母和上一個單詞的最後一個字母相同(如acm,malform,mouse)每個單詞最多包含1000個字母,輸入中可以有重複單詞。
杭電ACM1116——Play on Words~~尤拉路徑與歐拉回路
這一題,相比之前做的題目,增加了尤拉路徑的求解。而且這一題是有向圖。題目大概的意思就是成語接龍,能接起來就算可以開啟門,因此要考慮兩種,一種是迴路,另外一種是一條路徑。 第一次WR就是因為沒有考慮迴路這一個因素。 有向圖中,歐拉回路與尤拉路徑的求解方法: 1.歐拉回路: 首