DZY Loves Topological Sorting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 866    Accepted Submission(s): 250

Problem Description
A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge (u→v) from vertex u to vertex v,u comes before v in the ordering. Now, DZY has a directed acyclic graph(DAG). You should find the lexicographically largest topological ordering after erasing at most k edges from the graph.
 
Input
The input consists several test cases. (TestCase≤5) The first line, three integers n,m,k(1≤n,m≤105,0≤k≤m). Each of the next m lines has two integers: u,v(u≠v,1≤u,v≤n), representing a direct edge(u→v).
 
Output
For each test case, output the lexicographically largest topological ordering.
 
Sample Input
5 5 2
1 2
4 5
2 4
3 4
2 3
3 2 0
1 2
1 3
 
Sample Output
5 3 1 2 4
1 3 2

Hint

Case 1.
Erase the edge (2->3),(4->5).
And the lexicographically largest topological ordering is (5,3,1,2,4).

 
Source

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int M = ;
struct Edge
{
int v , nxt ;
Edge () {}
Edge (int v , int nxt) : v (v) , nxt (nxt) {}
}e[M];
int H[M] , E ;
bool vis[M] ;
int ans[M] , top ;
int in[M] ;
int n , m , k ;
int u , v ;
inline int read () {
int ans = ; char c; bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
if (c == '-') flag = true; else ans = c - '';
while ((c = getchar()) >= '' && c <= '') ans = ans * + c - '';
return ans * (flag ? - : );
} void addedge ()
{
e[E] = Edge ( v , H[u] ) ;
H[u] = E ++ ;
} void init ()
{
E = ;
top = ;
memset (H , - , sizeof(H)) ;
memset (ans , , sizeof(ans) ) ;
memset (in , , sizeof(in)) ;
memset (vis , , sizeof(vis) ) ;
} void topo ()
{
priority_queue <int> q ;
while (!q.empty ()) q.pop () ;
for (int i = ; i <= n ; i++) if (in[i] == && !vis[i]) q.push (i) ;
while ( !q.empty () ) {
int u = q.top () ;
q.pop () ;
ans[top ++] = u ;
for (int i = H[u] ; ~ i ; i = e[i].nxt) {
in[e[i].v] -- ;
if (in[e[i].v] == && !vis[e[i].v]) q.push (e[i].v) ;
}
}
} void solve ()
{
init () ;
while (m--) {
u = read () , v = read () ;
addedge () ;
in[v] ++ ;
}
priority_queue <int> q ;
for (int i = ; i <= n ; i++) if (in[i] <= k) q.push (i) ;
while ( !q.empty () ) {
u = q.top () ;
q.pop () ;
if (in[u] > k) continue ;
ans[top ++] = u ;
k -= in[u] ;
vis[u] = ;
for (int i = H[u] ; ~ i ; i = e[i].nxt) {
in[e[i].v] -- ;
if (in[e[i].v] <= k && !vis[u] ) q.push (e[i].v) ;
}
}
topo () ;
for (int i = ; i < top ; i++) {
printf ("%d%c" , ans[i] , i == top - ? '\n' : ' ') ;
}
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
while (~ scanf ("%d%d%d" , &n , &m , &k)) {
solve () ;
}
return ;
}

用邻接表优化后,topo的时间复杂度O(n),空间复杂度也大大减少。orz。
还有快速读入。

托它们的福,这道题让我530ms过了。233333333

hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)的更多相关文章

  1. HDU 5195 DZY Loves Topological Sorting 拓扑排序

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  2. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

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

  3. hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

    DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

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

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

  5. HDU 5195 - DZY Loves Topological Sorting

    题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会 ...

  6. 2019.01.22 hdu5195 DZY Loves Topological Sorting(贪心+线段树)

    传送门 题意简述:给出一张DAGDAGDAG,要求删去不超过kkk条边问最后拓扑序的最大字典序是多少. 思路:贪心帮当前不超过删边上限且权值最大的点删边,用线段树维护一下每个点的入度来支持查询即可. ...

  7. 数据结构(线段树):HDU 5649 DZY Loves Sorting

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  8. HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  9. HDU 5646 DZY Loves Partition

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ...

随机推荐

  1. 学习笔记——Maven实战(二)POM重构之增还是删

    重构是广大开发者再熟悉不过的技术,在Martin Fowler的<重构——改善既有代码的设计>一书中,其定义为“重构(名词):对软件内部结构的一种调整,目的是在不改变软件之可察行为前提下, ...

  2. 通俗易懂------this指向

    因为JavaScript 中this 是在运行期进行绑定的,因此JavaScript 中this 关键字具备多重含义. 具体在实际应用中,this的指向大致可以分为下面4种. 作为对象的方法调用   ...

  3. Enum扩展及MVC中DropDownListFor扩展方法的使用

    public enum SearchState { /// <summary> /// 全部 /// </summary> [Description("全部" ...

  4. publish_subscribe

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  5. Java-TreeSet

    如下: package 集合类.Set类; /** * Set不允许重复数据 */ /** * TreeSet 是用来进行集合排序的,请注意他和LinkedHashSet的区别. TreeSet是按照 ...

  6. Spring MVC设计模式

    MVC开始是存在于桌面程序中的,M是指业务模型,V是指用户界面,C则是控制器 使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式.比如一批统计数据可以分别用柱状图.饼图来 ...

  7. Oracle分页查询语句

    SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM (此处添加你要分页的表)) A WHERE ROWNUM <= 14000)WH ...

  8. golang thrift 总结一下网络上的一些坑

    我们以hello world来大概分析一下golang中的thrift包,并且扒一扒网络上有关thrift的一些坑 查看源码,服务器定义如下:(详见simple_server.go文件) type T ...

  9. 畅所欲言第1期 - 从Viola&Jones的人脸检测说起

    转载自http://c.blog.sina.com.cn/profile.php?blogid=ab0aa22c890006v0 不少人认识我或者听说我的名字都是因为我过去做的关于人脸检测的工作,那么 ...

  10. 【poj3537】 Crosses ans Crosses

    poj.org/problem?id=3537 (题目链接) 题意 给出一个1*n的棋盘,每次可以选择一个没被标记过的点打标记,若经过某一步操作使得出现3个连续的标记,则最后操作的人获胜.问是否存在先 ...