【拓扑排序】【线段树】Gym - 101102K - Topological Sort
Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this graph doesn’t contain any cycles.
Your task is to find the lexicographically largest topological sort of the graph after removing a given list of edges.
A topological sort of a directed graph is a sequence that contains all nodes from 1 to N in some order such that each node appears in the sequence before all nodes reachable from it.
Input
The first line of input contains a single integer T, the number of test cases.
The first line of each test case contains two integers N and M (1 ≤ N ≤ 105) , the number of nodes and the number of edges to be removed, respectively.
Each of the next M lines contains two integers a and b (1 ≤ a < b ≤ N), and represents an edge that should be removed from the graph.
No edge will appear in the list more than once.
Output
For each test case, print N space-separated integers that represent the lexicographically largest topological sort of the graph after removing the given list of edges.
Example
3
3 2
1 3
2 3
4 0
4 2
1 2
1 3
3 1 2
1 2 3 4
2 3 1 4 把普通的拓扑排序的栈操作改成线段树区间减一,查询区间最右侧的0的位置即可。注意一开始就删除的边,在区间减后要单点加回来。
然后当前的点处理完后,要把其置成无穷大。具体看代码。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010
int T,n,m,minv[N<<2],delta[N<<2];
void pushdown(int rt)//将rt结点的懒惰标记下传
{
if(delta[rt])
{
delta[rt<<1]+=delta[rt];//标记下传到左结点
delta[rt<<1|1]+=delta[rt];//标记下传到右结点
minv[rt<<1]+=delta[rt];
minv[rt<<1|1]+=delta[rt];
delta[rt]=0;//清除rt结点的标记,下传完成
}
}
void update(int ql,int qr,int v,int rt,int l,int r)
{
if(ql<=l&&r<=qr)
{
delta[rt]+=v;//更新当前结点的标记值
minv[rt]+=v;
return ;
}
pushdown(rt);//将该节点的标记下传到孩子们
int m=(l+r>>1);
if(ql<=m) update(ql,qr,v,rt<<1,l,m);
if(m<qr) update(ql,qr,v,rt<<1|1,m+1,r);
minv[rt]=min(minv[rt<<1],minv[rt<<1|1]);
}
int query(int rt,int l,int r)
{
if(l==r)
return l;
int m=(l+r>>1);
pushdown(rt);
if(minv[rt<<1|1]==0) return query(rt<<1|1,m+1,r);
else return query(rt<<1,l,m);
}
int first[N],e,next[N],v[N];
void AddEdge(int U,int V)
{
v[++e]=V;
next[e]=first[U];
first[U]=e;
}
int del[N];
int main()
{
int x,y;
scanf("%d",&T);
for(;T;--T)
{
e=0;
memset(minv,0,sizeof(minv));
memset(delta,0,sizeof(delta));
memset(first,0,sizeof(first));
memset(next,0,sizeof(next));
memset(del,0,sizeof(del));
memset(v,0,sizeof(v));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i)
{
scanf("%d%d",&x,&y);
AddEdge(x,y);
++del[y];
}
for(int i=1;i<=n;++i)
update(i,i,i-1-del[i],1,1,n);
for(int i=1;i<=n;++i)
{
int U=query(1,1,n);
printf("%d%c",U,i==n ? '\n' : ' ');
update(U,U,n+1,1,1,n);
update(U+1,n,-1,1,1,n);
for(int j=first[U];j;j=next[j])
update(v[j],v[j],1,1,1,n);
}
}
return 0;
}
【拓扑排序】【线段树】Gym - 101102K - Topological Sort的更多相关文章
- hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- CF798E. Mike and code of a permutation [拓扑排序 线段树]
CF798E. Mike and code of a permutation 题意: 排列p,编码了一个序列a.对于每个i,找到第一个\(p_j > p_i\)并且未被标记的j,标记这个j并\( ...
- Luogu5289 十二省联考2019字符串问题(后缀数组+拓扑排序+线段树/主席树/KDTree)
先考虑80分做法,即满足A串长度均不小于B串,容易发现每个B串对应的所有A串在后缀数组上都是一段连续区间,线段树优化连边然后判环求最长链即可.场上就写了这个. 100分也没有什么本质区别,没有A串长度 ...
- Nowcoder Hash Function ( 拓扑排序 && 线段树优化建图 )
题目链接 题意 : 给出一个哈希表.其避免冲突的方法是线性探测再散列.现在问你给出的哈希表是否合法.如果合法则输出所有元素插入的顺序.如果有多解则输出字典序最小的那一个.如果不合法则输出 -1 分析 ...
- P3588 [POI2015]PUS(拓扑排序+线段树)
P3588 [POI2015]PUS 对于每个$(l,r,k)$,将$k$个位置向剩下$r-l-k+1$个位置连边,边权为$1$,这样就保证$k$个位置比剩下的大 先给所有位置填$1e9$保证最优 然 ...
- hdu 5638 Toposort (拓扑排序+线段树)
Toposort Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- 【BZOJ-3832】Rally 拓扑序 + 线段树 (神思路题!)
3832: [Poi2014]Rally Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 168 Solved: ...
- Codeforces 558E A Simple Task (计数排序&&线段树优化)
题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...
- BZOJ4552:[TJOI2016&HEOI2016]排序(线段树,二分)
Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题,需要你来帮助他. 这个难题是这样子的:给出一个1到n的全排列,现在对这 ...
随机推荐
- 【PDF单页转化为图片输出 注意:英文或图片类的PDF可转化,中文抛异常】
public static void main(String[] args) throws IOException { /** * PDF单页转化为图片输出 注意:英文或图片类的PDF可转化,中文抛异 ...
- 关于Application.Lock…Application.Unlock有什么作用?
因为Application变量里一般存储的是供所有连接到服务器的用户共享的信息(就像程序中所说的 "全局变量 "), 由于是全局变量,所以就容易出现两个或者多个用户同时对这一变量进 ...
- jaxb异常 Class has two properties of the same name username
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; im ...
- Qt5:随窗口大小变化背景图片自动缩放的实现
在窗口程序中,当我们改变窗口大小的时候,背景图片通常会岁窗口大小变化而缩放 然而,在我们写的窗口程序中,设置背景图片后,如果缩放大小,会看到背景图片并不会随之缩放, 应为这需要特殊处理,一般常用的方法 ...
- extjs最普通的grid
的 <script> Ext.onReady(function () { //1.定义Model Ext.define("MyApp.model.User", { ex ...
- C语言5种存储区域
C语言5种存储区域 转发至:http://www.mamicode.com/info-detail-927635.html 系统为了管理内存 把内存划分了几个区域 1> 栈区 栈区之中的数据在栈 ...
- SVG页面loading动态图
https://github.com/SamHerbert/SVG-Loaders demo http://samherbert.net/svg-loaders/
- Win7下 IIS+PHP(ZendLoader)+MySQL
这里使用的是傻瓜式安装方式 下载php-5.3.18-nts-Win32-VC9-x86.msi,Win7下可执行文件.(下载地址 http://pan.baidu.com/s/1qvJCA) 执行到 ...
- Camera服务之--架构浅析
Camera服务之--架构浅析 分类: Camera 分析2011-12-22 11:17 7685人阅读 评论(3) 收藏 举报 android硬件驱动框架jnilinux内核平台 一.应用层 Ca ...
- mybatis+spring事务
http://www.mybatis.org/spring/zh/transactions.html 第四章 事务 一个使用 MyBatis-Spring 的主要原因是它允许 MyBatis 参与到 ...