【拓扑排序】【线段树】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的全排列,现在对这 ...
随机推荐
- ListView使用的时候遇到的一些问题
昨天在做项目时,请求服务器的好友动态后,将好友动态和评论显示到界面上,用ListView显示,发现一进这个界面时,listView的适配器的getVIew()方法就会执行6次,后来发现原来是ListV ...
- Sichuan State Programming Contest 2012 C。Counting Pair
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=118254#problem/C 其实这道题目不难...就是没有仔细分析... 我们可以发现 ...
- css脱离文档流
作者:张秋怡链接:http://www.zhihu.com/question/24529373/answer/29135021来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...
- C#入门经典第六章函数-2-委托
委托:
- string转化为int方法
int intA = 0; 1.intA =int.Parse(str); 2.int.TryParse(str, out intA); 3.intA = Convert.ToInt32(str);以 ...
- CentOS 6.2 安装vsftpd 服务器(转)
CentOS 6.2 安装vsftpd 服务器 本人的CentOS 6.2是安装在win 2008 R2 server 的 Hyper-V 虚拟机中.centos使用光盘安装,以最小模式安装,完成后用 ...
- BootStrap中关于input-group的问题(未解决)
****************************************2016年1月6日 23:08******************************* 本来想实现的如下功能: 但 ...
- Windows Server 2012 在个人终端上使用的推荐设置
Windows Server 2012,也就是 Windows 8 的服务器版本,相对于 Windows 8 企业版而言,增强了作为服务器的功能,弱化了作为终端系统的功能. 目前微软官方提供了 Win ...
- CodeForces 614B Gena's Code
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...
- [算法] aov图拓扑算法
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> ...