UVa 1572 Self-Assembly (拓扑排序)
题目链接:
https://cn.vjudge.net/problem/UVA-1572
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)。
/*
题意描述:
给出n张正方形的牌,牌上有从东开始逆时针四条边的符号,当两张牌的边上字母相同而符号不同的时候表示两张牌可以连接,问能否通过这
几张牌构成一各无限大的结构。
解题思路:
题意比较难以理解,理一理思路,无限大的结构,就是类似于某种分子结构中间是可以无限添加的,那么把标号看成是节点,一共有26*2个,
将各个牌看做是某种关系,构成一个有向图,如果该有向图中存在一个有向环,就说明可以构成一个无限大的结构。那么问题就转化成了,
判断一个有向图中是否存在有向环的问题,做一次拓扑排序,如果存在拓扑序列,则不存在有向环,如果不存在拓扑序列,则存在有向环。
拓扑排序的队列解法的思路
根据有向图计算每个节点的出度和入度,将入度为0的节点依次加入队列,然后进入循环一次出队一个,并将和它有关系的节点B的入度减一,
如果节点B的入度恰好减为0,就将节点B也加入队列,直到队列为空。如果出队的节点数和总数相等表示存在拓扑序列,也即不存在环。但是
由于本题中边的关系特殊,没有办法根据顶点的入度为0,确定入队,因为一张牌上有四个符号,统计入度和出度的时候必须组合一下得出关
系,增加入度,故采用拓扑排序的递归解法。
拓扑排序的递归解法
借助一个标记数组c,c[u]=0表示从来没有访问过u,c[u]=1表示已经访问过,并且已经递归访问过它的所有子孙,c[u]=-1表示正在访问,也
即正在访问它的子孙,而dfs(u)正在栈帧中,尚未返回。
*/
#include<bits/stdc++.h>
using namespace std; const int maxn=+;
bool mp[maxn][maxn];
int c[maxn];
int cycle();
int dfs(int u); int id(char a1,char b1){
return (a1-'A')*+ b1=='+'? : ;
}
void connect(char a1,char b1,char a2,char b2){
if(a1 == '' || b1 == '')
return;
int u=id(a1,b1)^,v=id(a2,b2);
mp[u][v]=;
} int main()
{
int n;
char list[maxn];
//freopen("E:\\testin.txt","r",stdin);
while(scanf("%d",&n) != EOF){
memset(mp,,sizeof(bool)*maxn*maxn);
for(int i=;i<n;i++){
scanf("%s",list); for(int i=;i<;i++){
for(int j=;j<;j++){
if(i != j) connect(list[i*],list[i*+],list[j*],list[j*+]);
}
}
} if(cycle())
printf("bounded\n");
else
printf("unbounded\n");
}
return ;
} int cycle(){
memset(c,,sizeof(int)*maxn);
for(int u=;u<;u++){
if(c[u] == && dfs(u)){
return ;
}
}
return ;
} int dfs(int u){
c[u]=-;
for(int v=;v<;v++){
if(mp[u][v]){
if(c[v] == -)
return ;//存在有向环
else
if(c[v] == && dfs(v))
return ;
}
}
c[u]=;
return ;
}
UVa 1572 Self-Assembly (拓扑排序)的更多相关文章
- UVA 1572 Self-Assembly(拓扑排序)
1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓 ...
- UVA.10305 Ordering Tasks (拓扑排序)
UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- UVa 10305 - Ordering Tasks (拓扑排序裸题)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- UVA 10305 Ordering Tasks(拓扑排序的队列解法)
题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...
- Ordering Tasks UVA - 10305 图的拓扑排序
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- UVA 12263 Rankings(拓扑排序)
给出一个n个数的序列1,然后有m个改动(a, b),在序列2中a跟b在序列中的相对顺序改变.求符合题意的序列2. 题中说道如果一个数的位置不确定,则输出‘?' ,仔细想想,这种情况是不会存在的,因为在 ...
- UVA - 1423 Guess (拓扑排序)
题意:已知矩阵S,求序列a.已知矩阵Sij = “ + ” if ai + . . . + aj > 0; Sij = “ − ” if ai + . . . + aj < 0; and ...
- UVa 1572 Self-Assembly (构造+拓扑排序。。。。。)
题意:给定n个带标号的正方形,标号要么是一个大写字母加一个+或-,要么是00, 当且仅当大写字母相同并且符号相反时可以连接,问你给定的能不能拼成一个无限大的的东西. 析:说实话,真心没有看出来是拓扑排 ...
随机推荐
- CRC校验3种算法_转载
//CRC16校验在通讯中应用广泛,这里不对其理论进行讨论,只对常见的3种//实现方法进行测试.方法1选用了一种常见的查表方法,类似的还有512字//节.256字等查找表的,至于查找表的生成,这里也略 ...
- TVS(瞬间电压抑制器)
1.原理 TVS二极管在线路板上与被保护线路并联,当瞬时电压超过电路正常工作电压后,TVS二极管便产生雪崩,提供给瞬时电流一个超低电阻通路,其结果是瞬时电流透过二极管被引开,避开被保护元件,并且在电压 ...
- redhat安装docker
一.禁用selinux 由于Selinux和LXC有冲突,所以需要禁用selinux.编辑/etc/selinux/config,设置两个关键变量. SELINUX=disabled SEL ...
- 【BZOJ3545】 [ONTAK2010]Peaks
BZOJ3545 [ONTAK2010]Peaks Solution 既然会加强版,直接把强制在线的操作去掉就好了. 代码实现 #include<stdio.h> #include< ...
- Linux基础命令-cd
cd 作用:切换路径 切换至家目录 $ cd $ cd~ 在上一个目录和当前目录来回切换 $ cd - 切换至某用户的家目录 # cd ~ # pwd /root # cd ~quail #pwd / ...
- C#6.0语言规范(十五) 委托
委托启用其他语言(如C ++,Pascal和Modula)已使用函数指针进行寻址的方案.但是,与C ++函数指针不同,委托是完全面向对象的,与成员函数的C ++指针不同,委托封装了对象实例和方法. 委 ...
- Shell - 简明Shell入门05 - 条件语句(Case)
示例脚本及注释 #!/bin/bash var=$1 # 将脚本的第一个参数赋值给变量var case $var in right) echo "Right!";; wrong) ...
- Java - 集成开发环境Eclipse的使用方法和技巧
00 - Eclipse教程 Eclipse 教程 01 - Eclipse设置编译和运行的环境 建议编译和运行的版本保持一致,否则请特别注意: 低编译,高运行 ---> 可行. 高编译,低运行 ...
- Selenium3 + Python3自动化测试系列五——常用断言Assertion
断言Assertion 验证应用程序的状态是否同所期望的一致. 常见的断言包括:验证页面内容,如标题是否为X或当前位置是否正确,或是验证该复选框是否被勾选. selenium 提供了三种模式的断言:a ...
- vue教程1-07 模板和过滤器
vue教程1-07 模板和过滤器 一.模板 {{msg}} 数据更新模板变化 {{*msg}} 数据只绑定一次 {{{msg}}} HTML转意输出 <!DOCTYPE html> < ...