Problem UVA1572-Self-Assembly(拓扑排序)
Problem UVA1572-Self-Assembly
Accept: 196 Submit: 1152
Time Limit: 3000 mSec
Problem Description
Automatic Chemical Manufacturing is experimenting with a process called self-assembly. In this process, molecules with natural affinity for each other are mixed together in a solution and allowed to spontaneously assemble themselves into larger structures. But there is one problem: sometimes molecules assemble themselves into a structure of unbounded size, which gums up the machinery. You must write a program to decide whether a given collection of molecules can be assembled into a structure of unbounded size. You should make two simplifying assumptions: 1) the problem is restricted to two dimensions, and 2) each molecule in the collection is represented as a square. The four edges of the square represent the surfaces on which the molecule can connect to other compatible molecules. In each test case, you will be given a set of molecule descriptions. Each type of molecule is described by four two-character connector labels that indicate how its edges can connect to the edges of other molecules. There are two types of connector labels:
• An uppercase letter (A, ..., Z) followed by + or -. Two edges are compatible if their labels have the same letter but different signs. For example, A+ is compatible with A- but is not compatible with A+ or B-.
• Two zero digits 00. An edge with this label is not compatible with any edge (not even with another edge labeled 00).
Assume there is an unlimited supply of molecules of each type, which may be rotated and reected. As the molecules assemble themselves into larger structures, the edges of two molecules may be adjacent to each other only if they are compatible. It is permitted for an edge, regardless of its connector label, to be connected to nothing (no adjacent molecule on that edge). Figure A.1 shows an example of three molecule types and a structure of bounded size that can be assembled from them (other bounded structures are also possible with this set of molecules).
Input
The input consists of several test cases. A test case consists of two lines. The first contains an integer n (1 ≤ n ≤ 40000) indicating the number of molecule types. The second line contains n eight-character strings, each describing a single type of molecule, separated by single spaces. Each string consists of four two-character connector labels representing the four edges of the molecule in clockwise order.
Output
For each test case, display the word ‘unbounded’ if the set of molecule types can generate a structure of unbounded size. Otherwise, display the word ‘bounded’.
Sample Input
3
A+00A+A+
00B+D+A-
B-C+00C+
1
K+K-Q+Q
Sample Output
bounded
unbounded
题解:这个题关键在于可以翻转,旋转倒在其次,能够翻转让这个题难度降低了很多,以正方形为边,连接可以相互转化的字符串,我一开始考虑的是直接连接一个正方形内的字符串,但是这样操作就要在
形如A-、A+形式的字符串之间连边,相对比较麻烦。看了lrj的代码,发现如果直接将连出边的那个字符串^1,就可以将边的含义转化为通过一个正方形,u可以转化为v,这样一来就不用添加刚才说的边了,这个操作看似简单,但是感觉很机智(orz)。
有一个地方值得注意,尝试对每一个字符串拓扑排序时,不用每一次都把vis清空,因为当你再一次遇到已经vis过的字符串时,后面的就不用操作了,如果有环,早就输出了,如果没环,也不会在这个地方再出现环。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; const int kind = ;
int n;
int gra[kind][kind]; void connect(char a1,char a2,char b1,char b2){
if(a1=='' || b1=='') return;
int u = ((a1-'A')<<)+(a2 == '+' ? : );
int v = ((b1-'A')<<)+(b2 == '+' ? : );
gra[u^][v] = ;
} int vis[kind]; bool dfs(int u){
vis[u] = -;
for(int i = ;i < kind;i++){
if(gra[u][i]){
if(vis[i] == -) return true;
if(!vis[i] && dfs(i)) return true;
}
}
vis[u] = ;
return false;
} int main()
{
//freopen("input.txt","r",stdin);
while(~scanf("%d",&n) && n){
char str[];
memset(gra,,sizeof(gra));
for(int k = ;k < n;k++){
scanf("%s",str);
for(int i = ;i < ;i++){
for(int j = ;j < ;j++){
if(i == j) continue;
connect(str[i<<],str[(i<<)+],str[j<<],str[(j<<)+]);
}
}
}
memset(vis,false,sizeof(vis));
int i;
for(i = ;i < kind;i++){
if(!vis[i] && dfs(i)) break;
}
if(i == kind) printf("bounded\n");
else printf("unbounded\n");
}
return ;
}
Problem UVA1572-Self-Assembly(拓扑排序)的更多相关文章
- UVA-1572 Self-Assembly(拓扑排序判断有向环)
题目: 给出几种正方形,每种正方形有无穷多个.在连接的时候正方形可以旋转.翻转. 正方形的每条边上都有一个大写英文字母加‘+’或‘-’.00,当字母相同符号不同时,这两条边可以相连接,00不能和任何边 ...
- Problem 1014 xxx游戏 暴力+拓扑排序
题目链接: 题目 Problem 1014 xxx游戏 Time Limit: 1000 mSec Memory Limit : 32768 KB 问题描述 小M最近很喜欢玩XXX游戏.这个游戏很简单 ...
- UVA-1572 Self-Assembly (图+拓扑排序)
题目大意:每条边上都有标号的正方形,两个正方形能通过相匹配的边连接起来,每种正方形都有无限多个.问能否无限延展下去. 题目分析:将边视为点,正方形视为边,建立无向图,利用拓扑排序判断是图否为DAG. ...
- UVa 1572 Self-Assembly (拓扑排序)
题目链接: https://cn.vjudge.net/problem/UVA-1572 Automatic Chemical Manufacturing is experimenting with ...
- UVA 1572 Self-Assembly(拓扑排序)
1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓 ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
- 图——拓扑排序(uva10305)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- *HDU1285 拓扑排序
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
随机推荐
- javascript中 Function.prototype.apply()与Function.prototype.call() 对比详解
Function.prototype.apply()|Function.prototype.call() apply()方法可以在使用一个指定的 this 值和一个参数数组(或类数组对象)的前提下调用 ...
- [转]Angular2: Cannot read property 'name' of undefined
本文转自:https://stackoverflow.com/questions/39755336/angular2-cannot-read-property-name-of-undefined 处理 ...
- spring cloud config与eureka配合使用
前面两篇介绍了Spring Cloud Config服务端和客户端的简单配置,本篇介绍Spring Cloud Config与Eureka配合使用 前言 默认情况下,配置客户端启动时,都是通过配置属性 ...
- angularjs学习第九天笔记(指令作用域【隔离作用域】研究)
您好,昨天学习了指令作用域为布尔型的情况, 今天主要研究其指针作用域为{}的情况 1.当作用域scope为{}时,子作用域完全创建一个独立的作用域, 此时,子做预约和外部作用域完全不数据交互 但是,在 ...
- MSSQL存储过程应用
1.原始表inoutinfo 2.现在想输入时间范围和操作类型输出对应的结果 2.1创建存储过程 create proc selecttype@type nvarchar(10),@starttime ...
- razor视图使用三元表达式
根据条件是否满足给input标签添加属性. <input type="radio" value="1" name="PortType" ...
- Linux常用基本命令:三剑客命令之-awk数组用法
AWK的数组用法跟javascript类似. 1,定义数组 awk 'BEGIN{a[0]="zhangsan";a[1]="lisi";print a[0]} ...
- HDU5293(SummerTrainingDay13-B Tree DP + 树状数组 + dfs序)
Tree chain problem Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- Python简单的网络编程
OSI 模型介绍 应用层 -- 对接受的数据进行解释.加密与解密.压缩与解压缩 会话层 -- 通过传输层(端口号: 传输端口和接受端口) 建立数据传输的通路 传输层 -- 定义了一些传输数据的协议和端 ...
- Android项目实战(三十二):圆角对话框Dialog
前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话框的"确定"按钮 难点:1.对话框边框圆角 ...