hdu 5195 DZY Loves Topological Sorting【拓扑排序+优先队列+邻接表】

本文介绍了一种算法,用于在有向无环图中找到字典序最大的拓扑排序,通过删除最多k条边实现。利用优先队列进行贪心选择,确保每次选择的顶点编号尽可能大。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DZY Loves Topological Sorting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1014    Accepted Submission(s): 303


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 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 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 2 1 2 4 5 2 4 3 4 2 3 3 2 0 1 2 1 3
 


Sample Output
5 3 1 2 4 1 3 2
Hint
Case 1. Erase the edge (2->3),(4->5). And the lexicographically largest topological ordering is (5,3,1,2,4).


题目大意:

一张有向图的拓扑序列是图中点的一个排列,满足对于图中的每条有向边(u→v)(u\rightarrow v)(uv)uuuvvv,都满足uuu在排列中出现在vvv之前。
现在,DZY有一张有向无环图(DAG)。你要在最多删去kkk条边之后,求出字典序最大的拓扑序列。

题目保证了图是合法的拓扑图、这里我们就不用多担心了。这里说要删除k条边,然后求出字典序最大的拓扑排序,看到这里,我们马上就应该有一种贪心的思想:去边的时候,找度小于等于k的点,使这个点的编号尽量大,然后去度、这里求的是字典序最大的拓扑序列,我们可以应用优先队列来搞定

因为图比较大,所以这里用vector来搞定图的问题:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
int now,head[200010],nex[200010],p[200010];
int vis[200010];
int degree[200010];
vector<int>map[200010];
int ans[200010];
/*
void add(int x,int y)
{
    nex[++now]=head[x];
    head[x]=now;
    p[now]=y;
}*/
int main()
{
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(int i=1;i<=n;i++)
        {
            map[i].clear();
        }
        memset(vis,0,sizeof(vis));
        memset(degree,0,sizeof(degree));
        memset(head,0,sizeof(head));
        now=0;
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            map[x].push_back(y);
            //add(x,y);
            degree[y]++;
        }
        priority_queue<int>s;
        for(int i=1;i<=n;i++)
        {
            if(degree[i]<=k)
            s.push(i);
        }
        int cont=0;
        while(!s.empty())
        {
            int u=s.top();
            s.pop();
            if(vis[u]||degree[u]>k)continue;
            vis[u]=1;
            k-=degree[u];
            ans[++cont]=u;

            for(int j=0;j<map[u].size();j++)
            {
                int v=map[u][j];
                degree[v]--;
                if(degree[v]<=k&&vis[v]==0)
                s.push(v);
            }
            /*
            for(int i=head[u];i;i=nex[i])
            {
                int v=p[i];
                degree[v]--;
                if(degree[v]<=k&&vis[v]==0)
                s.push(v);
            }*/
        }
        for(int i=1;i<cont;i++)
        {
            printf("%d ",ans[i]);
        }
        printf("%d\n",ans[cont]);
    }
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值