poj 1386 Play on Words(单向欧拉函数+并查集)
2015-09-24 21:02
645 查看
题目链接:
http://poj.org/problem?id=1386
Play on Words
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
Sample Output
题目大意就是给你n个单词问你这n个单词能否首尾连接成一串,有点像成语接龙的感觉。。
思路就是用并查集判断是否联通,欧拉回路判断出度入度就好
AC代码:
v#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<utility>
using namespace std;
vector<pair<int ,int> >edge;
int in[30],out[30];
int set[30];
int used[30];
int n;
void init()
{
memset(used,0,sizeof(used));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=0;i<=26;i++)
set[i]=i;
edge.clear();
}
int find(int a)
{
int root=a;
int temp;
while(set[root]!=root)
root=set[root];
while(set[a]!=root)
{
temp=a;
a=set[a];
set[temp]=root;
}
return root;
}
bool merge(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y)
return false;
set[x]=y;
return true;
}
bool eluer()
{
int ort,st,ed;
st=ed=0;
ort=-1;
for(int i=0;i<edge.size();i++)
merge(find(edge[i].first),find(edge[i].second));
for(int i=0;i<26;i++)
if(used[i])
{
if(ort==-1) ort=i;
if(abs(in[i]-out[i])>=2)
return false;
if(find(i)!=find(ort))
return false;
if(in[i]-out[i]==1)
{
ed++;
if(ed>1)
return false;
}
if(out[i]-in[i]==1)
{
st++;
if(st>1)
return false;
}
}
return true;
}
int main()
{
char s[1100];
int t;
scanf("%d",&t);
for(int kk=0;kk<t;kk++)
{
scanf("%d",&n);
init();
for(int i=0;i<n;i++)
{
scanf("%s",s);
int u=s[0]-'a';
int v=s[strlen(s)-1]-'a';
in[v]++,out[u]++;
used[v]=used[u]=1;
edge.push_back(make_pair(v,u));
}
if(!eluer())
printf("The door cannot be opened.\n");
else
printf("Ordering is possible.\n");
}
return 0;
}
http://poj.org/problem?id=1386
Play on Words
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10871 | Accepted: 3709 |
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.
题目大意就是给你n个单词问你这n个单词能否首尾连接成一串,有点像成语接龙的感觉。。
思路就是用并查集判断是否联通,欧拉回路判断出度入度就好
AC代码:
v#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<utility>
using namespace std;
vector<pair<int ,int> >edge;
int in[30],out[30];
int set[30];
int used[30];
int n;
void init()
{
memset(used,0,sizeof(used));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=0;i<=26;i++)
set[i]=i;
edge.clear();
}
int find(int a)
{
int root=a;
int temp;
while(set[root]!=root)
root=set[root];
while(set[a]!=root)
{
temp=a;
a=set[a];
set[temp]=root;
}
return root;
}
bool merge(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y)
return false;
set[x]=y;
return true;
}
bool eluer()
{
int ort,st,ed;
st=ed=0;
ort=-1;
for(int i=0;i<edge.size();i++)
merge(find(edge[i].first),find(edge[i].second));
for(int i=0;i<26;i++)
if(used[i])
{
if(ort==-1) ort=i;
if(abs(in[i]-out[i])>=2)
return false;
if(find(i)!=find(ort))
return false;
if(in[i]-out[i]==1)
{
ed++;
if(ed>1)
return false;
}
if(out[i]-in[i]==1)
{
st++;
if(st>1)
return false;
}
}
return true;
}
int main()
{
char s[1100];
int t;
scanf("%d",&t);
for(int kk=0;kk<t;kk++)
{
scanf("%d",&n);
init();
for(int i=0;i<n;i++)
{
scanf("%s",s);
int u=s[0]-'a';
int v=s[strlen(s)-1]-'a';
in[v]++,out[u]++;
used[v]=used[u]=1;
edge.push_back(make_pair(v,u));
}
if(!eluer())
printf("The door cannot be opened.\n");
else
printf("Ordering is possible.\n");
}
return 0;
}
相关文章推荐
- POJ ACM 1001
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- 【高手回避】poj3268,一道很水的dijkstra算法题
- POJ 1088 滑雪
- poj2387 Til the Cows Come Home—Dijkstra模板
- poj 2485 Highways