UVA10129 单词 Play on Words 图的一笔画问题

题意翻译

输入n(n≤100000)n(n\leq100000)n(n≤100000)个单词,是否可以把所有这些单词排成一个序列,使得每个单词的第一个字母可上一个单词的最后一个字母相同(例如acm,malform,mouseacm,malform,mouseacm,malform,mouse)。每个单词最多包含100010001000个小写字母。输入中可以有重复的单词。
输入输出样例
输入 #1

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

输出 #1

The door cannot be opened.
Ordering is possible.
The door cannot be opened.

一笔画条件 :

  • 底图联通(有向图看成无向图)
  • 最多只能有 2 ​ 2​ 2个点入度不等于出度,而且必须是其中一个点的入度比出度大1,另一个点反过来(出度比入度大1)
/** 
 * 一笔画条件 : 
 * 1. 底图联通
 * 2. 最多只有两个点入度不等于出度,
 *    且必须是一个点   入度-出度==1
 *    另一个点         出度-入度==1
 * 特殊情况 : 只有一个字符串 "axxxxxxa" 应该输出yes
 */
bool check() {
	bool ok = cnt == n; //先判断底图是否联通 记得用dfs判联通
	if(!ok) return false;

	int _in = 0; //记录入度减出度为1的个数
	int	_out = 0; //记录出度减入度为1的个数
	for(int i='a'; i<='z'; i++)
		if(mark[i]) {
			if(ind[i]-oud[i] == 1)
				_in ++;
			else if(oud[i]-ind[i] == 1)
			   	_out ++;
			else if(ind[i]-oud[i] != 0) //入度不为1或0的说明不能一笔画
				return false;
		}
	ok = (_in==1 && _out==1) || (_in==0 && _out==0); //AC
	//	ok = (_in==1&&_out==1); //not AC,原因是只有一个字符串"mabcm"应该输出yes

	return ok;
}

完整代码

#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN (int(512))
#define ll long long 
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \
	do { \
		cout << "\033[31;1m " << #x << " -> "; \
		err(x); \
	} while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO {

	char print_f[105];
	void read() { }
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
		inline void read(T &x, T2 &... oth) {
			x = 0;
			char ch = getchar();
			ll f = 1;
			while (!isdigit(ch)) {
				if (ch == '-') f *= -1; 
				ch = getchar();
			}
			while (isdigit(ch)) {
				x = x * 10 + ch - 48;
				ch = getchar();
			}
			x *= f;
			read(oth...);
		}
	template <typename T, typename... T2>
		inline void print(T x, T2... oth) {
			ll p3=-1;
			if(x<0) putchar('-'), x=-x;
			do{
				print_f[++p3] = x%10 + 48;
			} while(x/=10);
			while(p3>=0) putchar(print_f[p3--]);
			putchar(' ');
			print(oth...);
		}
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K, cnt;

int ind[MAXN], oud[MAXN], vis[MAXN], mark[MAXN];
set<int> G[MAXN];

#define cls(x) (memset(x, false, sizeof(x)))
void init() {
	cnt = 0;
	for(int i='a'; i<='z'; i++) 
		G[i].clear();
	cls(ind), cls(oud), cls(vis), cls(mark);
}

void dfs(int u) { //dfs判断底图是否联通
	vis[u] = true;
	cnt ++;
	for(auto v : G[u])
		if(!vis[v]) dfs(v);
}

/** 
 * 一笔画条件 : 
 * 1. 底图联通
 * 2. 最多只有两个点入度不等于出度,
 *    且必须是一个点   入度-出度==1
 *    另一个点         出度-入度==1
 * 特殊情况 : 只有一个字符串 "axxxxxxa" 应该输出yes
 */
bool check() {
	bool ok = cnt == n; //先判断底图是否联通
	if(!ok) return false;

	int _in = 0; //记录入度减出度为1的个数
	int	_out = 0; //记录出度减入度为1的个数
	for(int i='a'; i<='z'; i++)
		if(mark[i]) {
			if(ind[i]-oud[i] == 1)
				_in ++;
			else if(oud[i]-ind[i] == 1)
			   	_out ++;
			else if(ind[i]-oud[i] != 0) //入度不为1或0的说明不能一笔画
				return false;
		}
	ok = (_in==1 && _out==1) || (_in==0 && _out==0); //AC
	//	ok = (_in==1&&_out==1); //not AC,原因是只有一个字符串"mabcm"应该输出yes

	return ok;
}

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	// freopen("out_main", "w", stdout);
	clock_t stime = clock();
#endif
	cin >> Q;
	while(Q --) {
		cin >> m;
		init();
		string str;
		int u, v;
		set<int> seta;
		while(m --) {
			cin >> str;
			u = str.front(), v = str.back();
			G[u].insert(v), G[v].insert(u);
			mark[u] = mark[v] = true;
			ind[v] ++, oud[u] ++;
			seta.insert(v), seta.insert(u);
		}
		n = seta.size(); //点的个数
		
		dfs(u);
		bool ok = check();
		printf("%s\n", 
				ok ? "Ordering is possible." : "The door cannot be opened.");
	}





#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值