【HDU - 1116】【POJ - 1386】Play on Words(判断半欧拉图,欧拉通路)
題干:
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.?
There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.?
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.?
Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.?
If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".?
Sample Input
3 2 acm ibm 3 acm malform mouse 2 ok okSample Output
The door cannot be opened. Ordering is possible. The door cannot be opened.題目大意:
? 給n個字符串,問你能否通過首尾相連來把所有串串成一串。n=1e5
解題報告:
別忘判連通!
?半歐拉圖 :具有歐拉通路而無歐拉回路的圖
?歐拉通路:從圖的某一個頂點(diǎn)出發(fā),圖中每條邊走且僅走一次,最后到達(dá)某一個點(diǎn);如果這樣的路徑存在,則稱之為歐拉路徑。
?歐拉回路:從圖的某一個頂點(diǎn)出發(fā),圖中每條邊走且僅走一次,最后回到出發(fā)點(diǎn);如果這樣的回路存在,則稱之為歐拉回路。
?無向圖歐拉通路存在條件:至多有兩個頂點(diǎn)的度數(shù)為奇數(shù),其他頂點(diǎn)的度數(shù)均為偶數(shù)。
?有向圖歐拉通路存在條件:至多有兩個頂點(diǎn)的入度和出度絕對值差1(若有兩個這樣的頂點(diǎn),則必須其中一個出度大于入度,另一個入度大于出度),其他頂點(diǎn)的入度與出度相等。
?無向圖歐拉回路存在條件:所有頂點(diǎn)的度數(shù)均為偶數(shù)并且連通。
?有向圖歐拉回路存在條件:所有頂點(diǎn)的入度和出度相等并且連通。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; char s[MAX]; int n; int f[MAX],in[MAX],out[MAX]; int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]); } void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);f[t2] = t1; } int main() {int t;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=127; i++) f[i] = i,in[i]=out[i]=0;for(int i = 1; i<=n; i++) {scanf("%s",s+1);char st = s[1];char ed = s[strlen(s+1)];merge(st,ed);in[ed]++;out[st]++; }int flag = 1,ru=0,chu=0,ans=0;for(int i = 'a'; i<='z'; i++) {if(!in[i] && !out[i]) continue;if(f[i] == i) ans++;if(in[i] == out[i]) continue;else if(in[i] - out[i] == 1) ru++;else if(out[i] - in[i] == 1) chu++;else {flag = 0;break;}}if(ans>1||flag == 0 || ru>1 || chu>1 || ru!=chu) puts("The door cannot be opened.");else puts("Ordering is possible.");} return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【HDU - 1116】【POJ - 1386】Play on Words(判断半欧拉图,欧拉通路)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: whagent.exe - whagen
- 下一篇: 玩家主动为《赛博朋克2077》开发Mod