UVa 10129 - Play on Words, 欧拉道路

FILE10129-Play on Words8881
24.43%
1751
68.65%
题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=1070


题目:

Some of the secret doors contain avery 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 alarge 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 asequence (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直接搜索。


下面给出两个版本的代码:


1. 欧拉道路 + 并查集

#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;
}


2. 欧拉道路 + dfs

#include<cstring>
#include<iostream>
#include<cstdio>
#include<cmath>
#define MAXN 100
using namespace std;
int vis[MAXN],G[MAXN][MAXN],N, T,f[MAXN],rank[MAXN], in[MAXN],out[MAXN];

void dfs(int u){
    vis[u] = true;
    for(int i=0; i<MAXN; ++i){
        if(G[u][i] && !vis[i])
            dfs(i);
    }
}

int main(){

    freopen("input.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        memset(G, 0, sizeof(G));
        memset(in, 0, sizeof(in));
        memset(out, 0, sizeof(out));

        char str[1005];

        scanf("%d",&N);
        
        for(int i=0; i<N; ++i){
            scanf("%s", str);
            ++G[str[0]-'a'][str[strlen(str)-1]-'a'];
            ++out[str[0]-'a'];
            ++in[str[strlen(str)-1]-'a'];
        }

        bool flag=true;

        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){
            memset(vis, 0, sizeof(vis));
            for(int i=0; i<MAXN; ++i) 
                if(out[i]) { dfs(i); break; }

            bool flag2=true;
            for(int i=0; i<MAXN; ++i){
                if(in[i] && !vis[i]) { flag2=false; break; }
                if(out[i] && !vis[i]) { flag2=false; break; }
            }
            if(flag2) printf("Ordering is possible.\n");
            else printf("The door cannot be opened.\n");
        }
        else{
            printf("The door cannot be opened.\n");
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值