UVA-1572 Self-Assembly (图+拓扑排序)
题目大意:每条边上都有标号的正方形,两个正方形能通过相匹配的边连接起来,每种正方形都有无限多个。问能否无限延展下去。
题目分析:将边视为点,正方形视为边,建立无向图,利用拓扑排序判断是图否为DAG。
代码如下:
# include<iostream>
# include<cstdio>
# include<map>
# include<queue>
# include<string>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std; int vis[55],mp[55][55]; int get(char a,char b)
{
return (a-'A')*2+((b=='+')?1:0);///因为在这没注意运算符的优先级,一直WA。。。。。。
} void f(char *p)
{
for(int i=0;i<8;i+=2){
if(p[i]=='0') continue;
for(int j=0;j<8;j+=2){
if(i==j||p[j]=='0') continue;
int a=get(p[i],p[i+1])^1;
int b=get(p[j],p[j+1]);
mp[a][b]=1;
}
}
} bool dfs(int u)
{
vis[u]=-1;
for(int i=0;i<52;++i){
if(!mp[u][i])
continue;
if(vis[i]==-1)
return true;
if(!vis[i]&&dfs(i))
return true;
}
vis[u]=1;
return false;
} bool judge()
{
memset(vis,0,sizeof(vis));
for(int i=0;i<52;++i)
if(!vis[i]&&dfs(i))
return true;
return false;
} int main()
{
int n;
char p[9];
while(scanf("%d",&n)==1)
{
memset(mp,0,sizeof(mp));
while(n--)
{
scanf("%s",p);
f(p);
}
if(judge())
printf("unbounded\n");
else
printf("bounded\n");
}
return 0;
}
UVA-1572 Self-Assembly (图+拓扑排序)的更多相关文章
- UVa 1572 Self-Assembly (构造+拓扑排序。。。。。)
		
题意:给定n个带标号的正方形,标号要么是一个大写字母加一个+或-,要么是00, 当且仅当大写字母相同并且符号相反时可以连接,问你给定的能不能拼成一个无限大的的东西. 析:说实话,真心没有看出来是拓扑排 ...
 - HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)
		
逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...
 - POJ3687——Labeling Balls(反向建图+拓扑排序)
		
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
 - Bzoj 1565: [NOI2009]植物大战僵尸  最大权闭合图,拓扑排序
		
题目: http://cojs.tk/cogs/problem/problem.php?pid=410 410. [NOI2009] 植物大战僵尸 ★★★ 输入文件:pvz.in 输出文件:p ...
 - BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP
		
BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP Description 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建 一个水上乐园. ...
 - BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序
		
BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序 Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息 ...
 - 【BZOJ-2938】病毒      Trie图 + 拓扑排序
		
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
 - 图——拓扑排序(uva10305)
		
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
 - UVa 872 - Ordering 输出全拓扑排序
		
本题要求输出所有拓扑排序的序列. 还好本题的数据量不是非常大.限制在26个大写英文字母,故此能够使用递归法输出. 这个递归输出所有解在Leetcode非常多这种题目的,不小心的话,还是非常难调试的. ...
 - poj 3683  2-sat建图+拓扑排序输出结果
		
发现建图的方法各有不同,前面一题连边和这一题连边建图的点就不同,感觉这题的建图方案更好. 题意:给出每个婚礼的2个主持时间,每个婚礼的可能能会冲突,输出方案. 思路:n个婚礼,2*n个点,每组点是对称 ...
 
随机推荐
- sql 中如何将返回的记录某一条置顶
			
将table1中id 为2的记录置顶select * from table1order by case when id='2' then 0 else 1 end 例子:将已发布的置顶,status ...
 - AutoLayout性能不如frame
			
http://draveness.me/layout-performance.html 复杂视图, 数量超过30个,用autoLayout就比较卡顿了 发现首页类似朋友圈,卡顿的原因应该就是使用了au ...
 - talib 中文文档(三):talib 方法大全
			
Function API Examples Similar to TA-Lib, the function interface provides a lightweight wrapper of th ...
 - AOP切点表达式
			
Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式 execu ...
 - Grid Search学习
			
转自:https://www.cnblogs.com/ysugyl/p/8711205.html Grid Search:一种调参手段:穷举搜索:在所有候选的参数选择中,通过循环遍历,尝试每一种可能性 ...
 - 使用jackson工具类把对象或集合转为JSON格式
			
jackson使用方法: 1.加入jar包: jackson-annotations-2.2.2.jar jackson-core-2.2.2.jar jackson-databind-2.2.2.j ...
 - docker——安全防护与配置
			
Docker是基于Linux操作系统实现的应用虚拟化.运行在容器内的进程,跟运行在本地系统的进程本质上并无区别,配置不合适的安全策略将可能给本地系统带来安全风险,因此,Docker的安全性在生产环境中 ...
 - Java用数据结构解决实现问题之数学问题
			
有趣的整数: 完数:如果一个数字恰好等于他的因子之和,就叫做完数,需求是求出10000以内的所有的完数. 解法:1.用n去除以1-n之间的所有的整数,将能整除的被除数保存到一个数组中,作为n的一个因子 ...
 - poj1329 Circle Through Three Points
			
地址:http://poj.org/problem?id=1329 题目: Circle Through Three Points Time Limit: 1000MS Memory Limit: ...
 - C#反射——模仿ParameterInterceptor(ashx处理程序)
			
反射工具类请参见:https://www.cnblogs.com/threadj/p/10535796.html using System; using System.Collections.Gene ...