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, 当且仅当大写字母相同并且符号相反时可以连接,问你给定的能不能拼成一个无限大的的东西. 析:说实话,真心没有看出来是拓扑排 ...
随机推荐
- Python自动化开发 - 常用模块(一)
本节内容 1.模块介绍 2.time&datetime模块 3.random模块 4.os模块 5.sys模块 6.json&pickle模块 7.logging模块 一.模块介绍 模 ...
- centos7 搭建WEB服务器
centos7 搭建WEB服务器 2017年09月17日 09:44:50 逝然1994 阅读数:18321 标签: centosapacheweb服务器 更多 个人分类: centos服务器简单配置 ...
- C# windform 使用TreeGridView
1 下载 treeGridView.DLL库文件. 2 添加到工程中. 右键“工具箱”选择“选择项” 弹出对话框 选择“浏览” 选中下载还的dll库文件.完成后工具箱中会有 treeGridView控 ...
- WPF实战案例-打印
在前段时间做了一下打印,因为需要支持的格式比较多,所以wpf能打印的有限分享一下几种格式的打印(.xls .xlsx .doc .docx .png .jpg .bmp .pdf) 首先为了保证exc ...
- PowerDesigner的Name和Code不同步设置
1.“Tools”->"GeneralOptions"(最下方) 2.“Dialog”(左侧列表选第2个) 3.“Name to Code mirroring”的勾去掉
- Swift5 语言参考(六) 声明
一个声明引入了一个新的名称或构建到你的程序.例如,您使用声明来引入函数和方法,引入变量和常量,以及定义枚举,结构,类和协议类型.您还可以使用声明来扩展现有命名类型的行为,并将符号导入到其他地方声明的程 ...
- JQuery 限制文本输入只能输入数字(可自定义正则表达式)
var JVerify = { role: { number: /[0-9\/]/, decimal: /[0-9\.\/]/, code: /[0-9A-Z]/ }, Verify: functio ...
- vue教程3-01 路由、组件、bower包管理器使用
vue教程3-01 路由.组件.包管理器 以下操作前提是 已经安装好node.js npm bower-> (前端)包管理器 下载: npm install bower -g 验证: bower ...
- google浏览器高清壁纸保存
谷歌浏览器 扩展程序里边 有一个主题壁纸 好多不错的,并且是高清大图!!! 主题应用市场: https://chrome.google.com/webstore/category/themes?hl= ...
- vs2017 对dockerfile的支持
项目添加 dockerfile Docker file 内容 FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base WORKDIR /app EXP ...