Gym-101102-K-Topological Sort
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的更多相关文章
- 【拓扑排序】【线段树】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 ...
- topological sort~~~~初学
今天讲了topological sort 问题: 判环:记录入队的点数,若<n则有环,可证: 算法:o(n):queue or stack,而不是o(n^2)枚举 #. 关系运算图(vijos ...
- 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. ...
- topological sort
A topological sortof a dag G is a linear ordering of all its vertices such that if G contains anedg ...
- 拓扑排序(Topological Sort)
Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...
- 6-16 Topological Sort(25 分)
Write a program to find the topological order in a digraph. Format of functions: bool TopSort( LGrap ...
- [Algorithms] Topological Sort
Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...
- gym/102021/K GCPC18 背包dp算不同数和的可能
gym/102021/K 题意: 给定n(n<=60)个直线 ,长度<=1000; 可以转化为取 计算 ans = (sum + 10 - g) / ( n + 1) 在小于5的条件下 ...
- [MIT6.006] 14. Depth-First Search (DFS), Topological Sort 深度优先搜索,拓扑排序
一.深度优先搜索 它的定义是:递归探索图,必要时要回溯,同时避免重复. 关于深度优先搜索的伪代码如下: 左边DFS-Visit(V, Adj.s)是只实现visit所有连接某个特定点(例如s)的其他点 ...
- Gym - 100283K K. Cubes Shuffling —— 贪心
题目链接:http://codeforces.com/gym/100283/problem/K 题解: 要使其相邻两项的差值之和最小,那么越靠中间,其数值越小. 那么剩下的问题就是如何放数字了.一开始 ...
随机推荐
- appium 问题
adb shell pm list packages:列出所有的包名.adb shell dumpsys package:列出所有的安装应用的信息dumpsys package com.android ...
- golang 通过fsnotify监控文件,并通过文件变化重启程序
一.下载我们需要的包 > go get github.com/fsnotify/fsnotify 二.使用fsnotify监控文件 package main; import ( "gi ...
- [z]eclipase优化
eclipse启动优化,终于不那么卡了! 网上找了好多都是myEclipse的优化的,跟eclipse有点区别,找了很多方法还是不能让这个eclipse(Version: Kepler Release ...
- C#使用MiniDump捕获异常
c/c++语言里MiniDump是一个重要的调试手段,他们没有C#/java这样语言有很多异常输出信息( JVM异常导出bug日志功能,通常在jdk目录,文件格式hs_err_%pid%.log,pi ...
- 整理 oracle异常错误处理
5.1 异常处理概念 5.1.1 预定义的异常处理 5.1.2 非预定义的异常处理 5.1.3 用户自定义的异常处理 5.1.4 用户定义的异常处理 5.2 异常错误传播 5.2.1 在执行部分引发 ...
- AsyncTask的缺陷
导语:在开发Android应用的过程中,我们需要时刻注意保障应用的稳定性和界面响应性,因为不稳定或者响应速度慢的应用将会给用户带来非常差的交互体验.在越来越讲究用户体验的大环境下,用户也许会因为应用的 ...
- BOM心得-定时器
写在前面的话:之前一直以为定时器的返回值是Object类型,所以timer初始化也是写null,今天发现返回值是number,进而发觉这个返回值代表的是定时器的索引,指代这是第几个定时器 个人觉得只用 ...
- 异常检测(Anomaly Detection)
十五.异常检测(Anomaly Detection) 15.1 问题的动机 参考文档: 15 - 1 - Problem Motivation (8 min).mkv 在接下来的一系列视频中,我将向大 ...
- Bad owner or permissions on $HOME/.ssh/config
摘自:https://www.cnblogs.com/ytjjyy/p/4076442.html The ssh with RHEL 4 is a lot more anal about securi ...
- centos7 下安装mongodb指南;
1.下载:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.1.tgz; 2.解压缩; 3.文件存储在mongodb文件 ...