(判断欧拉回路)poj 1368

Play on Words

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10056   Accepted: 3447

Description

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.

Input

The 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

Your 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.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<vector>
using namespace std;
vector<int> e[27];
int tt,n,in[27],out[27],vis[27];
char s[100010];
void dfs(int x)
{
      vis[x]=1;
      for(int i=0;i<e[x].size();i++)
            if(vis[e[x][i]]==0)
                  dfs(e[x][i]);
}
bool ok(int x)
{
      dfs(x);
      for(int i=0;i<26;i++)
            if(vis[i]==0&&e[i].size()!=0)
                  return false;
      return true;
}
int main()
{
      scanf("%d",&tt);
      while(tt--)
      {
            memset(in,0,sizeof(in));
            memset(out,0,sizeof(out));
            memset(vis,0,sizeof(vis));
            int len,a,b;
            scanf("%d",&n);
            for(int i=0;i<27;i++)
                  e[i].clear();
            for(int i=0;i<n;i++)
            {
                  scanf("%s",s);
                  len=strlen(s);
                  a=s[0]-‘a‘,b=s[len-1]-‘a‘;
                  out[a]++,in[b]++;
                  e[a].push_back(b);
                  e[b].push_back(a);
            }
            if(!ok(a))
            {
                  printf("The door cannot be opened.\n");
                  continue;
            }
            bool flag=false;
            int j;
            for(j=0;j<26;j++)
            {
                  if(in[j]!=out[j])
                  {
                        if(in[j]<out[j])
                        {
                              if(in[j]-out[j]==-1&&flag==0)
                              {
                                    flag=1;
                              }
                              else
                              {
                                    break;
                              }
                        }
                  }
            }
            if(j<26)
                  printf("The door cannot be opened.\n");
            else
                  printf("Ordering is possible.\n");
      }
      return 0;
}

  注意连通性

时间: 2024-11-03 20:53:44

(判断欧拉回路)poj 1368的相关文章

poj1300判断欧拉回路

对于连通图 无向图:1.无奇点,可以从任意一点出发回到原点. 2.存在奇点,且只有两个,从一奇点出发,另一奇点终止. 有向图:1.所有点入度等于出度. 2.只有两个点入度不等于出度,且其中一个点入度比出度大一另一个点的出度比入度大一. #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <

[欧拉回路] poj 1386 Play on Words

题目链接: http://poj.org/problem?id=1386 Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9685   Accepted: 3344 Description Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve

[欧拉回路] poj 2513 Colored Sticks

题目链接: http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 30955   Accepted: 8159 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it

[欧拉回路] poj 1300 Door Man

题目链接: http://poj.org/problem?id=1300 Door Man Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2137   Accepted: 857 Description You are a butler in a large mansion. This mansion has so many rooms that they are merely referred to by number

[欧拉回路] poj 2230 Watchcow

题目链接: http://poj.org/problem?id=2230 Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6055   Accepted: 2610   Special Judge Description Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk acr

计蒜客 | 欧拉回图 | 判断欧拉回路

你学过一笔画问题么?其实一笔画问题又叫欧拉回路,是指在画的过程中,笔不离开纸,且图中每条边仅画一次,而且可以回到起点的一条回路. 蒜头君打算考考你,给你一个图,问是否存在欧拉回路? 输入格式 第 11 行输入两个正整数,分别是节点数 N(1 < N < 1000)N(1<N<1000) 和边数 M(1 < M < 100000)M(1<M<100000): 紧接着 MM 行对应 MM 条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从 1

素数判断+BFS POJ 3126

题意: 给两个四位的素数,求出从第一个素数变为第二个素数的最短路径.每步可以变素数的一位,并且每步得到的数必须是素数. 先把素数打表,然后bfs求最短路径就可以了,如果变换后得到的数是素数,就加入队列. 代码: #include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include<climits> #inc

HDU 1878(1Y) (判断欧拉回路是否存在 奇点个数为0 + 一个联通分量 *【模板】)

欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9797    Accepted Submission(s): 3554 Problem Description 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回路? Input 测试输入包含若干测试用例.每个测试用例的第

P1341 无序字母对(Hierholzer算法判断欧拉回路)

https://www.luogu.com.cn/problem/P1341 https://blog.csdn.net/STILLxjy/article/details/51956183?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task https://blog.csdn.net/qq_37555704/article/details/83347641