【例题 6-16 UVa 10129】Play on Words

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

入度减去出度个数为1的点和为-1的点各有1个,然后其他点入度减去出度为0 或者全都是入度为0的点即可。

【代码】

#include <bits/stdc++.h>
using namespace std;

const int N = 300;

int n,du[N];
map <char,int> mmap;
int f[N+10];
bool bo[N+10];

int ff(int x){
 	if (f[x]==x) return x;
 	else return f[x] =  ff(f[x]);
}

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("F:\c++source\rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
	int T;
	cin >> T;
	while (T--){
		memset(du,0,sizeof du);
		memset(bo,0,sizeof bo);
		for (int i = 'a';i <= 'z';i++)
			f[i] = i;
	 	cin >> n;
	 	string s;
	 	for (int i = 1;i <=n;i++){
	 	 	cin >> s;
	 	 	int x = s[0],y = s[(int) s.size()-1];
	 	 	du[(int) s[0]]++;
	 	 	du[(int) s[(int) s.size()-1]]--;
	 	 	bo[(int) s[0]] = true;
	 	 	bo[(int) s[(int) s.size()-1]] = true;
	 	 	if (ff(x)!=ff(y)){
	 	 	 	f[ff(x)] = ff(y);
	 	 	}

	 	}

	 	bool ok = true;
	 	int idx = 'a';
		for (int i = 'a';i <= 'z';i++)
			if (bo[i]){
			 	idx = ff(i);
			}
		for (int i = 'a';i <= 'z';i++)
			if (bo[i]&& ff(i)!=idx)
				ok = false;
		if (!ok){
		 cout <<"The door cannot be opened."<<endl;
		 continue;
		}

		int fi1 = 0,fi2 = 0;
	 	for (int i = 'a';i <= 'z';i++)
	 		if (du[i]==-1)
	 			fi1++;
	 		else if (du[i]==1){
	 		    	fi2++;
	 			}else if (du[i]!=0) fi1 = 2;
		
		if ((fi1==1 && fi2==1) || (fi1==0 && fi2==0)){
			cout << "Ordering is possible." << endl;
	    }else cout <<"The door cannot be opened."<<endl;
	}

	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7849160.html