题目链接:

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)。

 1 /*
 2 题意描述:
 3 给出n张正方形的牌,牌上有从东开始逆时针四条边的符号,当两张牌的边上字母相同而符号不同的时候表示两张牌可以连接,问能否通过这
 4 几张牌构成一各无限大的结构。
 5 解题思路:
 6 题意比较难以理解,理一理思路,无限大的结构,就是类似于某种分子结构中间是可以无限添加的,那么把标号看成是节点,一共有26*2个,
 7 将各个牌看做是某种关系,构成一个有向图,如果该有向图中存在一个有向环,就说明可以构成一个无限大的结构。那么问题就转化成了,
 8 判断一个有向图中是否存在有向环的问题,做一次拓扑排序,如果存在拓扑序列,则不存在有向环,如果不存在拓扑序列,则存在有向环。
 9 拓扑排序的队列解法的思路
10 根据有向图计算每个节点的出度和入度,将入度为0的节点依次加入队列,然后进入循环一次出队一个,并将和它有关系的节点B的入度减一,
11 如果节点B的入度恰好减为0,就将节点B也加入队列,直到队列为空。如果出队的节点数和总数相等表示存在拓扑序列,也即不存在环。但是
12 由于本题中边的关系特殊,没有办法根据顶点的入度为0,确定入队,因为一张牌上有四个符号,统计入度和出度的时候必须组合一下得出关
13 系,增加入度,故采用拓扑排序的递归解法。
14 拓扑排序的递归解法
15 借助一个标记数组c,c[u]=0表示从来没有访问过u,c[u]=1表示已经访问过,并且已经递归访问过它的所有子孙,c[u]=-1表示正在访问,也
16 即正在访问它的子孙,而dfs(u)正在栈帧中,尚未返回。
17 */
18 #include<bits/stdc++.h>
19 using namespace std;
20
21 const int maxn=52+10;
22 bool mp[maxn][maxn];
23 int c[maxn];
24 int cycle();
25 int dfs(int u);
26
27 int id(char a1,char b1){
28     return (a1-'A')*2+ b1=='+'? 0 : 1;
29 }
30 void connect(char a1,char b1,char a2,char b2){
31     if(a1 == '0' || b1 == '0')
32         return;
33     int u=id(a1,b1)^1,v=id(a2,b2);
34     mp[u][v]=1;
35 }
36
37 int main()
38 {
39     int n;
40     char list[maxn];
41     //freopen("E:\\testin.txt","r",stdin);
42     while(scanf("%d",&n) != EOF){
43         memset(mp,0,sizeof(bool)*maxn*maxn);
44         for(int i=0;i<n;i++){
45             scanf("%s",list);
46
47             for(int i=0;i<4;i++){
48                 for(int j=0;j<4;j++){
49                     if(i != j)    connect(list[i*2],list[i*2+1],list[j*2],list[j*2+1]);
50                 }
51             }
52         }
53
54         if(cycle())
55             printf("bounded\n");
56         else
57             printf("unbounded\n");
58     }
59     return 0;
60 }
61
62 int cycle(){
63     memset(c,0,sizeof(int)*maxn);
64     for(int u=0;u<52;u++){
65         if(c[u] == 0 && dfs(u)){
66             return 1;
67         }
68     }
69     return 0;
70 }
71
72 int dfs(int u){
73     c[u]=-1;
74     for(int v=0;v<52;v++){
75         if(mp[u][v]){
76             if(c[v] == -1)
77                 return 1;//存在有向环
78             else
79                 if(c[v] == 0 && dfs(v))
80                     return 1;
81         }
82     }
83     c[u]=1;
84     return 0;
85 }

转载于:https://www.cnblogs.com/wenzhixin/p/9337913.html

