题目链接:

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>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std; const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f; int n, m, k; struct Node {
int v, flag;
Node(int v,int flag=):v(v),flag(flag){}
Node() { flag = ; }
}; vector<Node> head[maxn];
int ind[maxn],done[maxn]; void init() {
for (int i = ; i < n; i++) {
ind[i] = done[i]=;
head[i].clear();
}
} int main() {
while (scanf("%d%d%d", &n, &m, &k) == && n) {
init();
for (int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v); u--, v--;
ind[v]++;
head[u].push_back(Node(v,));
} priority_queue<int> pq; //删边
for (int i = n - ; i >= ; i--) {
if (k >= ind[i]) {
//将i的父亲ui中,满足ui<i,即边(ui,i)删了,这里要注意,对于边(ui,i),ui>i的边,根本不用删
k -= ind[i];
pq.push(i);
for (int j = ; j < head[i].size(); j++) {
Node &nd = head[i][j];
if (i > nd.v) {
//把边(i,v)删了,拓扑排序的时候不能再走这条边了
nd.flag = ;
ind[nd.v]--;
}
}
}
} vector<int> ans;
//拓扑排序
while (!pq.empty()) {
int u = pq.top(); pq.pop();
if (done[u]) continue;
ans.push_back(u);
done[u] = ;
for (int i = ; i < head[u].size(); i++) {
Node& nd = head[u][i];
if (done[nd.v]||nd.flag) continue;
ind[nd.v]--;
if (ind[nd.v] == ) pq.push(nd.v);
}
} printf("%d", ans[]+);
for (int i = ; i < ans.size(); i++) printf(" %d", ans[i]+);
printf("\n");
}
return ;
}
/*
5 3 1
4 3
1 3
3 2 5 3 0
4 3
1 3
3 2
*/

2、线段树+贪心

对于节点i,入度为ind[i],则可以这样贪心:对于1<=i<=n,求最大的i使得ind[i]<=k,我们可以把入度数组做成线段树,维护最小入度,二分查找,优先搜右边(右边的i会更大),找到以后,吧对于的子节点vj的入度减1,k-=ind[i]。这样一直做n次就能得到答案。

 #include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define lson (o<<1)
#define rson ((o<<1)+1)
#define M (l+(r-l)/2)
using namespace std; const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f; int n, m, k; vector<int> head[maxn];
int ind[maxn<<],minv[maxn<<]; int _pos, _v;
void update(int o, int l, int r) {
if (l==r) {
ind[o] += _v;
minv[o] = ind[o];
}
else {
if (_pos <= M) update(lson, l, M);
else update(rson, M + , r);
minv[o] = min(minv[lson], minv[rson]);
}
} void query(int o, int l, int r,int &res) {
if (l == r) {
if (ind[o] <= k) {
k -= ind[o];
res = l;
}
}
else {
//printf("lson:%d,rson:%d\n",minv[lson],minv[rson]);
if (k >= minv[rson]) query(rson, M + , r,res);
else if (k >= minv[lson]) query(lson, l, M,res);
}
} void init() {
for (int i = ; i <= n; i++) head[i].clear();
memset(ind, , sizeof(ind));
memset(minv,,sizeof(minv));
} int main() {
while (scanf("%d%d%d", &n, &m, &k) == && n) {
init();
for (int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
head[u].push_back(v);
_pos = v, _v = ;
update(, , n);
}
//puts("after update");
vector<int> ans;
for (int i = ; i < n; i++) {
int res;
query(, , n, res);
//puts("after first qurey!");
//printf("res:%d\n", res);
ans.push_back(res);
for (int j = ; j < head[res].size(); j++) {
_pos = head[res][j], _v = -;
update(, , n);
//puts("after first upate");
}
_pos = res, _v = INF;
update(, , n);
}
//puts("ans is zero");
printf("%d", ans[]);
for (int i = ; i < ans.size(); i++) printf(" %d", ans[i]);
printf("\n");
}
return ;
}

HDU 5195 DZY Loves Topological Sorting 拓扑排序的更多相关文章

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

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

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

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

    DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

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

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

  5. HDU 5195 - DZY Loves Topological Sorting

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

  6. Topological Sorting拓扑排序

    定义: Topological Sorting is a method of arranging the vertices in a directed acyclic graph (DAG有向无环图) ...

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

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

  8. 数据结构(线段树):HDU 5649 DZY Loves Sorting

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  9. HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

随机推荐

  1. 安装Maven后使用cmd 执行 mvn -version命令 报错JAVA_HOME should point to a JDK not a JRE

    1. 可以执行maven指令,说明maven的配置没错 2. 打开cmd,在cmd输入: set JAVA_HOME=D:\Program Files\Java\jdk1.8.0_91 3. 再测试是 ...

  2. Rabbitmq(三)

    1.在服务器安装好rabbitmq后,自己配置自己用的vhost,exchange和queue的绑定 2.项目添加RabbitMqClient.dll(nuget获取)引用 3.添加helper就可以 ...

  3. BLDC无刷直流电机的原理及驱动基础

    无刷直流电机(BLDC,也称为马达驱动)是电机和控制技术相结合的产品,电调控制电机的运行,从电流驱动角度来看,无刷直流电机可分为正弦波驱动和方波驱动.通常,以方波驱动的电机称为无刷直流电机(BLDC) ...

  4. go_json解析

    总结: 其他类型转json func Marshal(v interface{}) ([]byte, error)  json 转其他类型 func Unmarshal(data []byte, v ...

  5. LintCode 896. Prime Product 简明题解

    Given a non-repeating prime array arr, and each prime number is used at most once, find all the prod ...

  6. Linux Shell中管道的原理及C实现框架

    在shell中我们经常用到管道,有没考虑过Shell是怎么实现管道的呢? cat minicom.log | grep "error" 标准输入.标准输出与管道 我们知道,每一个进 ...

  7. 第三次随笔——虚拟机及Linux入门

    虚拟机及Linux入门 虚拟机的安装 对于虚拟机的概念我早有接触,但是从来没有真正的实践过,借这次作业机会我终于实践了虚拟机的安装,安装的过程较为顺利,但还是出现了以下问题: 无法选择64位的系统 解 ...

  8. zabbix最新版3.4搭建(根据官方文档适当修改)

    操作系统:CentOS Linux release 7.4.1708 (Core) 1.安装apache 1.1 安装apache yum install httpd httpd-devel 1.2 ...

  9. [FJOI2014]最短路径树问题 长链剖分

    [FJOI2014]最短路径树问题 LG传送门 B站传送门 长链剖分练手好题. 如果你还不会长链剖分的基本操作,可以看看我的总结. 这题本来出的很没水平,就是dijkstra(反正我是不用SPFA)的 ...

  10. R的数据结构

    R语言中的数据结构包括标量.向量.矩阵.数组.列表以及数据框 目录 1 向量 2 矩阵 3 数据框 1 向量 向量是用于存储单一数据类型(数值.字符.逻辑值)的一维数组,示例如下: a <- c ...