HDU 5195 - DZY Loves Topological Sorting

题意:

  删去K条边,使拓扑排序后序列字典序最大

分析:

  因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队。我们定义点i的入度为di。

  假设当前还能删去k条边,那么我们一定会把当前还没入队的di≤k的最大的i找出来,把它的di条入边都删掉,然后加入拓扑序列。

  删除的一定是小连大的边,因为大连小的边在拓扑序列生成的时候就去掉了

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 #include <vector>
 5 using namespace std;
 6 const int MAXN = 100005;
 7 int n, m, k, u, v;
 8 vector<int> g[MAXN];
 9 int c[MAXN], vis[MAXN], ans[MAXN];
10 priority_queue<int> s;
11 int main()
12 {
13     while (~scanf("%d%d%d", &n, &m, &k))
14     {
15         while (!s.empty()) s.pop();
16         for (int i = 1; i <= n; i++) g[i].clear(), vis[i] = c[i] = 0;
17         for (int i = 1; i <= m; i++)
18         {
19             scanf("%d%d", &u, &v);
20             g[u].push_back(v);
21             c[v]++;
22         }
23         for (int i = 1; i <= n; i++)
24             if (k >= c[i]) s.push(i);
25         int cnt = 0;
26         while (!s.empty())
27         {
28             int x = s.top(); s.pop();
29             if (c[x] <= k && !vis[x] )
30             {
31                 vis[x] = 1;
32                 k -= c[x], c[x] = 0;
33                 ans[cnt++] = x;
34                 for (int i = 0; i < g[x].size(); i++)
35                 {
36                     c[g[x][i]]--;
37                     if ( !c[g[x][i]] ) s.push(g[x][i]);
38                 }
39             }
40         }
41         for (int i = 0; i < cnt-1; i++) printf("%d ", ans[i]);
42         printf("%d\n", ans[cnt-1]);
43     }
44 } 
				
时间: 2024-10-27 10:48:35

HDU 5195 - DZY Loves Topological Sorting的相关文章

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 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(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 (拓扑排序+线段树)

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

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&

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

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条边之后,求出字典序最大

【HDU】5195-DZY Loves Topological Sorting(拓扑 + 线段树 + 贪心)

每次找出入度小于K的编号最大点. 找的时候用线段树找,找完之后将这个点出度链接的点的入度全部减一 简直爆炸... #include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; #define lson (pos<<1) #define rson (pos<<1|1) const int maxn = 100005