hdu 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): 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]);
    }
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值