UVa 10129:Play on Words, 欧拉道路

题目链接:

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

题目类型: 欧拉道路

题目:

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.

题目大意翻译:

有一些秘密的门包含着非常有趣的单词迷题, 考古学家队伍必须解决它们才能够打开大门。 因为没有其他方法能偶打开这些门, 所以解决那些迷题对我们非常重要。

在每个门上有很多个有磁力的盘子,盘子上面写着单词。 必须重新移动放置这些盘子,让它们形成一个队列:队列中,除了第一个单词,每个单词的开头和上一个单词的结尾字母

一样。例如, 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;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索word
, acm
, acm问题
, 队列
, 单词
, 题目
, 1005acm杭电
, 一个
The
sensuva on好用吗、sensuva on、欧拉道路、on catchwords、play on words,以便于您获取更多的相关知识。

时间: 2024-09-03 17:27:31

UVa 10129:Play on Words, 欧拉道路的相关文章

uva 10129 - Play on Words

点击打开链接 题目意思:给定n个单词,要求单词的第一个字母和前一个单词的最后字母相同(除了第一个),判断所有单词是否可以连成一条链. 解题思路:题目很明显是一个有关欧拉路的问题,我们先了解一下有关欧拉道路和欧拉回路的区别.欧拉道路是分为有向和无向两种,对于欧拉道路上的节点有两种度,入度和出度,入度表示进入该节点次数,出度表示出去该节点的次数,对于一条普通的有向欧拉道路来说存在一个起点和终点,并且起点的入度比出度小1,终点的入度比出度大1,下面介绍特殊的欧拉道路-----欧拉回路,欧拉回路是起点和

UVa 11417 GCD (欧拉φ函数)

11417 - GCD Time limit: 2.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2412 Given the value of N, you will have to find the value of G. The definition of G is given

图形化编程实现改进的欧拉格式和龙格库塔格式。这里有个C语言的,想改写成C#。

问题描述 图形化编程实现改进的欧拉格式和龙格库塔格式.这里有个C语言的,想改写成C#. 1)改进欧拉法求解常微分方程的初值问题 #include float func(float x,float y) { return(y-x); } float euler(float x0,float xn,float y0,int N) { float x,y,yp,yc,h; int i; x=x0; y=y0; h=(xn-x0)/(float)N; for(i=1;i<=N;i++) { yp=y+h

HDU 1411 校庆神秘建筑(欧拉四面体公式)

HDU 1411:http://acm.hdu.edu.cn/showproblem.php?pid=1411 大意:人一个你一个六面体的六条边,求六面体的体积. 思路:没有什么思路,就是用欧拉四面体公式直接代入. 欧拉四面体公式: 具体的推导网上有很多.eg.  http://blog.csdn.net/archibaldyangfan/article/details/8035332 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Pro

HDU 1286 找新朋友(欧拉函数模板)

HDU 1286 找新朋友:http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文题. 思路:欧拉函数的纯模板题,没什么好说的,主要是理解欧拉函数的意义. 在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等. 例如φ(8)=4,因为1,3,5,7均和8互质.   ----by度娘. 更多精彩内容:http://www.bia

HDU 3501 Calculation 2:欧拉函数的应用

HDU 3501 Calculation 2:http://acm.hdu.edu.cn/showproblem.php?pid=3501 大意:求1~n之间与n不互质的数的总和. 思路:欧拉函数的应用:先用欧拉函数求出与n互质的总数m,计算m个数的总和,用n的总和减去m的总和就是想要的结果. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ #include <stdio.h> #define LL _

欧拉函数性质

先来介绍几个与欧拉函数有关的定理: 定理一:设m与n是互素的正整数,那么 定理二:当n为奇数时,有. 因为2n是偶数,偶数与偶数一定不互素,所以只考虑2n与小于它的奇数互素的情况,则恰好就等于n的欧拉函数值. 定理三:设p是素数,a是一个正整数,那么 关于这个定理的证明用到容斥: 由于表示小于与互素数的正整数个数,所以用减去与它不互素的数的个数就行了. 那么小于与不互素数的个数就是p的倍数个数,有个.所以定理得证. 定理四:设为正整数n的素数幂分解,那么 这个定理可以根据定理一和定理三证明,其实

数论之 素因子分解,素数筛选法,欧拉函数和扩展欧几里得算法 (整理)

今天突然想复习一下数论,也就顺便整理了一下关于数论的基础知识, 以后用的时候直接用就行了,也不用现敲了,其实就是有点懒.... 具体解释都在代码里有解释 直接上代码了: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue>

如何用线性筛法求欧拉函数

前几天做了一个关于欧拉函数的题,当时就做超时了,因为我是暴力做的,后来百度了一下 线性晒法求欧拉函数,所以今天就打算系统的看一下筛法求欧拉函数的问题,该算法在可在线性时间内筛素数的同时求出所有数的欧拉函数: 先介绍一下暴力的欧拉函数: Eular(m) = m - (1-1/p1) - (1-1/p2) - ... - (1-1/pk)  [其中 p1, p2...pk为m的素因子] int Eular(int m) { int ret = m; for(int i=2; i<m; i++) {