算法指南白书

思路:“连续和转化成前缀和之差”

 #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+拓扑排序的更多相关文章

  1. C. Journey bfs 拓扑排序+dp

    C. Journey 补今天早训 这个是一个dp,开始我以为是一个图论,然后就写了一个dij和网络流,然后mle了,不过我觉得如果空间开的足够的,应该也是可以过的. 然后看了题解说是一个dp,这个dp ...

  2. hihoCoder1343 : Stable Members【BFS拓扑排序】

    题目链接:https://hihocoder.com/problemset/problem/1343 #1343 : Stable Members 时间限制:10000ms 单点时限:1000ms 内 ...

  3. CH 2101 - 可达性统计 - [BFS拓扑排序+bitset状压]

    题目链接:传送门 描述 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. 输入格式 第一行两个整数N,M,接下来M行每行两个整数x,y,表示从x到y的一条 ...

  4. hihocoder 1174 [BFS /拓扑排序判断是否有环]

    hihocoder 1174 [算法]: 计算每一个点的入度值deg[i],这一步需要扫描所有点和边,复杂度O(N+M). 把入度为0的点加入队列Q中,当然有可能存在多个入度为0的点,同时它们之间也不 ...

  5. 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 ...

  6. 【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 ...

  7. BFS (1)算法模板 看是否需要分层 (2)拓扑排序——检测编译时的循环依赖 制定有依赖关系的任务的执行顺序 djkstra无非是将bfs模板中的deque修改为heapq

    BFS模板,记住这5个: (1)针对树的BFS 1.1 无需分层遍历 from collections import deque def levelOrderTree(root): if not ro ...

  8. hdu1532 用BFS求拓扑排序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目给出一些点对之间的先后顺序,要求给出一个字典序最小的拓扑排列.对于拓扑排序的问题,我们有DF ...

  9. 图论相关知识(DFS、BFS、拓扑排序、最小代价生成树、最短路径)

    图的存储 假设是n点m边的图: 邻接矩阵:很简单,但是遍历图的时间复杂度和空间复杂度都为n^2,不适合数据量大的情况 邻接表:略微复杂一丢丢,空间复杂度n+m,遍历图的时间复杂度为m,适用情况更广 前 ...

随机推荐

  1. 学习笔记---C++伪函数(函数对象)

    C++里面的伪函数(函数对象)其实就是一个类重载了()运算符,这样类的对象在使用()操作符时,看起来就像一个函数调用一样,这就叫做伪函数. class Hello{ public: void oper ...

  2. Maven多层嵌套

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  3. libz.so库分析

    from:http://blog.chinaunix.net/uid-12773189-id-84605.html 1.查看库文件是由哪个软件包提供的空闲时打开/usr/lib目录(因为我知道这个目录 ...

  4. 【原创】QT编程 多线程

    请先保证已安装QT,没有请参考 http://www.cnblogs.com/kavs/p/4608926.html  安装QT. 新建threads文件夹存放项目:mkdir threads sud ...

  5. struts2请求过程源码分析(转)

    Struts2是Struts社区和WebWork社区的共同成果,我们甚至 可以说,Struts2是WebWork的升级版,他采用的正是WebWork的核心,所以,Struts2并不是一个不成熟的产品, ...

  6. usb host和usb device

    S3C2440的数据手册将USB功能分为两章--usb host和usb device.具体什么意思呢? usb host: 微处理器作为usb主设备,可以挂接U盘之类的从属设备. usb devic ...

  7. RESTheart安装与设置

    作者:Maurizio Turatti, 最后在Feb 25, 2016时被 Andrea Di Cesare更新 安装与设置 1. 快速开始 Docker Vagrant Bare metal 2. ...

  8. Win7系统Matlab2013a安装.m文件不自动关联到MATLAB.exe解决方法

    1.在matlab命令行中输入以下代码: cwd=pwd; cd([matlabroot '\toolbox\matlab\winfun\private']); fileassoc('add',{'. ...

  9. 【网络流24题】No.9 方格取数问题 (二分图点权最大独立集)

    [题意] 在一个有 m*n 个方格的棋盘中, 每个方格中有一个正整数. 现要从方格中取数, 使任意 2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法. 输入文件示例inp ...

  10. 【HDU 3709】 Balanced Number (数位DP)

    Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...