题目链接:

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 (拓扑排序)的更多相关文章

  1. UVA 1572 Self-Assembly(拓扑排序)

    1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓 ...

  2. UVA.10305 Ordering Tasks (拓扑排序)

    UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...

  3. Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现

    今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...

  4. UVa 10305 - Ordering Tasks (拓扑排序裸题)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  5. UVA 10305 Ordering Tasks(拓扑排序的队列解法)

    题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...

  6. Ordering Tasks UVA - 10305 图的拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  7. UVA 12263 Rankings(拓扑排序)

    给出一个n个数的序列1,然后有m个改动(a, b),在序列2中a跟b在序列中的相对顺序改变.求符合题意的序列2. 题中说道如果一个数的位置不确定,则输出‘?' ,仔细想想,这种情况是不会存在的,因为在 ...

  8. UVA - 1423 Guess (拓扑排序)

    题意:已知矩阵S,求序列a.已知矩阵Sij = “ + ” if ai + . . . + aj > 0; Sij = “ − ” if ai + . . . + aj < 0; and ...

  9. UVa 1572 Self-Assembly (构造+拓扑排序。。。。。)

    题意:给定n个带标号的正方形,标号要么是一个大写字母加一个+或-,要么是00, 当且仅当大写字母相同并且符号相反时可以连接,问你给定的能不能拼成一个无限大的的东西. 析:说实话,真心没有看出来是拓扑排 ...

随机推荐

  1. python之函数2

    Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...

  2. AngularJS 模块及provide

    一.模块 模块是一些功能的集合,如控制器.服务.过滤器.指令等子元素组成的整体. 1.注册和使用 模块相当于是一个注册表,保存着名字和编程元素的对照表,可存入也可取出. angular.module( ...

  3. ASP.NET Web API 框架研究 ASP.NET Web API 路由

    ASP.NET Web API 核心框架是一个独立的.抽象的消息处理管道,ASP.NET Web API有自己独立的路由系统,是消息处理管道的组成部分,其与ASP.NET路由系统有类似的设计,都能找到 ...

  4. 3.1.1随机事件的概率的Breamer(2018-03-22)

    % !Mode:: "TeX:UTF-8" \documentclass[xcolor=svgnames,serif,table,12pt]{beamer}%, %\include ...

  5. Android-Kotlin-代理和委托

    代理和委托,在生活中的案例有很多: 例如:小明工作很忙,需要办理银行卡,此时他委托给>>小李去给自己办理银行卡,小李来到办理中心 把自己的身份证/小李的身份证,给办理人员,说是小明委托我, ...

  6. Windows远程桌面连接ubuntu 16

    一.安装Xrdp Windows远程桌面使用的是RDP协议,所以ubuntu上就要先安装Xrdp,在ubuntu软件中心搜索xrdp安装. 安装xrdp的同时会自动安装vnc4server,xbase ...

  7. 背水一战 Windows 10 (41) - 控件(导航类): Frame

    [源码下载] 背水一战 Windows 10 (41) - 控件(导航类): Frame 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) Frame 示例Controls ...

  8. 【SPOJ10707】 COT2 Count on a tree II

    SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...

  9. Yii2 Apache + Nginx 路由重写

    一.什么是路由重写 原本的HTTP访问地址: www.test.com/index.php?r=post/view&id=100 表示这个请求将由PostController 的 action ...

  10. C#6.0语言规范(十) 类

    类是可以包含数据成员(常量和字段),函数成员(方法,属性,事件,索引器,运算符,实例构造函数,析构函数和静态构造函数)和嵌套类型的数据结构.类类型支持继承,这是一种派生类可以扩展和专门化基类的机制. ...