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 (uv) from vertex u to vertex vu 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. (TestCase5)
The first line, three integers n,m,k(1n,m105,0km).
Each of the next m lines has two integers: u,v(uv,1u,vn), representing a direct edge(uv).
 

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 2
Hint
Case 1.Erase the edge (2->3),(4->5).And the lexicographically largest topological ordering is (5,3,1,2,4).
题意:求删除k条边后字典序最大的拓扑排序

思路:一开始想在拓扑排序里面改,可是怎么也贪心不出来。

后面才知道能用线段树去维护,每次取入度<=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
原创粉丝点击