uvaLA4255 Guess BFS+拓扑排序
算法指南白书
思路:“连续和转化成前缀和之差”
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int maxn=;
int T,n;
int fa[];
char str[][],s[];
int G[][];
int c[];
vector<int>topo; int findset(int x){//并查集求祖先
return fa[x]!=x?fa[x]=findset(fa[x]):x;
}
bool dfs(int u){//拓扑排序
c[u]=-;
for(int i=;i<=n;i++){
if(G[u][i]){
if(c[i]<)
return false;
if(c[i]==)
dfs(i);
}
}
c[u]=;
topo.push_back(u);
return true;
}
bool toposort(){
topo.clear();
memset(c,,sizeof(c));
for(int i=;i<=n;i++){
if(!c[i]){
if(!dfs(i))
return false;
}
}
reverse(topo.begin(),topo.end());
return true;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
getchar();
scanf("%s",s);
int index=;
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=n;i++){
for(int j=i;j<=n;j++){
str[i][j]=s[index++];
if(str[i][j]=='')
fa[j]=i-;//i-1和j同祖先
}
}
memset(G,,sizeof(G));
for(int i=;i<=n;i++){//sum[i]<sum[j],i到j连一条边
for(int j=i;j<=n;j++){
if(str[i][j]=='-')
G[findset(j)][findset(i-)]=;
if(str[i][j]=='+')
G[findset(i-)][findset(j)]=;
}
}
toposort();
int sum[];
memset(sum,,sizeof(sum));
int cur=;
for(int i=;i<=n;i++){//0到。。。赋值
sum[topo[i]]=cur++;
}
for(int i=;i<=n;i++){
sum[i]=sum[findset(i)];
if(i>)
printf(" ");
printf("%d",sum[i]-sum[i-]);
}
printf("\n");
}
return ;
}
uvaLA4255 Guess BFS+拓扑排序的更多相关文章
- C. Journey bfs 拓扑排序+dp
C. Journey 补今天早训 这个是一个dp,开始我以为是一个图论,然后就写了一个dij和网络流,然后mle了,不过我觉得如果空间开的足够的,应该也是可以过的. 然后看了题解说是一个dp,这个dp ...
- hihoCoder1343 : Stable Members【BFS拓扑排序】
题目链接:https://hihocoder.com/problemset/problem/1343 #1343 : Stable Members 时间限制:10000ms 单点时限:1000ms 内 ...
- CH 2101 - 可达性统计 - [BFS拓扑排序+bitset状压]
题目链接:传送门 描述 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. 输入格式 第一行两个整数N,M,接下来M行每行两个整数x,y,表示从x到y的一条 ...
- hihocoder 1174 [BFS /拓扑排序判断是否有环]
hihocoder 1174 [算法]: 计算每一个点的入度值deg[i],这一步需要扫描所有点和边,复杂度O(N+M). 把入度为0的点加入队列Q中,当然有可能存在多个入度为0的点,同时它们之间也不 ...
- Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Description I ...
- 【ZOJ - 3780】 Paint the Grid Again (拓扑排序)
Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...
- BFS (1)算法模板 看是否需要分层 (2)拓扑排序——检测编译时的循环依赖 制定有依赖关系的任务的执行顺序 djkstra无非是将bfs模板中的deque修改为heapq
BFS模板,记住这5个: (1)针对树的BFS 1.1 无需分层遍历 from collections import deque def levelOrderTree(root): if not ro ...
- hdu1532 用BFS求拓扑排序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目给出一些点对之间的先后顺序,要求给出一个字典序最小的拓扑排列.对于拓扑排序的问题,我们有DF ...
- 图论相关知识(DFS、BFS、拓扑排序、最小代价生成树、最短路径)
图的存储 假设是n点m边的图: 邻接矩阵:很简单,但是遍历图的时间复杂度和空间复杂度都为n^2,不适合数据量大的情况 邻接表:略微复杂一丢丢,空间复杂度n+m,遍历图的时间复杂度为m,适用情况更广 前 ...
随机推荐
- caffe源码阅读(3)-Datalayer
DataLayer是把数据从文件导入到网络的层,从网络定义prototxt文件可以看一下数据层定义 layer { name: "data" type: "Data&qu ...
- Android中View绘制流程以及invalidate()等相关方法分析(转载的文章,出处在正文已表明)
转载请注明出处:http://blog.csdn.net/qinjuning 前言: 本文是我读<Android内核剖析>第13章----View工作原理总结而成的,在此膜拜下作者 .同时 ...
- ubuntu 12.04 mysql转移目录后 无法 启动
http://www.boyunjian.com/do/article/snapshot.do?uid=com.iteye.xgbjmxn%2Fblog%2F1208086(转,) 我是用ap ...
- PHP文件下载方式
<?php// 不能是中文目录,其实如果是 .rar, .zip 类的这些文件,直接连接就可以下载了!function download($file_dir,$file_name)//参数说明: ...
- javascript 节点属性详解
javascript 节点属性详解 根据 DOM,html 文档中的每个成分都是一个节点 DOM 是这样规定的:整个文档是一个文档节点每个 html 标签是一个元素节点包含在于 html 元素中的文本 ...
- tr设置border无效的解决方法
给table的css添加属性:border-collapse: collapse; 边框不折叠的表格 行,列,行组是不具有border的
- JBPM4中常用概念总结
1. 流程定义(Process Definition) 流程定义是记录在xml文件中的对流程的描述,它包含唯一的流程开始节点和多个流程功能节点,每个节点之间使用transition进行连接. P ...
- KeepAlive详解
KeepAlive既熟悉又陌生,踩过坑的同学都知道痛.一线运维工程师踩坑之后对于KeepAlive的总结,你不应该错过! 最近工作中遇到一个问题,想把它记录下来,场景是这样的: 从上图可以看出,用户通 ...
- RegexKitLite 使用详解
1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中. 2.工程中添加libicucore.dylib frameworks. 友情提醒:一般 ...
- Word2003中如何使封面和目录中不插入页码
Word2003中如何使封面和目录中不插入页码?? 转载自: http://blog.zzedu.net.cn/user1/zhaoweijie/archives/2010/187266.html ...