hdu 5195 DZY Loves Topological Sorting(线段树)
来源:互联网 发布:贵阳大数据培训班 编辑:程序博客网 时间:2024/05/22 03:24
DZY Loves Topological Sorting
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1065 Accepted Submission(s): 328
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 mostk edges from the graph.
Now, DZY has a directed acyclic graph(DAG). You should find the lexicographically largest topological ordering after erasing at most
Input
The input consists several test cases. (TestCase≤5 )
The first line, three integersn,m,k(1≤n,m≤105,0≤k≤m) .
Each of the nextm lines has two integers: u,v(u≠v,1≤u,v≤n) , representing a direct edge(u→v) .
The first line, three integers
Each of the next
Output
For each test case, output the lexicographically largest topological ordering.
Sample Input
5 5 21 24 52 43 42 33 2 01 21 3
Sample Output
5 3 1 2 41 3 2HintCase 1.Erase the edge (2->3),(4->5).And the lexicographically largest topological ordering is (5,3,1,2,4).
思路:一开始想在拓扑排序里面改,可是怎么也贪心不出来。
后面才知道能用线段树去维护,每次取入度<=k的边,并且编号尽量大(也就是尽量往右取),然后每次取完更新即可。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;#define N 100005#define INF 999999999struct Edge{ int v,next;}edge[N];int tree[N<<2];int num[N],cnt,head[N];int ans,k;void init(){ cnt=0; memset(num,0,sizeof(num)); memset(head,-1,sizeof(head));}void addedge(int u,int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}void pushup(int root){ tree[root]=min(tree[root<<1],tree[root<<1|1]);}void build(int root,int l,int r){ if(l==r) { tree[root]=num[l]; return; } int mid=(l+r)>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); pushup(root);}void query(int root,int l,int r){ if(l==r) { k-=tree[root]; ans=l; tree[root]=INF; return; } int mid=(l+r)>>1; if(tree[root<<1|1]<=k) query(root<<1|1,mid+1,r); else query(root<<1,l,mid); pushup(root);}void update(int pos,int root,int l,int r){ if(l==r) { tree[root]--; return; } int mid=(l+r)>>1; if(mid>=pos) update(pos,root<<1,l,mid); else update(pos,root<<1|1,mid+1,r); pushup(root);}int main(){ int n,m; int u,v; while(~scanf("%d %d %d",&n,&m,&k)) { init(); for(int i=1; i<=m; i++) { scanf("%d %d",&u,&v); addedge(u,v); num[v]++; } build(1,1,n); for(int i=1;i<=n;i++) { query(1,1,n); printf("%d",ans); if(i<n) printf(" "); else printf("\n"); update(ans,1,1,n); for(int i=head[ans];i!=-1;i=edge[i].next) { int v=edge[i].v; num[v]--; update(v,1,1,n); } } } return 0;}
0 0
- HDU 5195 DZY Loves Topological Sorting(线段树)
- hdu 5195 DZY Loves Topological Sorting(线段树)
- HDU 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
- HDU 5195 DZY Loves Topological Sorting(线段树单点更新)
- 【HDU】5195-DZY Loves Topological Sorting(拓扑 + 线段树 + 贪心)
- [线段树] hdu5195 DZY Loves Topological Sorting
- Hdoj 5195 DZY Loves Topological Sorting 【拓扑】+【线段树】
- HDU 5195 DZY Loves Topological Sorting
- hdu 5195 DZY Loves Topological Sorting
- HDU 5195 DZY Loves Topological Sorting
- HDU 5195 DZY Loves Topological Sorting
- HDU 5195 DZY Loves Topological Sorting(优先队列)
- hdu5195 DZY Loves Topological Sorting 线段树+拓扑排序
- hdu 5195 DZY Loves Topological Sorting && BestCoder Round #35
- HDU 5195 DZY Loves Topological Sorting 拓扑序(贪心)
- 【Best Coder】#35 DZY Loves Topological Sorting(线段树维护)
- (拓扑排序)DZY Loves Topological Sorting
- [HDU 5649] DZY Loves Sorting (线段树+二分)
- Unity DestroyImmediate和Destroy区别
- 生产者消费者模式理解
- 使用strip工具减少.so库文件的大小
- 携程工作感言(三)(7.31)
- jQuery中的全局函数getJSON()异步读取数据乱码问题
- hdu 5195 DZY Loves Topological Sorting(线段树)
- Linux下安装软件的一般步骤
- 在NSObject类中,和继承它的类中,弹出UIAlertcontroller和push、present到其它界面
- Android中用native 修饰的方法学习
- VS2013+WDK8.1编写window驱动
- 工作杂谈
- RefreashListView(自定义ListView)
- AngularJSURL参数传递问题
- 统计分析学习之数值分析方法