欧拉路径

首先我们来介绍一下,什么是欧拉回路。
若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径。若该路径是一个圈,则称为欧拉(Euler)回路。
具有欧拉回路的图称为欧拉图(简称E图)。具有欧拉路径但不具有欧拉回路的图称为半欧拉图。

介绍一道例题:
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 wordmotorola’’. 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.

题目大意:
在每个门上有很多个有磁力的盘子,盘子上面写着单词。 必须重新移动放置这些盘子,让它们形成一个队列:队列中,除了第一个单词,每个单词的开头和上一个单词的结尾字母一样。例如, motorola的后面可以接上acm。
你的任务是写一个程序, 读入一系列单词,然后计算确定它们是否有可能被排成这样的队列。

输入:
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

输出:
The door cannot be opened.
Ordering is possible.
The door cannot be opened.

思路:
欧拉道路的定义是: 除了起点和终点外, 其他点的“进出” 次数应该相等。 换句话说,除了起点和终点外, 其他点的度数应该是偶数。
对于有向图, 则必须其中一个点的出度恰好比入度大1, 另一个的入度比出度大。
如果奇点数不存在的话, 则可以从任意点出发,最终一定会回到该点(成为欧拉回路)。
题目给的单词量比较大,但是有用的只有首和尾的字母,所以只需要存首尾字母就可以了。
还有个注意的地方是判断这一个图是连通的, 并且只有一个一个连通分支。
这个可以用并查集的方法, 也可一用dfs直接搜索。

AC代码:

#include<cstring>
#include<iostream>
#include<cstdio>
#include<cmath>
#define MAXN 30
using namespace std;
int vis[MAXN],N, T,f[MAXN],rank[MAXN], in[MAXN],out[MAXN];
 
void init()   //并查集判断是否是一个连通图  
{  
    for(int i=0;i<MAXN;i++)  
        f[i]=i,rank[i]=0;  
}  
 
int find(int x)  
{
    int r=x;  
    while(f[r]!=r)  
        r=f[r];  
    f[x]=r;  
    return r;      
}  
void Union(int x,int y)  
{  
    x = find(x);  
    y = find(y);  
    if(rank[x] > rank[y]){  
        f[y] = x;  
    }  
    else{  
        if(rank[x] == rank[y]) {  
            rank[y]++;  
        }  
        f[x] = y;  
    }   
}  
 
int main(){
 
    freopen("input.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        memset(in, 0, sizeof(in));
        memset(out, 0, sizeof(out));
 
        char str[1005];
        init();
        scanf("%d",&N);
        for(int i=0; i<N; ++i){
            scanf("%s", str);
            ++out[str[0]-'a'];
            ++in[str[strlen(str)-1]-'a'];       
            Union(str[0]-'a', str[strlen(str)-1]-'a');
        }
        
        int cnt=0;
        for(int i=0; i<27; ++i){
            if((in[i] || out[i]) && f[i]==i)
                ++cnt;
        }
            
        bool flag=true;
        if(cnt != 1) flag=false;
 
        int num1=0, num2=0;
        for(int i=0; i<MAXN; ++i){
            if(!flag) break;
            if(in[i]!=out[i]){
                if(in[i]==out[i]+1) { ++num1; }
                else if(out[i]==in[i]+1) { ++num2; }
                else {flag=false; break; }
            }
        }
        
        if(num1 && num2 && num1+num2>2) flag=false;
    
        if(flag){
            printf("Ordering is possible.\n");
        }
        else{
            printf("The door cannot be opened.\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值