[线段树] hdu5195 DZY Loves Topological Sorting

题意:

给一张拓扑排序图,最多删除K条边之后。

输出最大字典序的拓扑排序。

思路:

用线段树处理,节点存每个点的入度。

每次提取入度小于等于k的并且最大数字的点。

然后更新k和这个点已经所连的点的入度,并标记这个取出的点入度为无穷大。

重复操作n次。

代码:

#include"cstdio"
#include"cstring"
#include"cmath"
#include"cstdlib"
#include"algorithm"
#include"iostream"
#include"map"
#include"vector"
#include"queue"
#define ll long long
#define inf 9999999
using namespace std;
#define MAXN 100004
struct tree
{
    int l,r;
    int mi;
} a[MAXN*4];
int rd[MAXN];
vector<int>edge[MAXN];
void init(int id,int l,int r)
{
    a[id].l=l;
    a[id].r=r;
    if(a[id].l==a[id].r)
    {
        a[id].mi=rd[l];
        return;
    }
    init(2*id,l,(l+r)/2);
    init(2*id+1,(r+l)/2+1,r);
    a[id].mi=min(a[2*id].mi,a[2*id+1].mi);
}
void make(int id,int x,int y)
{
    if(a[id].l==a[id].r)
    {
        a[id].mi=y;
        return ;
    }
    int mid=(a[id].r+a[id].l)/2;
    if(x<=mid) make(id*2,x,y);
    else make(id*2+1,x,y);
    a[id].mi=min(a[id*2].mi,a[id*2+1].mi);
}
int solve(int id,int k)
{
    if(a[id].l==a[id].r) return a[id].l;
    if(a[id*2+1].mi<=k) return solve(id*2+1,k);
    else return solve(id*2,k);
}
int main()
{
    int n,m,k;
    while(cin>>n>>m>>k)
    {
        memset(rd,0,sizeof(rd));
        for(int i=1; i<=n; i++) edge[i].clear();
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            edge[x].push_back(y);
            rd[y]++;
        }
        init(1,1,n);
        for(int i=1;i<=n;i++)
        {
            int now=solve(1,k);
            k-=rd[now];
            rd[now]=inf;
            make(1,now,inf);
            int len=edge[now].size();
            for(int j=0;j<len;j++)
            {
                int tep=edge[now][j];
                rd[tep]--;
                make(1,tep,rd[tep]);
            }
            printf(i==n?"%d\n":"%d ",now);
        }
    }
    return 0;
}
时间: 2024-10-17 06:58:28

[线段树] hdu5195 DZY Loves Topological Sorting的相关文章

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

要求在一个DAG中删去不多于k条边,使得拓扑序的字典序最大. 贪心策略:每次删去入度小于res的,序号尽量大的点的入边. 需要用线段树维护区间最小值. 代码: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<vector> using name

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

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

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

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条边之后,求出字典序最大

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 gr

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

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

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