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), 如果边有向且不存在回路,则为无圈有向图.在无圈有向图上可以定义拓扑排序.下图是一个无圈有向图的例子. 拓扑排序 给定一 ...
随机推荐
- highcharts Ajax 动态请求加载数据
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- 嵌入式编程中使用 do{...} while(0) 的解释
最近在看esp32的idf,有一些宏定义使用了do while(0)这种看起来好像没啥用的代码.然后我查了一下资料,发现在linux内核代码中经常用到这个东西! 现在就将这个东西整理一下. 为什么在内 ...
- java面试题蚂蚁
hashmap结构:什么对象能做为key hashtable,concurrentHashMap,hashtable比较 String,StringBuilder,StringBuffer 对象的深浅 ...
- red hat 7、centos7的root密码破译
一.在开机画面时按"E". 二.找到linux16开头的这段,在段尾添加空格"rd.break"然后按Ctrl+x进入系统紧急救援模式. 三.新的界面出现命令行 ...
- Java 理论与实践: 哈希
有效和正确定义hashCode()和equals() 每个Java对象都有 hashCode() 和 equals() 方法.许多类 Override 这些方法的缺省实施,以在对象实例之间提供更深层次 ...
- 分析配置DispatcherServlet类时load-on-startup标签作用
<servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org. ...
- C#委托-事件示例
Program.cs using System;using System.Collections.Generic;using System.Linq;using System.Text;using S ...
- SpringCloud实战——(2)通过Feign调用其他模块
大型项目下往往有很多模块,ZCGL项目结构如下: 需要引用的其他模块已经发布成服务并在Eureka Server注册中心注册,如下: 写程序时引用了其他模块,并且其他模块在项目中,但是IDEA任然无法 ...
- 二、spring集成ibatis进行数据源事务管理拦截器环境配置
1.dataSource-applicationContext.xml文件配置理解:(spring1.2.8+ibatis1.5.3)1.1)配置数据源 DriverManagerDataSource ...
- Java面向对象之类、接口、多态
Java面向对象之类.接口.多态 类 class Person { // 实例属性 int age; String name; // 类属性 static int v = 1; // 构造器 publ ...