DZY Loves Topological Sorting (BC #35 hdu 5195 topsort+优先队列)

DZY Loves Topological Sorting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 264    Accepted Submission(s): 63

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 most k edges
from the graph.

Input

The input consists several test cases. (TestCase≤5)

The first line, three integers n,m,k(1≤n,m≤105,0≤k≤m).

Each of the next m lines
has two integers: u,v(u≠v,1≤u,v≤n),
representing a direct edge(u→v).

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).

Source

BestCoder Round #35

Recommend

hujie   |   We have carefully selected several similar problems for you:  5197 5196 5193 5192 5189

题意:n个点m条有向边组成的有向无环图,可以最多删除k条边让他的拓扑序最大。输出最大的拓扑序。

思路:在以前的topsort中是入读为零的点入队列,这里有k次机会可以删除边,那么我就把所有入度<=k的点全入队列,用优先队列维护最大的点序列号,去掉点最大序列号的所有入边,将它加入到拓扑序中,这样贪心是最优的。

13278437 2015-03-29 09:39:39 Accepted 5195 374MS 8344K 2822 B C++ wust_lyf
13278469 2015-03-29 09:42:51 Accepted 5195 1996MS 10928K 2822 B G++ wust_lyf

G++和C++差别如此大

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 200005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct Node
{
    int x;
    int id;
    friend bool operator<(const Node a,const Node b)
    {
        return a.id<b.id;
    }
};

int n,m,k;
vector <int> edge[maxn];
priority_queue<Node> Q;

int inDegree[maxn];
int ans[maxn];
int vis[maxn];

int main()
{
    int i;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        for (i=0;i<=n+2;i++)
        {
            edge[i].clear();
            inDegree[i]=0;
            vis[i]=0;  //不在队列中
            ans[i]=0;
        }
        for (i=0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            inDegree[b]++;
            edge[a].push_back(b);
        }

        while(!Q.empty())
            Q.pop();
        Node node;

        for (i=1;i<=n;i++)
            if (inDegree[i]<=k)
            {
                node.id=i;
                node.x=inDegree[i];
                Q.push(node);
                vis[i]=1;//在队列中
            }
        int t=0;

        while (!Q.empty())
        {
            Node now=Q.top();
            Q.pop();
            if (inDegree[now.id]<=k)
                k-=inDegree[now.id];
            else
            {
                vis[now.id]=0;
                continue;
            }
            vis[now.id]=2;//已经输出
            ans[t++]=now.id;
            for (i=0;i<edge[now.id].size();i++)
            {
                int v=edge[now.id][i];
                inDegree[v]--;
                node.id=v;
                node.x=inDegree[v];
                if (inDegree[v]<=k && vis[v]==0)
                {
                    Q.push(node);
                    vis[v]=1;
                }
            }
        }

        pf("%d",ans[0]);
        for (int i=1;i<t;i++)
            pf(" %d",ans[i]);
        pf("\n");
    }
    return 0;
}
/*
6 6 2
5 6
1 2
4 5
2 4
3 4
2 3
3 2 0
1 2
1 3
*/
时间: 2024-08-07 16:46:31

DZY Loves Topological Sorting (BC #35 hdu 5195 topsort+优先队列)的相关文章

hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5195 Description A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for ev

hdu.5195.DZY Loves Topological Sorting(topo排序 &amp;&amp; 贪心)

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 Problem Description A topological sort or topological ordering of a directed g

Hdoj 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): 922 Accepted Submission(s): 269 Problem Description A topological sort or topological ordering of a directed graph i

BC DZY Loves Topological Sorting

DZY Loves Topological Sorting Accepts: 112 Submissions: 586 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 一张有向图的拓扑序列是图中点的一个排列,满足对于图中的每条有向边(u→v) 从 u 到 v,都满足u在排列中出现在v之前. 现在,DZY有一张有向无环图(DAG).你要在最多删去k条边之后,求出字典序最大

HDU 5195 DZY Loves Topological Sorting (拓扑排序+线段树)

题目地址:HDU 5195 简直受不了了..BC第二题都开始线段树+拓扑排序了... 这题很容易想到拓扑排序过程中贪心,但是贪心容易TLE,所以需要用数据结构去维护,我用的是线段树维护.每次找入度小于等于k的编号最大的点,这样就可以保证字典序一定是最大的. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorith

HDU 5195 DZY Loves Topological Sorting 拓扑排序

题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=573&pid=1002 代码: 1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio&

HDU 5195 - DZY Loves Topological Sorting

题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会把当前还没入队的di≤k的最大的i找出来,把它的di条入边都删掉,然后加入拓扑序列. 删除的一定是小连大的边,因为大连小的边在拓扑序列生成的时候就去掉了 1 #include <iostream> 2 #include <cstdio> 3 #include <queue>

有史以来我最坑的一次,hdu5195 DZY Loves Topological Sorting 拓扑序

//这题可算是历经千辛万苦才算ac了 //建图,然后就拓扑序, //还是官方的bc的题解出的好 //贪心取编号最大的点 //令du[i]<=k的i进入优先队列 //然后依次整就行了, //每次取出的点,判断一下 //是否du[i]<=k,如果小于 //依次遍历与他相邻的点, //在这些相邻的点中找到du[j]<=k //且不在队列当中的i的值, //开始用g++交题,一直TLE, //用高效一点的邻接表,还是TLE //然后用c++交题,结果...ac了 //TLE了十多发... //发