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 ;
}
Play on Words 欧拉通路(回路)判断的更多相关文章
- POJ 2513 无向欧拉通路+字典树+并查集
题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...
- hdu1116有向图判断欧拉通路判断
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 欧拉回路&欧拉通路判断
欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...
- poj2513- Colored Sticks 字典树+欧拉通路判断
题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...
- POJ 1300 欧拉通路&欧拉回路
系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...
- ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)
判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- HDU 5883 F - The Best Path 欧拉通路 & 欧拉回路
给定一个图,要求选一个点作为起点,然后经过每条边一次,然后把访问过的点异或起来(访问一次就异或一次),然后求最大值. 首先为什么会有最大值这样的分类?就是因为你开始点选择不同,欧拉回路的结果不同,因为 ...
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
- POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)
Colored Sticks Time Limit: 5000MS Memory ...
随机推荐
- Unity3D 200个插件免费分享
插件清单: 2D_Toolkit_1.51 动画开发插件包 FingerGestures 触摸插件 ORK_Okashi_RPG_Kit Unity3D角色扮演 ...
- c语言指针详解(转载)
转自(http://blog.csdn.net/ad_ad_ad/article/details/1522145) 指针是C语言中广泛使用的一种数据类型. 运用指针编程是C语言最主要的风格之一.利用指 ...
- How Tomcat Works(七)
本文接下来介绍并分析servlet容器,servlet容器是用来处理请求servlet资源,并为web客户端填充response对象的模块. servlet容器是org.apache.catalina ...
- SpringMVC3.2+JPA使用注解的方式环境搭建
==============================entity=========================================================package ...
- Spring REST实践之Versioning,Paging和Sorting
Versioning 为适应需求的变化以及兼容已有的API,需要创建新版本的API,一般有四种流行的版本化API的方法: URI版本化 URI参数版本化 Accept header版本化 自定义hea ...
- jquery checkbox选中状态
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Synchronization in Delphi TThread class : Synchronize, Queue
http://embarcadero.newsgroups.archived.at/public.delphi.rtl/201112/1112035763.html > Hi,>> ...
- Java *字格
class XingDemo { public static void main(String[] args) { int j = 0; int i = 0; int k = 0; for(i = 0 ...
- 使用jQuery Mobile和Phone Gap开发Android应用程序
经过了一段时间的学习,初步了解了该如何使用jQuery Mobile和 Phone Gap来开发一个Android应用程序,也想把这些东西介绍给大家. 1. 软件准备 要进行android app的开 ...
- Codeforces Gym 100463D Evil DFS
Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...