题意翻译
输入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;
}