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

题意

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

题解:

因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队。我们定义点i的入度为di。假设当前还能删去k条边,那么我们一定会把当前还没入队的di≤k的最大的i找出来,把它的di条入边都删掉,然后加入拓扑序列。可以证明,这一定是最优的。
具体实现可以用线段树维护每个位置的di,在线段树上二分可以找到当前还没入队的di≤k的最大的i。于是时间复杂度就是O((n+m)logn).

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
//**************************************************************************************
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
int du[];
int vis[];
vector<int> lin[];
int n,m,k;
int t[];
int arr[];
void build(int i,int l,int r)
{
if (l==r)
{
t[i]=du[l];
arr[l]=i;
return;
}
int mid=(l+r)/;
build(i*,l,mid);
build(i*+,mid+,r);
t[i]=min(t[i*],t[i*+]);
return;
}
int query(int i,int l,int r,int k)
{
if (l==r)
return l;
int mid=(l+r)/;
if (t[i*+]<=k) return query(i*+,mid+,r,k);
else return query(i*,l,mid,k);
}
void insert(int i,int l,int r,int wei,int cc)
{
if (l==r)
{
t[i]+=cc;
return;
}
int mid=(l+r)/;
if (wei<=mid) insert(i*,l,mid,wei,cc);
else insert(i*+,mid+,r,wei,cc);
t[i]=min(t[i*],t[i*+]);
return;
}
int main()
{
while (cin>>n>>m>>k)
{
memset(vis,,sizeof(vis));
memset(du,,sizeof(du));
for (int i=;i<=n;i++)
lin[i].clear();
int i,j;
for (int tt=;tt<=m;tt++)
{
scanf("%d%d",&i,&j);
lin[i].push_back(j);
du[j]++;
}
int nn=;
int flag=;
build(,,n);
for (int i=;i<=n;i++)
{
int c=query(,,n,k);
if (flag) printf(" ");
printf("%d",c);
flag=;
k-=du[c];
insert(,,n,c,);
for (int kk=;kk<lin[c].size();kk++)
{
int j=lin[c][kk];
insert(,,n,j,-);
du[j]--;
} }
printf("\n");
}
}

hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序的更多相关文章

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

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  2. hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

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

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  4. HDU 5195 DZY Loves Topological Sorting 拓扑排序

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  5. HDU 5195 - DZY Loves Topological Sorting

    题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会 ...

  6. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. 2019.01.22 hdu5195 DZY Loves Topological Sorting(贪心+线段树)

    传送门 题意简述:给出一张DAGDAGDAG,要求删去不超过kkk条边问最后拓扑序的最大字典序是多少. 思路:贪心帮当前不超过删边上限且权值最大的点删边,用线段树维护一下每个点的入度来支持查询即可. ...

  8. ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)

    Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...

  9. HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)

    题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...

随机推荐

  1. 配置tomcat多域名访问

    C:\Windows\System32\drivers\etc下的hosts文件改成:127.0.0.1 localhost 127.0.0.1 www.greenmood.net 127.0.0.1 ...

  2. ISG2018 web题Writeup

    0x01.命令注入 这题可以使用burpsuite扫出来,但是可能需要测一下. 得知payload为:i%7cecho%20gzavvlsv9c%20q9szmriaiy%7c%7ca%20%23'% ...

  3. MongoDB之python简单交互(三)

    python连接mongodb有多种orm,主流的有pymongo和mongoengine. pymongo 安装相关模块 pip install pymongo pymongo操作 主要对象 Mon ...

  4. 122.Best Time to Buy and Sell Stock II---dp

    题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ 题目大意:基本定义与121类似,不 ...

  5. sicily 1259. Sum of Consecutive Primes

    Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...

  6. CentOS7 安装python库(numpy、scipy、matplotlib、scikit-learn、tensorflow)

    0.1准备工作 安装好CentOS7,配置好网络,确保网络畅通. 0.2root授权 首先:当前用户为kaid # vim /etc/sudoers 在root ALL=(ALL) ALL之后添加: ...

  7. centos7安装Python3的过程中会和Python2.7版本冲突导致yum版本比对应,致使yum不能使用的问题。

    centos7安装Python3的过程中会和Python2.7版本冲突导致yum版本比对应,致使yum不能使用的问题. 原因:yum调用Python,启动程/usr/bin/yum就是一个python ...

  8. 大型网站的 HTTPS 实践(一)—— HTTPS 协议和原理(转)

    原文链接:http://op.baidu.com/2015/04/https-s01a01/ 1 前言 百度已经于近日上线了全站 HTTPS 的安全搜索,默认会将 HTTP 请求跳转成 HTTPS.本 ...

  9. 【数据挖掘基础算法】KNN最近邻分类算法

    算法简介: 通过计算待预测样本和已知分类号的训练样本之间的距离来判断该样本属于某个已知分类号的概率.并选取概率最大的分类号来作为待预测样本的分类号 懒惰分类算法,其模型的建立直到待预测实例进行预测时才 ...

  10. promise应用于ajax

    promise应用于ajax,可以在本页打开控制台,复制代码试验 var url = 'https://www.cnblogs.com/mvc/blog/news.aspx?blogApp=dkplu ...