UVa 1572 Self-Assembly (拓扑排序)相关推荐

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

    题意:给你n个点.m个关系,每个关系两个点u.v,表示u小于v,叫你输出任意一个序列保证满足所有给定的关系 例如:n=3 m=2 1 2 3 1 3 2 3 1 2 题解:拓扑排序排的是一个有向无环图 ...

  2. 【UVA 437】The Tower of Babylon(拓扑排序+DP,做法)

    [Solution] 接上一篇,在处理有向无环图的最长链问题的时候,可以在做拓扑排序的同时,一边做DP; 设f[i]表示第i个方块作为最上面的最高值; f[y]=max(f[y],f[x]+h[y]) ...

  3. Ordering Tasks UVA - 10305(拓扑排序)

    在一个有向图中,对所有的节点进行排序,要求没有一个节点指向它前面的节点. 先统计所有节点的入度,对于入度为0的节点就可以分离出来,然后把这个节点指向的节点的入度减一. 一直做改操作,直到所有的节点都被 ...

  4. python 拓扑排序_拓扑排序(topsort)算法详解

    在图论中,由某个集合上的偏序得到全序的策略就是拓补排序算法.拓扑排序常出现在涉及偏序关系的问题中,例如时序的先后.事物的依赖等.针对这些问题拓扑排序通常能有效地给出可行解. 为了便于理解,我们先来看一 ...

  5. 【图论】有向无环图的拓扑排序

    1. 引言 有向无环图(Directed Acyclic Graph, DAG)是有向图的一种,字面意思的理解就是图中没有环.常常被用来表示事件之间的驱动依赖关系,管理任务之间的调度.拓扑排序是对DA ...

  6. C#实现有向无环图(DAG)拓扑排序

    对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序列中出现在 ...

  7. hdu 5438 Ponds 拓扑排序

    Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...

  8. usaco frame up(所有拓扑排序的输出)

    先根据图建图再拓扑排序. /** ID: jinbo wu TASK: frameup LANG:C++ */ #include<bits/stdc++.h> using namespac ...

  9. HDU1811 Rank of Tetris 拓扑排序+并查集 OR 差分约束最短路+并查集

    题目链接 题意:就是给你一堆关系,看能不能排出个确定的顺序 做法: 1. 拓扑排序+并查集 应该很容易想到的一种思路,大于小于建立单向边.对于相等的呢,就把他们缩成一个点.就用并查集缩成一个点就行了 ...

最新文章

  1. web服务器(IIS)的操作步骤
  2. java stringbuilder appendline_StringBuilder
  3. java创建文件和目录
  4. 知道不知道 (刘若英演唱歌曲)
  5. SAP Spartacus B2B 页面 Popover Component 的条件显示逻辑
  6. 文件包含——本地无视后缀(二)
  7. 伯纳德•罗森伯格先生参加华为技术2016首届国际光电连接技术研讨会
  8. python decimal 转 float_python教程之二python数学运算
  9. 2007-2019中国城市竞争力排行榜Top10,你的家乡上榜了嘛?
  10. UML建模系列文章总结 [转]
  11. 中国芯片人才大军在哪里?
  12. 职场调侃:工作五年之后的十三种痛!
  13. spring源码:循环依赖源码学习
  14. 2019年1月30日
  15. Unity实现鼠标拾取电脑屏幕指定区域像素点颜色
  16. 【分布式机器学习】基本知识
  17. Python:实现Gale-Shapley盖尔-沙普利算法(附完整源码)
  18. 小米手机连接电脑(Mac)刷miui12开发版的操作指南
  19. android 自定义控件之AutoCompleteTextView邮箱后缀自动补全
  20. python批量分割音频-无bug完美运行

热门文章

  1. 谷歌Guava工具类的使用(1):BloomFilter的使用
  2. 2019 AI算法岗求职攻略
  3. 蓝牙设备连接失败或很慢
  4. ant入门指南—web前端开发七武器(1)
  5. Java自学道路心得体会
  6. 小马哥-----高仿机修复摄像头实例说明 其他机型可借鉴
  7. 四天后你就是应用分子动力学软件(NAMD、AMBER)的神!
  8. 经典Bug永流传---每周一“虫”(二十二)
  9. h5支付不能打开支付宝 ios_iOS支付宝支付(Alipay)详细接入流程以及项目中遇到的问题分析...
  10. 使用VS TFS源码分析软件PATFS在Team Explorer中检查Incidents事件