sdnu-并查集&图论-weeklyexam ——C - Play on Words

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.

哇,没想到欧拉图和七桥问题的代码原理是这样的,这个题当时给我的知识还是很多的

判断能否联通用到了欧拉图(主要看入度和出度)

既然有了欧拉图的参与那么除了普通的找老大,连老大外还要有对图是否能联通的判断

把每个单词的首尾字母相连(一端为入一端为出,任意,无非就是走向不同)我把尾部定义为入,首部为出

计算每一个的出度和入度

并把他们连在一块

然后对于每一个出现过的字母(vis检查)

如果发现有一个人的老大就是他自己的话,那么还好说无非就是a________a这种情况,可是要是还有一个人他的老大也是自己,娜美办法了,有了两个阵营,失败

然后对于入度和出度要么每一个点入度 = 出度要么有两个点分别是入度 — 出度 = 1和出度 — 入度 = 1 这两个点也就是一笔画的起点和终点,其余情况 失败

剩下的就是输出了

#include
#include
#include
#include
using namespace std;
int pre[30];
int vis[30];
int outnum[30];
int innum[30];
int find(int x)
{

return x == pre[x] ? x : find(pre[x]);
	
}
void join(int a,int b)
{
	a = find(a);
	b = find(b);
	if(a != b)
	{
		pre[b] = a; 
	}
	
}
int main()
{
	int t,n;
	cin>>t;
	while(t--)
	{
		memset(pre,0,sizeof(pre));
		memset(vis,0,sizeof(vis));
		memset(outnum,0,sizeof(outnum));
		memset(innum,0,sizeof(innum));
		for(int i = 1;i < 30;i++)
		{
			pre[i] = i;
		}
		cin>>n;
		for(int i = 0;i < n;i++)
		{
			string s;
			cin>>s;
			int begin = int(s[0] - 'a') + 1;
			//cout<<"begin = "<





你可能感兴趣的:(oj)