UVA - 1572 Self-Assembly(图论模型+拓扑排序)
题意:判断利用给出的正方形是否能拼接出无限延伸的结构。
分析:正方形上的字母看做点,正方形看做有向边。
例如:

若上下两个正方形能拼接,需要B+~C+是个有向边。
对输入的处理是:把A+,A-分别映射成2n+1,2n,利用(2n)^1 = 2n+1 , (2n+1)^1 = 2n 的性质处理有向边。
若存在有向环则unbounded,即不存在拓扑排序
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
char s[];
int pic[MAXN][MAXN];
int vis[MAXN];
priority_queue<int> q;
int get_id(char a, char b){
return b == '+' ? * (a - 'A') + : * (a - 'A');
}
bool dfs(int x){
vis[x] = -;
for(int i = ; i < ; ++i){
if(pic[x][i]){
if(vis[i] == -) return false;
else if(!vis[i] && !dfs(i)) return false;
}
}
vis[x] = ;
return true;
}
bool toposort(){
for(int i = ; i < ; ++i){
if(!vis[i] && !dfs(i)){
return false;
}
}
return true;
}
int main(){
int n;
while(scanf("%d", &n) == ){
memset(pic, , sizeof pic);
memset(vis, , sizeof vis);
while(n--){
scanf("%s", s);
for(int i = ; i < ; ++i){//处理除了00之外的点之间的有向边的关系
for(int j = ; j < ; ++j){
if(i != j && s[i * ] != '' && s[j * ] != ''){
int from = get_id(s[i * ], s[i * + ]) ^ ;
int to = get_id(s[j * ], s[j * + ]);
pic[from][to] = ;
}
}
}
}
bool ok = toposort();
if(ok){
printf("bounded\n");
}
else{
printf("unbounded\n");
}
}
return ;
}
UVA - 1572 Self-Assembly(图论模型+拓扑排序)的更多相关文章
- 图论之拓扑排序 poj 2367 Genealogical tree
题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...
- 图论之拓扑排序 poj1128 Frame Stacking
题目网址 http://poj.org/problem?id=1128 思路:遍历找出每一种字母出现的最大和最小的横纵坐标,假如本应出现字母A的地方出现了字母B,那么A一定在字母B之前,这就相当于点A ...
- UVA 10305:Ordering Tasks(拓扑排序)
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...
- tsort - 拓扑排序
tsort - 拓扑排序 本文链接:http://codingstandards.iteye.com/blog/834572 (转载请注明出处) 用途说明 tsort命令通常用于解决一种逻辑问题, ...
- 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 1572 Self-Assembly (构造+拓扑排序。。。。。)
题意:给定n个带标号的正方形,标号要么是一个大写字母加一个+或-,要么是00, 当且仅当大写字母相同并且符号相反时可以连接,问你给定的能不能拼成一个无限大的的东西. 析:说实话,真心没有看出来是拓扑排 ...
- uvalive 6393(uva 1572) Self-Assembly 拓扑排序
题意: 给出一些正方形,这些正方形的每一条边都有一个标号.这些标号有两种形式:1.一个大写字母+一个加减号(如:A+, B-, A-......), 2.两个0(如:00):这些正方形能够任意翻转和旋 ...
- 拓扑排序的 +Leapms 线性规划模型
知识点 拓扑排序 拓扑排序的+Leapms模型 无圈有向图 一个图G(V,E), 如果边有向且不存在回路,则为无圈有向图.在无圈有向图上可以定义拓扑排序.下图是一个无圈有向图的例子. 拓扑排序 给定一 ...
随机推荐
- Centos7 nginx的负载均衡概念与配置
一.负载均衡概念 负载均衡(Server Load Balancer)是将访问流量根据转发策略分发到后端多台 ECS 的流量分发控制服务.负载均衡可以通过流量分发扩展应用系统对外的服务能力,通过消除单 ...
- C语言的常用的数据类型有哪些_所占字节分别是多少
整型 整形打印使用%d short:短整型,占16位,2个字节 int:占32位,4个字节 long:长整型,占4个字节,本来意思比int更多,但是目前来看基本都是和int一样 浮点型 浮点型计算会影 ...
- Lesson 12 banks and their customers
Why is there no risk to the customer when a bank prints the customer's name on his cheques? When any ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:清除浮动
<!DOCTYPE html> <html> <head> <title>Bootstrap .clearfix 实例</title> &l ...
- hibernate中简单的增删改查
项目的整体结构如下 1.配置文件 hibernate.cfg.xml <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hi ...
- ActivePerl 安装
下载 https://www.activestate.com/products/activeperl/downloads/ 链接:https://pan.baidu.com/s/1IXPdYFd5bD ...
- Lesson 45 Of men and galaxies
In man's early days, competition with other creatures must have been critical. But this phase of our ...
- 【Unity】双击物体
using UnityEngine; using System.Collections; using System; public class Click_yushe : MonoBehaviour ...
- NO17 第二关考试: 返回上次目录和ls -lrt倒序看文件--删除7天前的日志--查看日志更新--记录行号
第二题:不用cd /ildboy命令如何回到上一次的目录: 假如当前目录是: [root@localhost oldboy]# pwd/oldboy现在因为需要进入到了/tmp目录下进行操作,执行的命 ...
- P1087 有多少不同的值
P1087 有多少不同的值 转跳点: