Play on Words POJ - 1386 Euler路

来源:互联网 发布:搜狐2016出品的网络剧 编辑:程序博客网 时间:2024/06/05 15:32
就用这个题目来总结一下Euler回路吧

参考:欧拉图的判定和求法

Euler路分为两类,一类是Euler回路与Euler通路;

欧拉回路所有节点的出度等于入度,欧拉通路有且必有一个出度大于入度 1 和一个入度大于入度1的点;

搜索有无回路可用一个DFS做遍历(套圈法);


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
32acmibm3acmmalformmouse2okok
Sample Output
The door cannot be opened.Ordering is possible.The door cannot be opened.
#include <iostream>#include <cstring>#include <cstdio>#include <stack>using namespace std;int s[100][100];int v[50][50];void dfs(int u) {    for(int i=0; i<26; i++) {        if(v[u][i]==0&&s[u][i]>0) {            v[u][i]=1;            dfs(i);        }    }}int jiancha() {    int i,j;    for(i=0; i<26; i++)        for(j=0; j<26; j++)            if(v[i][j]==0&&s[i][j]>0) {                return 0;            }    return 1;}int main() {    int N,n;    cin>>N;    while(N--) {        memset(s,0,sizeof(s));        char ch[1000];        scanf("%d",&n);        for(int i=0; i<n; i++) {            scanf("%s",ch);            int l=strlen(ch);            s[ch[0]-'a'][ch[l-1]-'a']++;        }        int rudu=0,chudu=0,sum=0,k=0,i,j;        memset(v,0,sizeof(v));        for(i=0; i<26; i++) //无回路            if(!k) {                chudu=0;                rudu=0;                for(j=0; j<26; j++) {                    chudu=chudu+s[i][j];                    rudu=rudu+s[j][i];                }                if(chudu!=rudu)                    if(chudu-rudu==1||rudu-chudu==1) {                        if(chudu>rudu) {                            dfs(i);                            if(jiancha()==0)                                k=1;                        }                        sum++;                    } else      //出度与入度差大于1                        k=1;            }        if(sum!=2&&sum!=0)     //无回路奇数点个数为2 有回路奇数点为0            k=1;        if(sum==0&&!k) {            int y=0;                    //有回路            for(i=0; i<30; i++) {                for(j=0; j<30; j++) {                    if(s[i][j]>0) {                        y=1;                        break;                    }                }                if(y)break;            }            dfs(i);            if(jiancha()==0)                k=1;        }        if(k)            cout<<"The door cannot be opened."<<endl;        else            cout<<"Ordering is possible."<<endl;    }    return 0;}

0 0
原创粉丝点击