给n个单词,判断n个单词能否形成欧拉通路

要先判连通!

#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
int book[30], in[30];
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		queue<int>q;
		vector<int>v[30];
		memset(in, 0, sizeof in);
		memset(book, 0, sizeof book);
		int n;
		int all = 0, maxn = 0;
		char s[1005];
		scanf("%d", &n);
		while (n--)
		{
			scanf("%s", s);
			in[s[0] - 'a']++;
			in[s[strlen(s) - 1] - 'a']--;

			//vector和book用来判连通
			v[s[0] - 'a'].push_back(s[strlen(s) - 1] - 'a');
			v[s[strlen(s) - 1] - 'a'].push_back(s[0] - 'a');
			
			book[s[0] - 'a'] = 1;
			book[s[strlen(s) - 1] - 'a'] = 1;
		}
		//判连通
		for (int i = 0; i < 30; i++)
		{
			if (book[i])
			{
				q.push(i);
				book[i] = 0;
				break;
			}
		}
		while (!q.empty())
		{
			int temp = q.front();
			q.pop();
			for (auto it = v[temp].begin(); it != v[temp].end(); it++)
			{
				if (book[*it])
				{
					book[*it] = 0;
					q.push(*it);
				}
			}
		}
		//不连通
		for (int i = 0; i < 26; i++)
		{
			if (book[i])
			{
				puts("The door cannot be opened.");
				goto qwe;
			}
		}
		//若入度和出度有大于1或者小于-1,则不连通
		for (int i = 0; i < 26; i++)
		{
			if (in[i] > 1 || in[i] < -1)
			{
				puts("The door cannot be opened.");
				goto qwe;
			}
			all += in[i];
			if (in[i] == 1)
				maxn++;
		}
		//总入度和为0,最多只能有一个结点的入度为1,一个结点的出度为-1
		if (all == 0 && maxn <= 1)
			puts("Ordering is possible.");
		else
			puts("The door cannot be opened.");
	qwe:;
	}
}