POJ1386Play on Words(欧拉回路)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11748 | Accepted: 4018 |
Description
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
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
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
- 3
- 2
- acm
- ibm
- 3
- acm
- malform
- mouse
- 2
- ok
- ok
Sample Output
- The door cannot be opened.
- Ordering is possible.
- The door cannot be opened.
【题意】这个就相当于成语接龙,需满足前一个单词的尾字母与后一个的首字母相同。然后就是问是否 存在欧拉通路。
【分析】用两个数组存每个字母的入度与出度,再用vis[]判断字母是否出现,然后并查集判断是否联通。
定理1:无向图G存在欧拉通路的条件是:G为连通图,并且G只有两个奇度节点或者无奇度节点。
推论1:(1)当G是仅有两个奇度节点的连通图时,G的欧拉通路必以此两个节点为端点。
(2)当G是无奇度节点连通图时,G比为欧拉回路。
(3)G为欧拉图(存在欧拉回路)的充要条件是G为无奇度节点的连通图。
定理2:有向图D存在欧拉通路的充要条件是:D为有向图,D的基图联通,并且所有顶点的出度与入度都相等;或者除两个顶点外
其余顶点的出度与入度都相等,而这两个顶点中一个顶点的出度-入度==1,另一个出度-入度==-1;
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <algorithm>
- #include <climits>
- #include <cstring>
- #include <string>
- #include <set>
- #include <map>
- #include <queue>
- #include <stack>
- #include <vector>
- #include <list>
- #include<functional>
- #define mod 1000000007
- #define inf 0x3f3f3f3f
- #define pi acos(-1.0)
- using namespace std;
- typedef long long ll;
- const int N=;
- const int M=;
- ll power(ll a,int b,ll c){ll ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
- char str[N];
- int n,m;
- int vis[],pre[];
- int out[],in[];
- struct man
- {
- int u,v;
- }edg[N];
- void init()
- {
- memset(vis,,sizeof(vis));
- memset(out,,sizeof(out));
- memset(in,,sizeof(in));
- for(int i=;i<;i++)pre[i]=i;
- }
- int Find(int x) {
- if(pre[x] != x) pre[x] = Find(pre[x]);
- return pre[x];
- }
- void Union(int x,int y) {
- x = Find(x);
- y = Find(y);
- if(x == y) return;
- pre[y] = x;
- }
- bool beconnect()
- {
- for(int i=;i<n;i++){
- int u=edg[i].u,v=edg[i].v;
- if(u!=v&&Find(u)!=Find(v))Union(u,v);
- }
- int f=-,k;
- for(k=;k<;k++){
- if(!vis[k])continue;
- if(f==-)f=k;
- else if(Find(k)!=Find(f))break;
- }
- if(k<)return false;
- return true;
- }
- int main()
- {
- int t;
- scanf("%d",&t);
- while(t--){
- init();
- scanf("%d",&n);
- for(int i=;i<n;i++){
- scanf("%s",str);
- int u=str[]-'a',v=str[strlen(str)-]-'a';
- edg[i].u=u;edg[i].v=v;
- vis[u]=vis[v]=;
- out[u]++;in[v]++;
- }
- bool flag=true;
- int cnt1=,cnt2=;
- for(int i=;i<;i++){
- if(!vis[i])continue;
- if(abs(out[i]-in[i])>){
- flag=false;
- break;
- }
- if(out[i]-in[i]==){
- cnt1++;
- if(cnt1>){
- flag=false;
- break;
- }
- }
- if(out[i]-in[i]==-){
- cnt2++;
- if(cnt2>){
- flag=false;
- break;
- }
- }
- }
- if(cnt1!=cnt2)flag=false;
- if(!beconnect())flag=false;
- if(flag)puts("Ordering is possible.");
- else puts("The door cannot be opened.");
- }
- return ;
- }
POJ1386Play on Words(欧拉回路)的更多相关文章
- ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)
//网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...
- [poj2337]求字典序最小欧拉回路
注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
- UVA 10054 the necklace 欧拉回路
有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...
- POJ 1637 混合图的欧拉回路判定
题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...
- codeforces 723E (欧拉回路)
Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...
- UVa 12118 检查员的难题(dfs+欧拉回路)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 10054 (欧拉回路) The Necklace
题目:这里 题意:有一种由彩色珠子连接而成的项链,每个珠子两半由不同颜色(由1到50的数字表示颜色)组成,相邻的两个珠子在接触的地方颜色相同,现在有一些零碎的珠子,确认它是否能 复原成完整的项链. 把 ...
- poj2513Colored Sticks(无向图的欧拉回路)
/* 题意:将两端涂有颜色的木棒连在一起,并且连接处的颜色相同! 思路:将每一个单词看成一个节点,建立节点之间的无向图!判断是否是欧拉回路或者是欧拉路 并查集判通 + 奇度节点个数等于2或者0 */ ...
随机推荐
- 【BZOJ 2006】[NOI2010]超级钢琴 ST
我们先把所有最左端对应的最优右端入堆,eg: z 在[l,r](由题目给出的L,R决定)之间的最优解 y,然后出堆以后,再入堆z,y-1,z,y+1,那么我们只需要用st找最大前缀和就好了(ST是一 ...
- gogole调试请求体的数据怎么知道
在network---->header->request payload中看 详细情况见下图所示:
- nodejs npm insttall 带不带-g这个参数的区别
-g 中的g是global的意思所以带-g这个参数是全局安装,不带-g这个参数是本地安装. 在windows系统中全局安装的目录在:C:\Users\linsenq\AppData\Roaming\n ...
- java程序在centos7里面开机自启动
1.我们先来个简单的start,status,stop程序: [root@localhost ~]# cat /home/tomcat/jarservice.sh #!/bin/bashCU_PID= ...
- Linux Uptime 命令,让你知道你的系统运行了多久
对于一些人来说系统运行了多久是无关紧要的,但是对于服务器管理员来说,这是相当重要的信息.服务器在运行重要应用的时候,必须尽量保证长时间的稳定运行,有时候甚至要求零宕机.那么我们怎么才能知道服务器运行了 ...
- 利用saltstack初始化OpenStack服务器环境
目录架构图如上图所示 sls脚本详情如下: Sync_Host: file.managed: - name: /etc/hosts - source: salt://state/files/hosts ...
- SSL步骤
SSL步骤 被认证的服务器 1.创建keystore 2.创建信任证书 3.导出信任证书供客户端使用 客户端 1.创建keystore(如果不存在) 2.导入信任证书
- 【CF24D】Broken Robot (DP+高斯消元)
题目链接 题意:给定一个\(n\times m\)的矩阵,每次可以向→↓←移动一格,也可以原地不动,求从\((x,y)\)到最后一行的期望步数. 此题标签\(DP\) 看到上面这个肯定会想到 方法一: ...
- tyvj1048 田忌赛马
描述 中国古代的历史故事“田忌赛马”是为大家所熟知的.话说齐王和田忌又要赛马了,他们各派出N匹马,每场比赛,输的一方将要给赢的一方200两黄金,如果是平局的话,双方都不必拿出钱.现在每匹马的速 ...
- jquery with ajax
with session storage: 1.ajax请求可以放在 $(document).ready(function (){...}); 里. 2. $.ajax({ url: "/a ...