HDU 5195 DZY Loves Topological Sorting 拓扑排序

题目链接:

hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195

bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=573&pid=1002

代码:

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<vector>
 6 #include<queue>
 7 using namespace std;
 8
 9 const int maxn = 1e5 + 10;
10 const int INF = 0x3f3f3f3f;
11
12 int n, m, k;
13
14 struct Node {
15     int v, flag;
16     Node(int v,int flag=0):v(v),flag(flag){}
17     Node() { flag = 0; }
18 };
19
20 vector<Node> head[maxn];
21 int ind[maxn],done[maxn];
22
23 void init() {
24     for (int i = 0; i < n; i++) {
25         ind[i] = done[i]=0;
26         head[i].clear();
27     }
28 }
29
30 int main() {
31     while (scanf("%d%d%d", &n, &m, &k) == 3 && n) {
32         init();
33         for (int i = 0; i < m; i++) {
34             int u, v;
35             scanf("%d%d", &u, &v); u--, v--;
36             ind[v]++;
37             head[u].push_back(Node(v,0));
38         }
39
40         priority_queue<int> pq;
41
42         //删边
43         for (int i = n - 1; i >= 0; i--) {
44             if (k >= ind[i]) {
45                 //将i的父亲ui中,满足ui<i,即边(ui,i)删了,这里要注意,对于边(ui,i),ui>i的边,根本不用删
46                 k -= ind[i];
47                 pq.push(i);
48                 for (int j = 0; j < head[i].size(); j++) {
49                     Node &nd = head[i][j];
50                     if (i > nd.v) {
51                         //把边(i,v)删了,拓扑排序的时候不能再走这条边了
52                         nd.flag = 1;
53                         ind[nd.v]--;
54                     }
55                 }
56             }
57         }
58
59         vector<int> ans;
60         //拓扑排序
61         while (!pq.empty()) {
62             int u = pq.top(); pq.pop();
63             if (done[u]) continue;
64             ans.push_back(u);
65             done[u] = 1;
66             for (int i = 0; i < head[u].size(); i++) {
67                 Node& nd = head[u][i];
68                 if (done[nd.v]||nd.flag) continue;
69                 ind[nd.v]--;
70                 if (ind[nd.v] == 0) pq.push(nd.v);
71             }
72         }
73
74         printf("%d", ans[0]+1);
75         for (int i = 1; i < ans.size(); i++) printf(" %d", ans[i]+1);
76         printf("\n");
77     }
78     return 0;
79 }
80 /*
81 5 3 1
82 4 3
83 1 3
84 3 2
85
86 5 3 0
87 4 3
88 1 3
89 3 2
90 */
时间: 2024-07-30 02:46:57

HDU 5195 DZY Loves Topological Sorting 拓扑排序的相关文章

HDU 5195 DZY Loves Topological Sorting (拓扑排序+线段树)

题目地址:HDU 5195 简直受不了了..BC第二题都开始线段树+拓扑排序了... 这题很容易想到拓扑排序过程中贪心,但是贪心容易TLE,所以需要用数据结构去维护,我用的是线段树维护.每次找入度小于等于k的编号最大的点,这样就可以保证字典序一定是最大的. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorith

hdu.5195.DZY Loves Topological Sorting(topo排序 &amp;&amp; 贪心)

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 g

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

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

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

DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5195 Description A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for ev

HDU 5195 - DZY Loves Topological Sorting

题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会把当前还没入队的di≤k的最大的i找出来,把它的di条入边都删掉,然后加入拓扑序列. 删除的一定是小连大的边,因为大连小的边在拓扑序列生成的时候就去掉了 1 #include <iostream> 2 #include <cstdio> 3 #include <queue>

Hdoj 5195 DZY Loves Topological Sorting 【拓扑】+【线段树】

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 922 Accepted Submission(s): 269 Problem Description A topological sort or topological ordering of a directed graph i

有史以来我最坑的一次,hdu5195 DZY Loves Topological Sorting 拓扑序

//这题可算是历经千辛万苦才算ac了 //建图,然后就拓扑序, //还是官方的bc的题解出的好 //贪心取编号最大的点 //令du[i]<=k的i进入优先队列 //然后依次整就行了, //每次取出的点,判断一下 //是否du[i]<=k,如果小于 //依次遍历与他相邻的点, //在这些相邻的点中找到du[j]<=k //且不在队列当中的i的值, //开始用g++交题,一直TLE, //用高效一点的邻接表,还是TLE //然后用c++交题,结果...ac了 //TLE了十多发... //发

DZY Loves Topological Sorting (BC #35 hdu 5195 topsort+优先队列)

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 264    Accepted Submission(s): 63 Problem Description A topological sort or topological ordering of a directed gr

BC DZY Loves Topological Sorting

DZY Loves Topological Sorting Accepts: 112 Submissions: 586 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 一张有向图的拓扑序列是图中点的一个排列,满足对于图中的每条有向边(u→v) 从 u 到 v,都满足u在排列中出现在v之前. 现在,DZY有一张有向无环图(DAG).你要在最多删去k条边之后,求出字典序最大