hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)
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
1 2
4 5
2 4
3 4
2 3
3 2 0
1 2
1 3
1 3 2
Case 1.
Erase the edge (2->3),(4->5).
And the lexicographically largest topological ordering is (5,3,1,2,4).
#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排序 && 贪心)的更多相关文章
- HDU 5195 DZY Loves Topological Sorting 拓扑排序
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- 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 ...
- hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序
DZY Loves Topological Sorting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...
- hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- HDU 5195 - DZY Loves Topological Sorting
题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会 ...
- 2019.01.22 hdu5195 DZY Loves Topological Sorting(贪心+线段树)
传送门 题意简述:给出一张DAGDAGDAG,要求删去不超过kkk条边问最后拓扑序的最大字典序是多少. 思路:贪心帮当前不超过删边上限且权值最大的点删边,用线段树维护一下每个点的入度来支持查询即可. ...
- 数据结构(线段树):HDU 5649 DZY Loves Sorting
DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数
DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- HDU 5646 DZY Loves Partition
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ...
随机推荐
- 20145234黄斐《信息安全系统设计基础》GDB调试汇编堆栈过程分析
堆栈跟踪 首先编辑一个程序 用gcc编译,再使用gdb调试,发现gdb尚未下载 下载后重新运行gdb 设置断点:b+行号或者"main" 运行:r frame:打印出的信息:栈的层 ...
- json和cookie兼容以前的
'json': function(data) { try { if (typeof data === "string") { if (typeof JSON != 'undefin ...
- 多个相同name的文本输入框,输入其中一个后,使剩下的不能输入值
可以用blur或keyup事件响应: 实现一: <body> <input type="text" id="AfterOtOt1" name= ...
- [USACO2004][poj2373]Dividing the Path(DP+单调队列)
http://poj.org/problem?id=2373 题意:一条直线分割成N(<=25000)块田,有一群奶牛会在其固定区域吃草,每1把雨伞可以遮住向左右延伸各A到B的区域,一只奶牛吃草 ...
- xml文件的读写操作
1.直接上代码:包含了xml文档的创建,读取xml文档,创建根节点,向根节点中添加子节点,保存xml文档----------先来张效果图: static void Main(string[] args ...
- ie6下使用js替换img标签src属性图片不显示的错误
首先,我必须再次强调一下,F-U-C-K I-E! 其次,简单阐述一下这个bug的出现的情况.页面中有个<a href=”javascript:void(0)” onclick=”swapImg ...
- JS模式:简单的图书馆享元模式
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- 【前端】Sublime text3 插件LiveReload 实现实时预览
1.首先要安装插件LiveReload Sublime text3. 菜单 preferences->packages control,输入install.. 回车,输入LiveReload回车 ...
- BZOJ-2929 洞穴攀岩 最大流Dinic(傻逼题)
竟然没有1A真羞耻...1分钟不到读完题,10分钟不到打完....MD没仔细看...WA了一遍,贱! 2929: [Poi1999]洞穴攀行 Time Limit: 1 Sec Memory Limi ...
- 直接运行可执行文件linux终端一闪而过
运行elasticsearch的时候进入bin目录,ela 然后tab提示的内容中没有e..s..,很奇怪,然后我直接双击运行es,终端一闪而过,我就手动打开终端, ./elasticsearch 这 ...