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,适用情况更广 前 ...
随机推荐
- bzoj2732: [HNOI2012]射箭 半平面交
这题乍一看与半平面交并没有什么卵联系,然而每个靶子都可以转化为两个半平面. scanf("%lf%lf%lf",&x,&ymin,&ymax); 于是乎就有 ...
- Microsoft Windows Installer 工具 Msiexec.exe 的命令行选项
摘自:http://support.microsoft.com/kb/314881/zh-cn 概要 本文列出了 Windows Installer 工具 Msiexec.exe 的命令行选项.Msi ...
- spring拦截器
一:拦截器配置 <!-- 拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path=&qu ...
- Dataset
1,if(ds == null) 这是判断内存中的数据集是否为空,说明DATASET为空,行和列都不存在!! 2,if(ds.Tables[0].Count == 0) 这应该是在内存中存在一个DAT ...
- C语言2048
这段时间google上有个小游戏挺火的,我也很喜欢,业余时间做个C语言版的. 老规矩先上干货: http://files.cnblogs.com/GhostZCH/2048.rar (.c & ...
- python多重继承:
除了从一个父类继承外,Python允许从多个父类继承,称为多重继承. 多重继承的继承链就不是一棵树了,它像这样: class A(object): def __init__(self, a): pri ...
- hdu 4752
计算几何+数值计算的题目: 要用到辛普森积分,没有学过~~~ 参考学习了acm_Naruto大神 的代码! 代码: #include<cstdio> #include<cmath&g ...
- 分析ECMall的注册与登录机制
ecmall的注册流程index.php?app=member&act=register. 首先app是member,act是register方法. index.php中.通过ecmall的s ...
- linux 监控
http://www.iyunv.com/thread-50606-1-1.html http://segmentfault.com/a/1190000002537665 http://blog.cs ...
- Docker日志自动化: ElasticSearch、Logstash、Kibana以及Logspout
http://www.open-open.com/lib/view/open1432107136989.html