hdu 4857(好题,反向拓扑排序)
逃生
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3652 Accepted Submission(s): 1040
现在有n个人,从1标号到n。同时有一些奇怪的约束条件,每个都形如:a必须在b之前。
同时,社会是不平等的,这些人有的穷有的富。1号最富,2号第二富,以此类推。有钱人就贿赂负责人,所以他们有一些好处。
负责人现在可以安排大家排队的顺序,由于收了好处,所以他要让1号尽量靠前,如果此时还有多种情况,就再让2号尽量靠前,如果还有多种情况,就让3号尽量靠前,以此类推。
那么你就要安排大家的顺序。我们保证一定有解。
然后对于每个测试数据,第一行有两个整数n(1 <= n <= 30000)和m(1 <= m <= 100000),分别表示人数和约束的个数。
然后m行,每行两个整数a和b,表示有一个约束a号必须在b号之前。a和b必然不同。
5 10
3 5
1 4
2 5
1 2
3 4
1 4
2 3
1 5
3 5
1 2
6 -> 3 -> 1
5 -> 4 -> 2
直接拓扑排序的结果是:5 4 2 6 3 1 ,结果是错误的,因为我们可以把1号安排到更前面的位置 即:6 3 1 5 4 2(正确答案)。
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <vector>
#include <functional>
#include <string.h>
using namespace std;
priority_queue<int>q;
const int N = ;
const int M = ;
int p[N];
int indegree[M];
struct Edge{
int v,next;
}edge[N];
int head[M];
void addEdge(int u,int v,int &k){
edge[k].v = v,edge[k].next = head[u],head[u]=k++;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
head[i] = -;
indegree[i] = ;
}
int tot = ;
for(int i=;i<m;i++){
int a,b;
scanf("%d%d",&a,&b);
addEdge(b,a,tot); ///重点,反向弧
indegree[a]++;
}
for(int i=;i<=n;i++){
if(indegree[i]==){
q.push(i);
}
}
int k = ;
int id = ;
while(!q.empty()){
int u = q.top();
p[id++] = u;
q.pop();
for(int k = head[u];k!=-;k=edge[k].next){
if(indegree[edge[k].v]>) indegree[edge[k].v]--;
if(indegree[edge[k].v]==) q.push(edge[k].v);
}
}
for(int i=id-;i>;i--){
printf("%d ",p[i]);
}
printf("%d\n",p[]);
}
}
错误代码:
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <vector>
#include <functional>
#include <string.h>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
const int N = ;
const int M = ;
int p[N];
int indegree[M];
struct Edge
{
int v,next;
} edge[N];
int head[M];
void addEdge(int u,int v,int &k)
{
edge[k].v = v,edge[k].next = head[u],head[u]=k++;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++)
{
head[i] = -;
indegree[i] = ;
}
int tot = ;
for(int i=; i<m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
addEdge(a,b,tot);
indegree[b]++;
}
for(int i=; i<=n; i++)
{
if(indegree[i]==)
{
q.push(i);
}
}
int k = ;
int id = ;
while(!q.empty())
{
int u = q.top();
q.pop();
p[id++] = u;
for(int k = head[u]; k!=-; k=edge[k].next)
{
if(indegree[edge[k].v]>) indegree[edge[k].v]--;
if(indegree[edge[k].v]==) q.push(edge[k].v);
}
}
for(int i=; i<id-; i++)
{
printf("%d ",p[i]);
}
printf("%d\n",p[id-]);
}
}
hdu 4857(好题,反向拓扑排序)的更多相关文章
- HDU 4857 逃生 (反向拓扑排序 & 容器实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 4857 逃生(反向拓扑排序+优先队列)
( ̄▽ ̄)" //这题对序号输出有要求,较小的序号优先输出,所以用到优先队列 //优先队列是优先弹出值最大的,所以最后要反向输出结果,才是正确的output #include<iost ...
- HDU 4857 逃生(反向拓扑排序)
传送门 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社 ...
- hdu 4857 逆向建图+拓扑排序 ***
题意:糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行.现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会是不平等的,这些人有的穷有 ...
- HDU 2647 Reward【反向拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 逃生(HDU4857 + 反向拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题面是中文题面,就不解释题意了,自己点击链接去看下啦~这题排序有两个条件,一个是按给定的那个序列 ...
- CF-825E Minimal Labels 反向拓扑排序
http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字 ...
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- HDU.1285 确定比赛名次 (拓扑排序 TopSort)
HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...
随机推荐
- Base64及其Python实现
1. 什么是Base64 Base64是一种基于64个可打印字符来表示二进制数据的表示方法 Base64是一种编码方式,提及编码方式,必然有其对应的字符集合.在Base64编码中,相互映射的两个集合是 ...
- 各种友(e)善(xin)数论总集(未完待续),从入门到绝望
目录 快速幂 扩展欧几里得 GCD 扩展欧几里得 同余系列 同余方程 同余方程组 一点想法 高次同余方程 BSGS exBSGS 线性筛素数 埃式筛 欧拉筛 欧拉函数 讲解 两道水题 法雷级数 可见点 ...
- UML类图关系模式(C++代码说明)
在UML类图中的关系模式主要有以下几种: 泛化(Generalization), 实现(Realization), 关联(Association), 聚合(Aggregation), 依赖(Depe ...
- DFS、栈、双向队列:CF264A- Escape from Stones
题目: Squirrel Liss liv Escape from Stonesed in a forest peacefully, but unexpected trouble happens. S ...
- Visual Studio-IIS Express 支持局域网访问配置
转自:http://www.itnose.net/detail/6132793.html 注意:本人测试后,发现个问题,不知是我个人的VS问题还是普遍的.就是将配置文件中的新增的节点注释后,会导致页面 ...
- 查询语句为“%string_”的情况
select * from t_user where user_name like '%Joe_%'实际查询出来的语句为: 而不像预计的前两条.
- RF,GBDT,XGBoost,lightGBM的对比
转载地址:https://blog.csdn.net/u014248127/article/details/79015803 RF,GBDT,XGBoost,lightGBM都属于集成学习(Ensem ...
- MySQL基础4-SQL简单查询(单表)
1.SELECT语句 2.运算符的优先级 利用Navicat中的查询方法: 栗子1:查询所有货品信息 栗子2:查询所有货品的id,productName,salePrice 当查询错误的时候出现的界面 ...
- idea 安装findBugs 可以做代码扫描,也可以导出扫描结果生成扫描报告
idea 安装findBugs 可以做代码扫描,也可以导出扫描结果生成扫描报告 https://my.oschina.net/viakiba/blog/1838296 https://www.cnbl ...
- loj2308 「APIO2017」商旅
ref #include <iostream> #include <cstring> #include <cstdio> #include <queue> ...