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

题意

给出N个点,每个对于每个对(u,v)若u<v,那么连一条边,给出m条边,先删除他们,问拓扑序字典序最大是多少。

考场上想了半天,大概最后想用最长路做,但是搞不定。。然后看题解嘛、。。 用线段树维护,操作有:区间修改 单点修改 查询最靠右0的位置。

代码

#include <bits/stdc++.h>
using namespace std; int T;
int n;
int m;
int x,y;
vector<int> g[100010];
int a[4000010];
int lazy[4000010]; void build(int l,int r,int o)
{
if (l==r)
{
a[o]=l-1;
return;
}
int mid=(l+r)>>1;
build(l,mid,o<<1);
build(mid+1,r,o<<1|1);
a[o]=min(a[o<<1],a[o<<1|1]);
return;
} void push_down(int o)
{
lazy[o<<1]+=lazy[o];
lazy[o<<1|1]+=lazy[o];
a[o<<1]+=lazy[o];
a[o<<1|1]+=lazy[o];
lazy[o]=0;
} void add(int L,int R,int l,int r,int o,int dlt)
{
if (L<=l && R>=r)
{
lazy[o]+=dlt;
a[o]+=dlt;
return;
}
push_down(o);
int mid=(l+r)>>1;
if (L<=mid) add(L,R,l,mid,o<<1,dlt);
if (R>mid) add(L,R,mid+1,r,o<<1|1,dlt);
a[o]=min(a[o<<1],a[o<<1|1]);
} int query(int l,int r,int o)
{
if (l==r) return l;
push_down(o);
int mid=(l+r)>>1;
if (a[o<<1|1]==0) return query(mid+1,r,o<<1|1);
else return query(l,mid,o<<1);
} int main()
{
ios::sync_with_stdio(false);
cin>>T;
while (T--)
{
cin>>n>>m;
memset(a,0,sizeof a);
memset(lazy,0,sizeof lazy);
build(1,n,1);
for (int i=1;i<=m;i++)
{
cin>>x>>y;
g[x].push_back(y);
add(y,y,1,n,1,-1);
}
for (int i=1;i<n;i++)
{
int find=query(1,n,1);
cout<<find<<" ";
if (find+1<=n) add(find+1,n,1,n,1,-1);
for (int o=0;o<g[find].size();o++) add(g[find][o],g[find][o],1,n,1,1);
add(find,find,1,n,1,2*n);
}
int find=query(1,n,1);
cout<<find<<endl;
for (int i=1;i<=n;i++) g[i].clear();
}
}

题目链接

http://codeforces.com/gym/101102/problem/K

Gym-101102-K-Topological Sort的更多相关文章

  1. 【拓扑排序】【线段树】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 ...

  2. topological sort~~~~初学

    今天讲了topological sort 问题: 判环:记录入队的点数,若<n则有环,可证: 算法:o(n):queue or  stack,而不是o(n^2)枚举 #. 关系运算图(vijos ...

  3. Some facts about topological sort

    Definition: a topological sort of a DAG G is a sort such that for all edge (i,j) in G, i precedes j. ...

  4. topological sort

    A topological sortof a dag G  is a linear ordering of all its vertices such that if G contains anedg ...

  5. 拓扑排序(Topological Sort)

    Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...

  6. 6-16 Topological Sort(25 分)

    Write a program to find the topological order in a digraph. Format of functions: bool TopSort( LGrap ...

  7. [Algorithms] Topological Sort

    Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...

  8. gym/102021/K GCPC18 背包dp算不同数和的可能

    gym/102021/K 题意: 给定n(n<=60)个直线 ,长度<=1000; 可以转化为取 计算 ans = (sum  + 10 - g) / ( n + 1)  在小于5的条件下 ...

  9. [MIT6.006] 14. Depth-First Search (DFS), Topological Sort 深度优先搜索,拓扑排序

    一.深度优先搜索 它的定义是:递归探索图,必要时要回溯,同时避免重复. 关于深度优先搜索的伪代码如下: 左边DFS-Visit(V, Adj.s)是只实现visit所有连接某个特定点(例如s)的其他点 ...

  10. Gym - 100283K K. Cubes Shuffling —— 贪心

    题目链接:http://codeforces.com/gym/100283/problem/K 题解: 要使其相邻两项的差值之和最小,那么越靠中间,其数值越小. 那么剩下的问题就是如何放数字了.一开始 ...

随机推荐

  1. linux下查看配置信息命令

    # uname -a                                                 # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue   ...

  2. [BX]指令

    mov ax,[bx] 功能:bx中存放的数据作为一个偏移地址EA,段地址SA默认在ds中,将SA:EA处的数据送入ax中.即(ax)=((ds)*16+(bx)). mov [bx],ax 功能:b ...

  3. SideBar 选择城市时右侧边上的 选择bar

    需要定义一个SideBar的视图类  在布局文件中引用   同时在布局中设置一个textView默认不可见 当触摸时才显示   在调用的Activity中 sideBar.setOnTouchingL ...

  4. encode/decode/bytes

    python3中如何将字符型转换成utf-8格式的bytes类型 str_me = '字符是我'.encode('utf-8') print(str_me) >>:b'\xe5\xad\x ...

  5. 1、javaweb学习之配置文件web.xml

    今天这里主要讲述javaweb中的配置文件web.xml中的内容及其作用,都是基础部分,对于初学者需要好好掌握理解. 简单配置: <servlet>    <servlet-name ...

  6. Liunx 硬盘分区

    1)什么要进行硬盘分区:a) 更容易管理和控制系统,因为相关的文件和目录都放在一个分区中.b) 系统效率更高.c) 可以限制用户使用硬盘的份额(磁盘空间的大小).d) 更容易备份和恢复. 2)硬盘的逻 ...

  7. andorid 手机外部储存

    .xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  8. hdu 3415(单调队列) Max Sum of Max-K-sub-sequence

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=3415 大意是给出一个有n个数字的环状序列,让你求一个和最大的连续子序列.这个连续子序列的长度小于等于k. ...

  9. hdu 5418 (Floyd+哈密顿) 飞向世界

    http://acm.hdu.edu.cn/showproblem.php?pid=5418 题目大意是城市的编号是1到n,给出m条路线,表示从a城市飞到b城市飞机要耗多少油,最后问飞机从1出发飞过所 ...

  10. Linux系统和性能监控之CPU篇

    Linux系统和性能监控之CPU篇 性能优化就是找到系统处理中的瓶颈以及去除这些的过程.本文由sanotes.net站长tonnyom在2009年8月翻译自Linux System and Perfo ...