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. LN : leetcode 712 Minimum ASCII Delete Sum for Two Strings

    lc 712 Minimum ASCII Delete Sum for Two Strings 712 Minimum ASCII Delete Sum for Two Strings Given t ...

  2. 初识react native遇到的问题

    Andriod 使用react native时遇到的问题     打开现有项目报错: 从第一行Error可以知道是一个zip的压缩文件打不开,往下看应该是下载的Gradle文件有问题,提示也是让从新下 ...

  3. sql把两值之和当作条件进行查询

    目的:把表中两个字段之和作为where条件进行过滤查询 //查询在没有过期的记录select a,b from test where a+b>now();// a:存入时间 b:有效期时间段 进 ...

  4. 迅为I.MX6开发板工业级嵌入式开发平台

    迅为-i.MX6开发板是是基于ARM Cortex™-A9架构的高扩展性多核系列应用处理器, i.MX6系列芯片而且根据应用场合的不同,提供了可供选择的单核.双核和四核产品供客户选择.i.MX6系列的 ...

  5. swift class extension 与继承

    1.扩展中无法继承重写已有函数,不能添加函数. Extensions can add new functionality to a type, but they cannot override exi ...

  6. CREATE USER - 创建一个新的数据库用户帐户

    SYNOPSIS CREATE USER name [ [ WITH ] option [ ... ] ] where option can be: SYSID uid | [ ENCRYPTED | ...

  7. AngularJS小练习20170508

    首先可能需要安装npm,并且配置环境. 1.打开Dos(命令提示符).按Windows徽标键+R组合键,输入cmd然后按回车键进入Dos. 2.安装Yeoman.在Dos下输入npm install ...

  8. OAuth - 第三方登录的原理

    第三方登录的原理 所谓第三方登录,实质就是 OAuth 授权.用户想要登录 A 网站,A 网站让用户提供第三方网站的数据,证明自己的身份.获取第三方网站的身份数据,就需要 OAuth 授权. 举例来说 ...

  9. JavaScript:JSON 和 JS 对象

    区别 JSON(JavaScript Object Notation)仅仅是一种数据格式(或者叫数据形式).数据格式其实就是一种规范,按照这种规范来存诸和交换数据.就好像 XML 格式一样. 区别 J ...

  10. js判断图片是否有效

    var ImgObj=new Image(); ImgObj.src= 'http://192.168.10.6:8082/3D/SERVER_1_DELL_880.jpg'; if(ImgObj.f ...