Play on Words 欧拉通路(回路)判断

                      Play on Words

note:  判断一下连通性。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long LL;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=;
const int SIZE=; int n;
char word[MS];
int od[SIZE],id[SIZE]; int appear[SIZE];
int fa[SIZE]; struct edge
{
int u,v;
}edges[MS]; void fa_init()
{
for(int i=;i<SIZE;i++)
fa[i]=-;
} int find(int x)
{
int s;
for(s=x;fa[s]>=;s=fa[s]); while(s!=x)
{
int tmp=fa[x];
fa[x]=s;
x=tmp;
}
return s;
} void merge(int x,int y)
{
int f1=find(x);
int f2=find(y);
int tmp=fa[f1]+fa[f2];
if(fa[f1]>fa[f2])
{
fa[f1]=f2;
fa[f2]=tmp;
}
else
{
fa[f2]=f1;
fa[f1]=tmp;
}
} bool connect()
{
int u,v,i;
fa_init();
for(i=;i<n;i++)
{
u=edges[i].u;
v=edges[i].v;
if(u!=v&&find(u)!=find(v))
merge(u,v);
} int first=-;
for( i=;i<SIZE;i++)
{
if(appear[i]==)
continue;
if(first==-)
first=i;
else if(find(i)!=find(first))
break;
}
if(i<SIZE)
return false;
return true;
} int main()
{
int u,v;
int i,j;
int T;
scanf("%d",&T);
while(T--)
{
memset(od,,sizeof(od));
memset(id,,sizeof(id));
memset(appear,,sizeof(appear));
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%s",word);
u=word[]-'a';
v=word[strlen(word)-]-'a';
od[u]++;
id[v]++; appear[u]=appear[v]=;
edges[i].u=u;
edges[i].v=v;
} bool Euler=true;
int one=,none=;
for(i=;i<SIZE;i++)
{
if(appear[i]==)
continue;
if(od[i]-id[i]>=||id[i]-od[i]>=)
{
Euler=false;
break;
}
if(od[i]-id[i]==)
{
one++;
if(one>)
{
Euler=false;
break;
}
}
if(id[i]-od[i]==)
{
none++;
if(none>)
{
Euler=false;
break;
}
}
}
if(one!=none)
Euler=false;
if(connect()==false)
Euler=false;
if(Euler)
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return ;
}
上一篇:EasyUI组件(窗口组件)


下一篇:WinForm多线程编程简单Demo