Genealogical tree poj-2367

    题目大意:给你一个n个点关系网,求任意一个满足这个关系网的序列,使得前者是后者的上级。

    注释:1<=n<=100.

      想法:刚刚学习toposort,什么是toposort?

        就是每一个点的遍历或选取有先决条件,那么我们可以通过队列或者栈将控制当前点的点先遍历,这样弹栈或出队的序列,就是toposort的结果

      我们先附上模板

int v[100100];//入度
bool map[110][110]={false};//是否存在控制与被控制
int ans[110];//出队序列
int tot;
int n;void toposort()
{
queue<int>q;
for(int i=1;i<=n;i++)
{
if(!v[i]) q.push(i);
}
while(q.size())//其实toposort不同于spfa和bfs在于时间复杂度是O(n)的
{
int x=q.front(); q.pop(); ans[++tot]=x;//直接更新
for(int i=1;i<=n;i++)
{
if(map[x][i]==1)
{
v[i]--;
if(v[i]==0) q.push(i);
}
}
}
}

    最后,附上丑陋的代码... ...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int v[100100];//入度
bool map[110][110]={false};//是否存在控制与被控制
int ans[110];//出队序列
int tot;
int n;void toposort()
{
queue<int>q;
for(int i=1;i<=n;i++)
{
if(!v[i]) q.push(i);
}
while(q.size())//其实toposort不同于spfa和bfs在于时间复杂度是O(n)的
{
int x=q.front(); q.pop(); ans[++tot]=x;//直接更新
for(int i=1;i<=n;i++)
{
if(map[x][i]==1)
{
v[i]--;
if(v[i]==0) q.push(i);
}
}
}
}
void original()
{
memset(map,false,sizeof map);
memset(ans,0,sizeof ans);
tot=0;
memset(v,0,sizeof v);
}
int main()
{
while(~scanf("%d",&n))
{
original();
for(int a,i=1;i<=n;i++)
{
while(1)
{
scanf("%d",&a);
if(!a) break;
v[a]++;
map[i][a]=1;
map[a][i]=1;
}
}
toposort();
for(int i=1;i<=tot;i++)
{
printf("%d ",ans[i]);
}
puts("");
}
return 0;
}

    小结:toposort是一种处理存在先决条件问题或上下级关系的主要手段之一,它在一般情况下作为一个辅助的条件。比如我们可以维护一个appear数组表示一个数是否出现过从而达到判环的效果(NOI2009植物大战僵尸),我们也可以通过拓扑排序来处理一些比较容易用图来表达的问题(没有上司的舞会)

[poj2367]Genealogical tree_拓扑排序的更多相关文章

  1. POJ2367 Genealogical tree (拓扑排序)

    裸拓扑排序. 拓扑排序 用一个队列实现,先把入度为0的点放入队列.然后考虑不断在图中删除队列中的点,每次删除一个点会产生一些新的入度为0的点.把这些点插入队列. 注意:有向无环图 g[] : g[i] ...

  2. timus 1022 Genealogical Tree(拓扑排序)

    Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background The system of Martians’ blood ...

  3. Poj 2367 Genealogical tree(拓扑排序)

    题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...

  4. POJ 2367 Genealogical tree 拓扑排序入门题

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8003   Accepted: 5184 ...

  5. POJ 2367:Genealogical tree(拓扑排序模板)

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7285   Accepted: 4704 ...

  6. 【拓扑排序】Genealogical tree

    [POJ2367]Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accep ...

  7. poj 2367 Genealogical tree (拓扑排序)

    火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...

  8. POJ2367【拓扑排序】

    很裸的拓扑排序~ //#include <bits/stdc++.h> #include<iostream> #include<string.h> #include ...

  9. POJ2367 拓扑排序 裸题 板子题

    http://poj.org/problem?id=2367 队列版 #include <stdio.h> #include <math.h> #include <str ...

随机推荐

  1. $_FILES数组为空的原因

    今天做上传的文件时候,打印$_files总是为空,查阅了下资料. 发现是 max_file_uploads=0 知道了原因 file_uploads = On upload_max_filesize ...

  2. javascript 学习笔记 三大特性

    <script type="text/javascript"> //封装 function Person (name,age,sal){ this.name=name; ...

  3. HighCharts之气泡图

    HighCharts之气泡图 1.HighCharts之气泡图源码 bubble.html: <!DOCTYPE html> <html> <head> <m ...

  4. JLINK(SEGGER)灯不亮 USB不识别固件修复、clone修改

    今天调SMT32插拔几下,JLINK竟然挂掉了网上找了这个教程,搞了半天才搞好,驱动没装好!WIN7系统,自动安装的驱动是GPS.COM10,郁闷,错误来的.应该是:atm6124.sys.要手动选择 ...

  5. 手机端仿ios的银行下拉脚本五

    代码 <script> $('#bankName').click(function(){ var $this = $(this); new Picker({ "title&quo ...

  6. TP5 数组分页

    需要 use think\Page; 我这个是 Page是从tp3.2的移到5.0来用的,如果你的里面没有这个也可以移动过来 PHP代码: $page= $this->request->p ...

  7. GridView添加事件监听和常用属性解析

    1. 使用流程 graph LR 准备数据源-->新建适配器 新建适配器-->绑定数据源 绑定数据源-->加载适配器 2. 常用属性 android:columnWidth:每一列的 ...

  8. 简述Spring事务有几种管理方法,写出一种配置方式

    Spring事务有两种方式: 1.编程式事务:(代码中嵌入) 2.声明式事务:(注解,XML) 注解方式配置事务的方式如下: 首先,需要在applicationContext.xml中添加启动配置,代 ...

  9. 【BZOJ1500】【NOI2005】维修数列(Splay)

    [BZOJ1500][NOI2005]维修数列(Splay) 题面 不想再看见这种毒瘤题,自己去BZOJ看 题解 Splay良心模板题 真的很简单 我一言不发 #include<iostream ...

  10. Luogu4175:[CTSC2008]网络管理Network

    题面 Luogu4175:[CTSC2008]网络管理Network Sol 路径第\(k\)大 无解直接判断就好了 然后整体二分,加上树链剖分+树状数组统计 # include <bits/s ...