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 拓扑排序 裸题 板子题的更多相关文章

  1. UVa 10305 - Ordering Tasks (拓扑排序裸题)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  2. HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题 &amp;&amp; 邻接表实现】

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  3. POJ2367(拓扑排序裸题

    #include<iostream> #include<vector> #include<queue> using namespace std; typedef l ...

  4. uva 10305 拓扑排序裸题

    https://vjudge.net/problem/UVA-10305 目前没学dfs做法,用的队列做法,每次找到一个入度为零的点出队后更新其他点,再加入入度为零的点直到查找完毕,这个题目显然一定有 ...

  5. HDU1285(拓扑排序裸题

    ..被多组测试坑了一波 #include<iostream> #include<vector> #include<queue> using namespace st ...

  6. 差异---虐爆了yxs的 后缀数组裸题 板子题 单调栈的简单应用 字符串的基础理解考察题

    先玩柿子,发现可以拆开,前半部分可以瞬间求出,于是只求后半部分 然后抄板子就好了,完结撒花! 下边是个人口胡,因为已经被虐爆头脑不清醒了 定义:LCP(a,b)为排名为a,b两个后缀的最长公共前缀 证 ...

  7. 第十二届湖南省赛 (B - 有向无环图 )(拓扑排序+思维)好题

    Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始.点 v 结束的路径). 为了方便,点用 1,2,…,n 编号. 设 count(x,y) 表示点 x 到点 y ...

  8. C#LeetCode刷题-拓扑排序

    拓扑排序篇 # 题名 刷题 通过率 难度 207 课程表   40.0% 中等 210 课程表 II   39.8% 中等 329 矩阵中的最长递增路径   31.0% 困难 ​​​​​​​

  9. ACM: HDU 1285 确定比赛名次 - 拓扑排序

     HDU 1285 确定比赛名次 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

随机推荐

  1. 死磕 java集合之终结篇

    概览 我们先来看一看java中所有集合的类关系图. 这里面的类太多了,请放大看,如果放大还看不清,请再放大看,如果还是看不清,请放弃. 我们下面主要分成五个部分来逐个击破. List List中的元素 ...

  2. 纯css实现的三级水平导航菜单

    vscode练习使用开发纯css的三级水平导航菜单.先上图: 1.html5布局 <html> <head> <meta charset="UTF-8" ...

  3. php(三)使用PDO链接数据库

    1.启动 mysql数据库,打开图形化控制界面 2.新建一个数据库 3.创建一个数据表 4.给数据表添加数据 id是数字类型的  类型选择int长度 11 username 等其他数据  会是字符串形 ...

  4. asterisk-java ami3 属性改变监听

    asteriskServer.addAsteriskServerListener(new AsteriskListenerInit());//服务属性监听会自动连接服务 实现AsteriskServe ...

  5. CSS中的趣事之float浮动

       浮动float一般跟left或是right: 特性: 1,包裹性:浮动文本类型时,需要指定宽度width,如果不指定,就会折叠到最小宽度: 2,浮动会影响别的元素: 3,子级浮动,会导致父级高度 ...

  6. JavaScript回文数

    基本解决方案 function palindrome(str) { return str.replace(/[\W_]/g, '').toLowerCase() === str.replace(/[\ ...

  7. ZooKeeper系列(三)

    前面虽然配置了集群模式的Zookeeper,但是为了方面学建议在伪分布式模式的Zookeeper学习Zookeeper的shell命令. 一.Zookeeper的四字命令 Zookeeper支持某些特 ...

  8. leetcode_712. Minimum ASCII Delete Sum for Two Strings

    https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/ 给定两个string s1,s2,从s1和s2中删除一些 ...

  9. HDU_1237_简单计算器

    运算符为+,-,*,/:操作数为整数:且没有括号 设定符号优先级,先在栈底压运算符0 #include<iostream> #include<cstdio> #include& ...

  10. git 控制操作

    克隆文件 git clone https://gitee.com/xxxxx/xxxxx.git 克隆分支文件 git clone -b 分支名 https://gitee.com/xxxxx/xxx ...