POJ2367 拓扑排序 裸题 板子题
http://poj.org/problem?id=2367
队列版
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = 1e3+;
const int maxm = 1e4+;
const int inf = 0x3f3f3f3f;
const int mod = ;
const double epx = 1e-;
typedef long long ll;
const ll INF = 1e18;
const double pi = acos(-1.0);
vector<int> g[maxn];
int rudu[maxn],order[maxn],vis[maxn];
int n,m,cnt;
void toposort()
{
priority_queue<int,vector<int>,greater<int> > q;
for(int i=;i<=n;i++)
if(rudu[i]==)
q.push(i),vis[i]=;
while(!q.empty())
{
int v=q.top();q.pop();
order[cnt++]=v;
for(int i=;i<g[v].size();i++)
{
int u=g[v][i];
rudu[u]--;
if(rudu[u]==)
q.push(u),vis[u]=;
}
}
}
int main()
{
cin>>n;
memset(rudu,,sizeof(rudu));
memset(order,,sizeof(order));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
int u,v;
do{
cin>>v;
if(v!=)
g[i].push_back(v),rudu[v]++;
}while(v!=);
}
cnt=;toposort();
if(cnt==n)
for(int i=;i<n;i++)
{
if(i==n-)
cout<<order[i]<<endl;
else
cout<<order[i]<<" ";
}
//else
//cout<<"no"<<endl;
}
dfs版
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = 1e3+;
const int maxm = 1e4+;
const int inf = 0x3f3f3f3f;
const int mod = ;
const double epx = 1e-;
typedef long long ll;
const ll INF = 1e18;
const double pi = acos(-1.0);
int g[maxn][maxn];
int c[maxn],n,m;
int topo[maxn],t;
bool dfs(int u) //思想是 一直搜找到当前没有出度的点 存到数组里 逆序操作
{
c[u]=-; //c数组 0表示没有访问过 -1表示还在栈中尚未返回 1表示已经访问完
for(int v=;v<=n;v++)
{
if(g[u][v])
{
if(c[v]<)
return false; //当前节点已经在栈中了 说明有环 返回false
else if(!c[v]&&!dfs(v))
return false; //若子节点搜索过程中遇到环 向前返回失败
}
}
c[u]=;topo[t--]=u; //遍历完之后存到数组里
return true;
}
bool toposort()
{
t=n;
memset(c,,sizeof(c));
for(int u=;u<=n;u++)
if(!c[u]&&!dfs(u))
return false; //如果当前节点没有被访问过 进行dfs 若返回false 说明环 不能拓扑排序
return true;
}
int main()
{
cin>>n;
memset(g,,sizeof(g));
for(int i=;i<=n;i++)
{
int u;
while()
{
cin>>u;
if(u==)
break;
g[i][u]=;
}
}
if(toposort())
{
for(int i=;i<=n-;i++)
cout<<topo[i]<<" ";
cout<<topo[n]<<endl;
}
}
POJ2367 拓扑排序 裸题 板子题的更多相关文章
- UVa 10305 - Ordering Tasks (拓扑排序裸题)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- HDU 3342 -- Legal or Not【裸拓扑排序 &&水题 && 邻接表实现】
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- POJ2367(拓扑排序裸题
#include<iostream> #include<vector> #include<queue> using namespace std; typedef l ...
- uva 10305 拓扑排序裸题
https://vjudge.net/problem/UVA-10305 目前没学dfs做法,用的队列做法,每次找到一个入度为零的点出队后更新其他点,再加入入度为零的点直到查找完毕,这个题目显然一定有 ...
- HDU1285(拓扑排序裸题
..被多组测试坑了一波 #include<iostream> #include<vector> #include<queue> using namespace st ...
- 差异---虐爆了yxs的 后缀数组裸题 板子题 单调栈的简单应用 字符串的基础理解考察题
先玩柿子,发现可以拆开,前半部分可以瞬间求出,于是只求后半部分 然后抄板子就好了,完结撒花! 下边是个人口胡,因为已经被虐爆头脑不清醒了 定义:LCP(a,b)为排名为a,b两个后缀的最长公共前缀 证 ...
- 第十二届湖南省赛 (B - 有向无环图 )(拓扑排序+思维)好题
Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始.点 v 结束的路径). 为了方便,点用 1,2,…,n 编号. 设 count(x,y) 表示点 x 到点 y ...
- C#LeetCode刷题-拓扑排序
拓扑排序篇 # 题名 刷题 通过率 难度 207 课程表 40.0% 中等 210 课程表 II 39.8% 中等 329 矩阵中的最长递增路径 31.0% 困难
- ACM: HDU 1285 确定比赛名次 - 拓扑排序
HDU 1285 确定比赛名次 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
随机推荐
- Spring @Resource、@Autowired、@Qualifier区别
@Resource默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入: @Autowired默认是按照类型装配注入的,如果想按照名称来转配注入,则需要结合@Qualif ...
- office doc/xls/ppt 和 docx/xlsx/pptx 区别
经同事告诉,今天才真正明白两都区别: doc/xls/ppt 是office2007以前的扩展名: docx/xlsx/pptx 是office2007版本及以后的扩展名,是基于xml的文件格式,x ...
- RabbitMQ五:生产者--队列--多消费者
一.生成者-队列-多消费者(前言) 上篇文章,我们做了一个简单的Demo,一个生产者对应一个消费者,本篇文章就介绍 生产者-队列-多个消费者,下面简单示意图 P 生产者 C 消费者 中间队列 ...
- C#控件置于底层或顶层
btn.BringToFront();//置于顶层 btn.SendToBack();//置于底层
- outlook 插件:导出rss的link地址
由于对于rss的应用程序不熟悉,所以使用Outlook接收rss.使用过程和平时收邮件没有什么差别. 唯一的遗憾是鉴于安全考虑,outlook没有全部下载网页,所以每次都要打开浏览器.有时候遇到一些需 ...
- windows ubuntu bcdeditor
双系统. 先装windows,后装ubuntu12.04 为了避免grub引导,所以安装bcdeditor. 平时使用没有什么不适,可是每次linux升级内核以后,grub列表可能就会消失,自然是不能 ...
- swift -Dynamic Dispatch
These instructions perform dynamic lookup of class and generic methods. The class_method and super_m ...
- pdf 使用模板下载
//根据模板下载模板 /** * * 政策5-8条的创建的pdf的模板 */public String createPdfCashTemplate(PdfCashParam pdfCashParam) ...
- chroot - 以 特定 根 目录 运行 命令 或者 交互式 shell
总览 (SYNOPSIS) chroot [OPTION] NEWROOT [COMMAND...] chroot OPTION 描述 (DESCRIPTION) 以 NEWROOT 为 根 目录 运 ...
- 07JavaScript数组与字符串对象
JavaScript数组与字符串对象 5.1.1数组(Array)对象 <script> //声明一个数组并赋值; var arr = new Array("aa",& ...