uvalive 6393(uva 1572) Self-Assembly 拓扑排序
题意:
给出一些正方形,这些正方形的每一条边都有一个标号。这些标号有两种形式:1.一个大写字母+一个加减号(如:A+, B-, A-......), 2.两个0(如:00);这些正方形能够任意翻转和旋转。当两个正方形通过旋转或翻转,使得他们的公共边为同样大写字母而且符号相反时,他们就能够彼此结合拼在一起。如今给出n中正方形。每种正方形有无限多种,问这些正方形是否能拼成一个无限大的结构。
题解:
easy想到。要使这些正方形形成无限大地结构。那么这些正方形通过拼接后一定能循环(即通过不断地拼接出现了和曾经同样地正方形),那么就能够通过推断将这些正方形地全部可能地拼接方式连有向边。然后推断是否有有向环,就可以通过拓扑排序来推断。
代码:
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 52 + 10;
int G[maxn][maxn], vis[maxn]; int ID(char a, char b)
{
return (a - 'A')*2 + (b == '+' ? 0 : 1);
}
void conect(char a1, char a2, char b1, char b2)
{
if (a1 == '0' || b1 == '0')
{
return ;
}
int u = ID(a1, a2)^1, v = ID(b1, b2);
G[u][v] = 1;
}
bool dfs(int u)
{
vis[u] = -1;
for (int v = 0; v < 52; v++) if (G[u][v])
{
if (vis[v] == -1) return true;
if (!vis[v] && dfs(v)) return true;
}
vis[u] = 1;
return false;
}
bool find_cycle()
{
memset(vis, 0, sizeof(vis));
for (int i = 0; i < 52; i++) if (!vis[i])
{
if (dfs(i)) return true;
}
return false;
}
int main()
{
// freopen("/Users/apple/Desktop/in.txt", "r", stdin);
int n;
while(scanf("%d", &n) == 1 && n)
{
memset(G, 0, sizeof(G));
while (n--)
{
char s[10]; scanf("%s", s);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++) if (i != j)
{
conect(s[i*2], s[i*2+1], s[j*2], s[j*2+1]);
}
}
}
if (find_cycle()) printf("unbounded\n");
else printf("bounded\n");
} return 0;
}
uvalive 6393(uva 1572) Self-Assembly 拓扑排序的更多相关文章
- UVA 1572 Self-Assembly(拓扑排序)
1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓 ...
- UVa 1572 Self-Assembly (拓扑排序)
题目链接: https://cn.vjudge.net/problem/UVA-1572 Automatic Chemical Manufacturing is experimenting with ...
- 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 ...
随机推荐
- RvmTranslator6.2 is released
RvmTranslator6.2 is released eryar@163.com RvmTranslator can translate the RVM file exported by AVEV ...
- POJ 3039 搜索??? (逼近)
思路: 抄的题解 这叫搜索? 难以理解 我觉得就是枚举+逼近 //By SiriusRen #include <cmath> #include <cstdio> #includ ...
- java为什么要定义接口等相关解释
1.接口的作用是实现多重继承 因为只能继承一个类(规定的) 2.一个类只能继承一个父类,但是可以实现一个或多个接口 3.abstract关键词能让你在类里创建一个或多个没有定义的方法—你给出接口,但 ...
- 【基础篇】Android MediaPlayer基本使用方式
使用MediaPlayer播放音频或者视频的最简单例子: JAVA代码部分: public class MediaPlayerStudy extends Activity { private Butt ...
- C++ 补课(一)
1,在C语言中,全局变量必须声明在所有的函数之前,局部变量必须声明在所有可执行语句之前: C++ 允许在代码块的任何位置对局部变量进行声明 2,常量定义方面,C语言 #define 可能因计算的优先级 ...
- jersey+jetty实现文件上传
服务配置与启动类 import org.glassfish.jersey.servlet.ServletContainer; import javax.ws.rs.Path; import org.e ...
- Linux下设置ip和主机名进行绑定
1:输入命令gedit /etc/hosts 这样你就打开了一个文本,然后在文本的末尾进行加入例如以下: ip地址 主机名 192.168.0.125 h ...
- OpenStack_Swift源代码分析——ObjectReplicator源代码分析(1)
1.ObjectorReplicator的启动 首先执行启动脚本 swift-init object-replicator start 此执行脚本的执行过程和ring执行脚本执行过程差点儿相同.找到s ...
- [NowCoder]牛客网NOIP赛前集训营-提高组(第六场)题解
A.最长路 题意:给定有向图,每条边有个字符\([0,10^9]\),求每个点最长路字典序最小的方案.\(N,M\le 10^6\) 建反图跑拓扑排序,显然入过队的点都有最长路,考虑如何判断字典序大小 ...
- weblogic12
http://www.oracle.com/technetwork/middleware/weblogic/downloads/index.html 建立ejb http://docs.oracle. ...