POJ--1386--Play on Words【判断有向图欧拉通路、欧拉回路】

链接:http://poj.org/problem?id=1386

题意:要开启一扇门,n个单词是密码,n个单词中,如果一个单词的首字母和前一个单词的尾字母相同,并且每个单词都能这么连起来且只用一次,则门可以开启,否则不能开启,现给出单词,判断门是否可以开。

有向图欧拉通路充要条件:D为有向图,D的基图连通,并且所有顶点的出度与入度都相等;或者除两个顶点外,其余顶点的出度与入度都相等,而这两个顶点中一个顶点的出度与入度之差为1,另一个顶点的出度与入度之差为-1。

有向图欧拉回路充要条件:当D的所有顶点的出、入度都相等时,D中存在有向欧拉回路。

思路:一个单词关键是首字母和尾字母,可以把首字母和尾字母看成顶点,这个单词看成这两个顶点间的边,这么建图,于是原题就变成了找这个图中是否存在欧拉通路或者欧拉回路。建完图之后只需要根据定理判断每个顶点的出度、入度以及图的连通性即可。

#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 500100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define LLINF 0x7FFFFFFFFFFFFFFF
#define seed 131
#define mod 1000000007
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

int du[30],vis[30],father[30];
int n,cnt;
char str[1010];
int Find(int x){
    int t = x;
    while(t!=father[t])
        t = father[t];
    int k = x;
    while(k!=t){
        int temp = father[k];
        father[k] = t;
        k = temp;
    }
    return t;
}
int main(){
    int t,i,j,l;
    scanf("%d",&t);
    while(t--){
        cnt = 0;
        memset(du,0,sizeof(du));
        memset(vis,0,sizeof(vis));
        for(i=0;i<30;i++)   father[i] = i;
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%s",str);
            l = strlen(str);
            du[str[0]-'a']--;
            du[str[l-1]-'a']++;
            vis[str[0]-'a'] = 1;
            vis[str[l-1]-'a'] = 1;
            int aa = Find(str[0]-'a');
            int bb = Find(str[l-1]-'a');
            if(aa!=bb){
                if(aa<bb)   father[bb] = aa;
                else    father[aa] = bb;
            }
        }
        int flag1,flag2,flag3,flag;
        flag1 = flag2 = flag3 = flag = 0;
        for(i=0;i<30;i++){
            if(du[i]==0)    continue;
            else if(du[i]==-1)  flag1++;
            else if(du[i]==1)   flag2++;
            else    flag3 = 1;
        }
        if(!flag3&&flag1==1&&flag2==1)  flag = 1;
        else if(!flag1&&!flag2&&!flag3) flag = 1;
        else    flag = 0;
        if(flag){
            int tt = 0;
            for(i=0;i<26;i++){
                if(vis[i]&&father[i]==i){
                    tt++;
                }
            }
            if(tt>1)    flag = 0;
            if(flag)    puts("Ordering is possible.");
            else    puts("The door cannot be opened.");
        }
        else    puts("The door cannot be opened.");
    }
    return 0;
}

POJ--1386--Play on Words【判断有向图欧拉通路、欧拉回路】

时间: 2024-08-04 07:03:07

POJ--1386--Play on Words【判断有向图欧拉通路、欧拉回路】的相关文章

[ACM] POJ 2513 Colored Sticks (Trie树,欧拉通路,并查集)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 29736   Accepted: 7843 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a st

POJ 2337 Catenyms (有向图欧拉通路)

Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9914   Accepted: 2588 Description A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For e

poj 1386 Play on Words(有向图欧拉路+并查集)

题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指向尾字母的有向边, 则则所有的单词构成了一个有向图,问题变为判断该有向图中是否存在一条欧拉路:有向图中存在欧拉路的两个充分必要条件为:该图是联通的并且所有的点的入度等于出度或者只存在两个点的入度与出度不等,一个点的入度=出度+1,另一个点的入度=出度-1: 代码如下: #include <cstdi

hdu3472 混合图判断欧拉通路

对于欧拉回路,先判断出度入度的差是否为偶数,然后最大流一次. 此题是判断有无欧拉通路,前提要判断图是否连通,然后欧拉通路的条件:要么出入度差没有奇数,或者只有2个点. 所以先统计差为奇数的个数,如果不为0或2,不可能.然后如果为2,表示可能使欧拉路,所以此时可以将这两个点相连,类似添加一条无向边.然后就是判断是否为欧拉回路了. #include<stdio.h> #include<string.h> #include<queue> #define maxn 50 #de

CodeForces - 508D Tanya and Password(欧拉通路)

Description While dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of n + 2 characters. She has written all the possible n three-letter continuous substrings of the

uva 10054 The Necklace(欧拉通路)

提交了7次,总算AC了.题目不难,就是判断下欧拉通路.注意细节. /* Status:AC Title :The Necklace */ #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <stack> #include <queue> #include <s

POJ训练计划_Colored Sticks(字典树+判断欧拉通路)

解题报告 http://blog.csdn.net/juncoder/article/details/38236333 题目传送门 题意: 问给定一堆的棒,两端有颜色,相同的颜色的棒可以头尾相接,能否连在一条直线. 思路: 把每一根棒两端看成两个点,之间连着线,判断这样的一个图中是否有欧拉通路 欧拉通路: 在联通无向图中,经过G的每一条边一次并且仅有一次的路径为欧拉通路. 求欧拉通路的充分条件:图为联通图,并且仅有两个奇度数的结点或无奇度结点. #include <queue> #includ

POJ--1300--Door Man【判断欧拉通路】

链接:http://poj.org/problem?id=1300 题意:有n个房间,每个房间有若干个门和别的房间相连,管家从m房间开始走,要回到自己的住处(0),问是否有一条路可以走遍所有的门并且没有重复的路. 思路:判断是否存在欧拉通路,根据欧拉通路.欧拉回路的性质来做.有两种情况:一种是欧拉回路,所有房间的门的个数都是偶数个,并且此时初始房间不是0,此时存在要求的路径,如果初始是0则不行.另一种是欧拉通路,只有两个房间门是奇数个,剩下都是偶数个,并且这两个房间一个是0,一个是当前起点,并且

[ACM] POJ 2154 Color (Polya计数优化,欧拉函数)

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7630   Accepted: 2507 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th