Consider a directed graph G of N nodes and all edges (uv) 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

Input
3
3 2
1 3
2 3
4 0
4 2
1 2
1 3
Output
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的更多相关文章

  1. hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  2. CF798E. Mike and code of a permutation [拓扑排序 线段树]

    CF798E. Mike and code of a permutation 题意: 排列p,编码了一个序列a.对于每个i,找到第一个\(p_j > p_i\)并且未被标记的j,标记这个j并\( ...

  3. Luogu5289 十二省联考2019字符串问题(后缀数组+拓扑排序+线段树/主席树/KDTree)

    先考虑80分做法,即满足A串长度均不小于B串,容易发现每个B串对应的所有A串在后缀数组上都是一段连续区间,线段树优化连边然后判环求最长链即可.场上就写了这个. 100分也没有什么本质区别,没有A串长度 ...

  4. Nowcoder Hash Function ( 拓扑排序 && 线段树优化建图 )

    题目链接 题意 : 给出一个哈希表.其避免冲突的方法是线性探测再散列.现在问你给出的哈希表是否合法.如果合法则输出所有元素插入的顺序.如果有多解则输出字典序最小的那一个.如果不合法则输出 -1 分析 ...

  5. P3588 [POI2015]PUS(拓扑排序+线段树)

    P3588 [POI2015]PUS 对于每个$(l,r,k)$,将$k$个位置向剩下$r-l-k+1$个位置连边,边权为$1$,这样就保证$k$个位置比剩下的大 先给所有位置填$1e9$保证最优 然 ...

  6. hdu 5638 Toposort (拓扑排序+线段树)

    Toposort Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  7. 【BZOJ-3832】Rally 拓扑序 + 线段树 (神思路题!)

    3832: [Poi2014]Rally Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 168  Solved:  ...

  8. Codeforces 558E A Simple Task (计数排序&&线段树优化)

    题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...

  9. BZOJ4552:[TJOI2016&HEOI2016]排序(线段树,二分)

    Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题,需要你来帮助他. 这个难题是这样子的:给出一个1到n的全排列,现在对这 ...

随机推荐

  1. 对比C#中==与equal方法

    C#中equal与==的区别 收藏 对于值类型,如果对象的值相等,则相等运算符 (==) 返回 true,否则返回 false.对于string 以外的引用类型,如果两个对象引用同一个对象,则 == ...

  2. MyEclipse9,MyEclipse10 安装ADT

    Eclipse安装ADT 时步骤是开 Eclipse IDE,进入菜单中的 "Help" -> "Install New Software" ,点击Add ...

  3. 2016沈阳网络赛 QSC and Master

    QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  4. 蛙人(ple)

    蛙人(ple) 题目描述 蛙人使用特殊设备潜水.设备中有一个气瓶,分两格:一格装氧气,另一格装氮气.留在水中有时间的限制,在深水中需要大量的氧气与氮气.为完成任务,蛙人必须安排好气瓶.每个气瓶可以用它 ...

  5. 转:创建编码的WebTest

    创建编码的WebTest•通常,通过将现有的已记录Web测试转换为编码的Web测试来创建编码的Web测试.记录的Web测试以“Web测试编辑器”中可见的请求树开头.编码的Web测试是一个生成一系列We ...

  6. HDU 5170 GTY's math problem

    数学题,a的b次方和c的d次方都很大,直接判断是做不出来的. 如果我们能找到一个函数F(x)是单调的,而F(X)的值又比较好算,那么可以通过比较F(X)的大小来判断自变量的大小. 令F(X)=log( ...

  7. java中单例设计模式

    在java中创建单例的方式主要有三种:饿汉式.懒汉式.登记式.以下内容均是摘抄自 http://blog.csdn.net/jason0539/article/details/23297037/ 一. ...

  8. 合并BIN文件的两种方法(转)

    源:http://blog.chinaunix.net/uid-20745340-id-1878803.html 合并BIN文件的两种方法 在单片机的开发过程中,经常需要将两个单独的BIN文件合并成一 ...

  9. Restful based service 的跨域调用

    1.关于跨域, w3c的官方文档:https://www.w3.org/TR/cors/ 2.有时间再整理吧. <html> <head> <script src=&qu ...

  10. Netbeans 6.8 + apktool_2.0.0b9 动态调试smali文件

    前言 很早就知道用Netbeans能够单步调试smali,一直拖到现在才真正的自己实现了一次~ 下面是详细步骤! 0×1 环境及工具 a.apktool_2.0.0b9 下载地址:http://con ...