HDU1116图论
http://acm.split.hdu.edu.cn/showproblem.php?pid=1116

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
const int M=;
int fa[M],vis[M],in[M],out[M],ans[M];
char ch[];
int fin(int x){
return fa[x]==x?fa[x]:fa[x]=fin(fa[x]);
}
int unin(int x,int y)
{ return fa[fin(y)]=fin(x);
}
int main()
{
int t,n,a,b;
cin>>t;
while(t--){
cin>>n;
memset(vis,,sizeof(vis));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
for(int i=;i<;i++){///初始化父亲结点
fa[i]=i;
}
for(int i=;i<n;i++){
scanf("%s",ch);
a=ch[]-'a';
b=ch[strlen(ch)-]-'a';
unin(a,b);///把字符串的开头字母和结尾字母连结起来,即是把它看作是一条有向线段
out[a]++;///出度
in[b]++;///入度
vis[a]=;///标记
vis[b]=;///标记
}
for(int i=;i<;i++)
fa[i]=fin(i);///fa存储的是每个点最终的根节点
int cnt=;
for(int i=;i<;i++)
if(vis[i]&&fa[i]==i)
cnt++;
if(cnt>)///cnt大于1代表除啦根节点,还有根节点没有变的节点,那么此时代表图不连通
{ cout<<"The door cannot be opened.\n";
continue;
}
int j=;
for(int i=;i<;i++){
if(vis[i]&&out[i]!=in[i])
ans[j++]=i;
}
if(j==){
cout<<"Ordering is possible.\n";///j=0代表所有点的入度等于出度,此时形成欧拉回路
continue;
}
if(j==&&((out[ans[]]-in[ans[]]==&&
in[ans[j-]]-out[ans[j-]]==)
||out[ans[j-]]-in[ans[j-]]==&&
in[ans[]]-out[ans[]]==))///此处为欧拉通路的判断,是初始节点和末尾节点的度的判断,为欧拉通路
{
cout<<"Ordering is possible.\n";
continue;
}
cout<<"The door cannot be opened.\n"; }
}
HDU1116图论的更多相关文章
- [leetcode] 题型整理之图论
图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...
- 并查集(图论) LA 3644 X-Plosives
题目传送门 题意:训练指南P191 分析:本题特殊,n个物品,n种元素则会爆炸,可以转移到图论里的n个点,连一条边表示u,v元素放在一起,如果不出现环,一定是n点,n-1条边,所以如果两个元素在同一个 ...
- NOIp 2014 #2 联合权值 Label:图论 !!!未AC
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
- HDU 5521 [图论][最短路][建图灵感]
/* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...
- SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...
- [转] POJ图论入门
最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...
- HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Codeforces 553C Love Triangles(图论)
Solution: 比较好的图论的题. 要做这一题,首先要分析love关系和hate关系中,love关系具有传递性.更关键的一点,hate关系是不能成奇环的. 看到没有奇环很自然想到二分图的特性. 那 ...
- 图论(floyd算法):NOI2007 社交网络
[NOI2007] 社交网络 ★★ 输入文件:network1.in 输出文件:network1.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在社交网络( ...
随机推荐
- smarty函数
内置函数(重要的几个): <{html_checkboxes name='nation' values=$code output=$name selected=$selid separator= ...
- VBA 插入一行保留样式
Rows(processingRow).Insert ' 在指定的行数processingRow处插入一行 Rows(processingRow - 1).Select ' 选择上一行的整行 Sele ...
- Java文件读写
import java.io.*; public class Study { public static void main(String[] args) { try { String strPath ...
- 【python】unittest中常用的assert语句
下面是unittest模块的常用方法: assertEqual(a, b) a == b assertNotEqual(a, b) a != b assertTrue(x) b ...
- LintCode "Binary Representation"
Not hard to think of a solution. But the key is all details. class Solution { public: /** *@param n: ...
- (转)C# 使用BackgroundWorker
本文转载自:http://blog.csdn.net/andrew_wx/article/details/6615077 该例子为使用BackgroundWorker在TextBox文本中产生一个10 ...
- RPM Fusion on CentOS7
RPM Fusion RPM Fusion provides software that the Fedora Project or Red Hat doesn't want to ship. Tha ...
- css hack 总结 包括ie6-11,chrome,opera,firefox
<!DOCTYPE html> <html> <head> <title>Css Hack ie各版本 opera chrome safari fire ...
- Hadoop学习9--动态增加datanode
http://www.cnblogs.com/ggjucheng/archive/2012/04/18/2454689.html
- Git-Flow
Overview Git-Flow is a high-level command set wrapping low-level Git commands to support the "s ...