ACM: SGU 101 Domino- 欧拉回路-并查集
Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u
Description
Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men, stones, or even cards.
The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice...
The principle in nearly all modern dominoes games is to match one end of a piece to another that is identically or reciprocally numbered.
ENCYCLOPÆDIA BRITANNICA
Given a set of domino pieces where each side is marked with two digits from 0 to 6. Your task is to arrange pieces in a line such way, that they touch through equal marked sides. It is possible to rotate pieces changing left and right side.
Input
The first line of the input contains a single integer N (1 ≤ N ≤ 100) representing the total number of pieces in the domino set. The following N lines describe pieces. Each piece is represented on a separate line in a form of two digits from 0 to 6 separated by a space.
Output
Write “No solution” if it is impossible to arrange them described way. If it is possible, write any of way. Pieces must be written in left-to-right order. Every of N lines must contains number of current domino piece and sign “+” or “-“ (first means that you not rotate that piece, and second if you rotate it).
Sample Input
5
1 2
2 4
2 4
6 4
2 1
Sample Output
2 -
5 +
1 +
3 +
4 - //这个题目做了我一个晚上,有点头晕,还是要让自己保持冷静啊。。。 //多米诺骨牌有两个面,每个面有一个数字,相同的数字只能推倒相同数字面的骨牌,骨牌可以转向,给出的数字是严格的正面反面,问是否能推到所有的骨牌。 //如果可以输出骨牌的顺序和是否需要反转骨牌。如果不可以全部推倒,输出"No solution" //AC代码:
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"string"
#include"cstdio"
#include"vector"
#include"cmath"
#include"queue"
using namespace std;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define MX 401 bool vis[MX];
int Head[MX],cnt;
int indegree[MX],p[MX],siz,bin;
int fid[7]; struct Edge {
int v,nxt;
int flag;
} E[MX]; int find(int x) {
return fid[x]==x?x:(fid[x]=find(fid[x]));
} void union_root(int st,int ed) {
int rt1=find(st);
int rt2=find(ed);
if(rt1!=rt2)fid[rt2]=rt1;
} void edge_init() {
cnt=0;
siz=0;
memset(p,0);
memset(E,0);
memset(vis,0);
memset(Head,-1);
memset(indegree,0);
for(int i=0; i<=6; i++) {
fid[i]=i;
}
} void edge_add(int st,int ed,int flag) {
E[cnt].v=ed;
E[cnt].flag=flag;
E[cnt].nxt=Head[st];
Head[st]=cnt++;
} bool check() {
bin=-1;
int tot=0;
for(int i=0; i<=6; i++) {
if(vis[i]) {
if(bin==-1)bin=i;
if(indegree[i]%2) {
tot++;
if(tot>2)return 0;//判断奇数有多少个,超过两个奇数就判断不是
bin=i;
}
}
}
for(int i=0; i<=6; i++) {
if(bin!=-1)if(indegree[i]&&find(i)!=find(bin))return 0; //如果不是联通块就判断不是
}
if(bin==-1)return 0;
if(tot==1)return 0; //如果奇数只有一个判断不是
else return 1;
} void Fleury(int u,int e) {
for(int i=Head[u]; ~i; i=E[i].nxt) {
int v=E[i].v;
if(vis[i|1])continue;
vis[i|1]=1;
Fleury(v,E[i].flag);
}
if(e)p[siz++]=e; //标记是哪一条边。
} int main() {
int m,st,ed;
while(~scanf("%d",&m)) {
edge_init();
for(int i=1; i<=m; i++) {
scanf("%d%d",&st,&ed);
vis[st]=vis[ed]=1;
edge_add(st,ed,i); //正向
edge_add(ed,st,-i); //记录多米诺骨牌是否需要转向 反向
indegree[st]++;
indegree[ed]++;
union_root(st,ed); //合成联通块
}
bool flag=check();
if(!flag)puts("No solution");
else {
memset(vis,0); //再次清空标记数组
Fleury(bin,0);
for(int i=siz-1; i>=0; i--) {
printf("%d %c\n",p[i]>0?p[i]:-p[i],p[i]>0?'+':'-');
}
}
}
return 0;
}
ACM: SGU 101 Domino- 欧拉回路-并查集的更多相关文章
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
- SGU 101 Domino (输出欧拉路径)
101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...
- hdu 1116 欧拉回路+并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
- HDU 1116 Play on Words(欧拉回路+并查集)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...
- HDU1878 欧拉回路---(并查集+图论性质)
http://acm.hdu.edu.cn/showproblem.php?pid=1878 欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- ACM: Mr. Kitayuta's Colorful Graph-并查集-解题报
Mr. Kitayuta's Colorful GraphTime Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)
题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- nyist 42 一笔画 (欧拉回路 + 并查集)
nyoj42 分析: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径. 若该路径是一个圈,则称为欧拉(Euler)回路. 具有欧拉回路的图称为欧拉图(简称E图).具有欧拉路 ...
随机推荐
- IIS网站发布若干问题
1.Win7 64位 IIS未能加载文件或程序集"System.Data.SQLite"或它的某一个依赖项 未能加载文件或程序集"System.Data.SQLite ...
- ****php:require_once(dirname(__FILE__)."/./config_uc.php");
Q:麻烦清楚地讲解一下这句的意思,具体路径是怎样的,这个文解在 根目录,如果我想放在根目录下的tieba文件夹里,应该怎么修改/./ 这个是表示什么? A: require_once(dirname( ...
- 关于 redis、memcache、mongoDB 的对比(转载)
from:http://yang.u85.us/memcache_redis_mongodb.pdf 从以下几个维度,对 redis.memcache.mongoDB 做了对比.1.性能都比较高,性能 ...
- protobuf-net 对象二进制序列化与反序列号(转)
概述: Protobuf是google开源的一个项目,用户数据序列化反序列化,google声称google的数据通信都是用该序列化方法.它比xml格式要少的多,甚至比二进制数据格式也小的多. Prot ...
- 10g ASM下修改control file的位置
1.查看位置以及name是否正确 SQL> sho parameter name NAME TYPE VALUE ------------------------------------ --- ...
- Android 大牛的 blog 值得推荐 (转 整理)
1 收集了 国外著名开发者 25 人,包括 Github 地址.Blog 地址以及重点贡献介绍 链接 收集了 国内部分开发者 32人,包括 Github 地址.Blog 地址以及重点贡献介绍, 链接 ...
- 【项目总结】之——JS分割字符串
背景: 在我们做那个招标项目的时候,由于是刚刚接触到这个BS东西,我基本上是什么也不会.可是当时组长浩哥给过我一个任务,就是叫我将数据里面以字符串形式存在的信息切割开,然后显示到前台上去.当时对于浩哥 ...
- MapReduce中的分区方法Partitioner
在进行MapReduce计算时,有时候需要把最终的输出数据分到不同的文件中,比如按照省份划分的话,需要把同一省份的数据放到一个文件中:按照性别划分的话,需要把同一性别的数据放到一个文件中.我们知道最终 ...
- python 继承
继承一个类 如果已经定义了Person类,需要定义新的Student和Teacher类时,可以直接从Person类继承: class Person(object): def __init__(self ...
- DSP using MATlAB 示例Example2.10
上代码 % noise sequence 1 x = [3, 11, 7, 0, -1, 4, 2]; nx = [-3:3]; % given signal x(n) [y,ny] = sigshi ...