POJ1270 Following Orders[拓扑排序所有方案 Kahn]
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 4885 | Accepted: 1973 |
Description
This problem involves neither Zorn's Lemma nor fix-point semantics, but does involve order.
Given a list of variable constraints of the form x < y, you are to write a program that prints all orderings of the variables that are consistent with the constraints.
For example, given the constraints x < y and x < z there are two orderings of the variables x, y, and z that are consistent with these constraints: x y z and x z y.
Input
All variables are single character, lower-case letters. There will be at least two variables, and no more than 20 variables in a specification. There will be at least one constraint, and no more than 50 constraints in a specification. There will be at least one, and no more than 300 orderings consistent with the contraints in a specification.
Input is terminated by end-of-file.
Output
Output for different constraint specifications is separated by a blank line.
Sample Input
a b f g
a b b f
v w x y z
v y x v z v w v
Sample Output
abfg
abgf
agbf
gabf wxzvy
wzxvy
xwzvy
xzwvy
zwxvy
zxwvy
Source
L← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
insert n into L
foreach node m with an edge e from nto m do
remove edge e from thegraph
ifm has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least onecycle)
else
return L (a topologically sortedorder)
就是找入度为0的点(最好用个stack,循环的话复杂的太高),加入topo头部
感觉比dfs好,复杂度都是O(V+E)
本题回溯所有方案,复杂度乘上一个V;V很小,不用stack也可以;用个id比较方便吧
字符读入太坑人.........
//
// main.cpp
// poj1270
//
// Created by Candy on 9/11/16.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=,M=;
char s[];
int a[N],num=,n=,id[N];
int ch[N][N],topo[N],ind[N]; void print(){
for(int i=;i<=n;i++) printf("%c",(char)topo[i]+'a'-);
printf("\n");
}
void dfs(int d){ //printf("dfs %d\n",d);
if(d==n+){print();return;}
for(int i=;i<=n;i++)
if(ind[i]==){
ind[i]--; topo[d]=a[i];
for(int j=;j<=ch[i][];j++) ind[ch[i][j]]--;
dfs(d+);
for(int j=;j<=ch[i][];j++) ind[ch[i][j]]++;
ind[i]++;
}
}
int main(int argc, const char * argv[]) {
while(fgets(s,,stdin)){ //printf("p %s\n",s);
n=;
memset(topo,,sizeof(topo));
memset(ch,,sizeof(ch));
memset(ind,,sizeof(ind));
int len=strlen(s); //printf("len %d\n",len);
for(int i=;i<len;i++)
if(s[i]>='a'&&s[i]<='z') a[++n]=s[i]-'a'+;
sort(a+,a++n);
for(int i=;i<=n;i++) id[a[i]]=i; fgets(s,,stdin);
len=strlen(s);
int last=;
for(int i=;i<=len;i++)
if(s[i]>='a'&&s[i]<='z'){
int t=s[i]-'a'+;
t=id[t];
if(last==) last=t;
else{ch[last][++ch[last][]]=t;ind[t]++;last=;}
}
dfs();
printf("\n");
}
return ;
}
POJ1270 Following Orders[拓扑排序所有方案 Kahn]的更多相关文章
- POJ 1270 Following Orders (拓扑排序,dfs枚举)
题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y. 要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...
- POJ 1270 Following Orders 拓扑排序
http://poj.org/problem?id=1270 题目大意: 给你一串序列,然后再给你他们部分的大小,要求你输出他们从小到大的所有排列. 如a b f g 然后 a<b ,b< ...
- POJ1270 Following Orders (拓扑排序)
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4254 Accepted: 1709 ...
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- AOV网络和Kahn算法拓扑排序
1.AOV与DAG 活动网络可以用来描述生产计划.施工过程.生产流程.程序流程等工程中各子工程的安排问题. 一般一个工程可以分成若干个子工程,这些子工程称为活动(Activity).完成了这些活动 ...
- poj1270Following Orders(拓扑排序+dfs回溯)
题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...
- 拓扑排序+DFS(POJ1270)
[日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...
- POJ 1270 Following Orders(拓扑排序)
题意: 给两行字符串,第一行为一组变量,第二行时一组约束(每个约束包含两个变量,x y 表示 x <y).输出满足约束的所有字符串序列. 思路:拓扑排序 + 深度优先搜索(DFS算法) 课本代码 ...
- 2017-2018 ACM-ICPC NEERC B题Berland Army 拓扑排序+非常伤脑筋的要求
题目链接:http://codeforces.com/contest/883/problem/B There are n military men in the Berland army. Some ...
随机推荐
- CSS中兼容的一面-----Hack
国庆了,出去玩耍,也有好长时间没有更新博客了.. 今天就和大家共享一篇技术博文吧.. CSS中兼容的一面-----Hack技术大全 兼容范围: IE:6.0+,FireFox:2.0+,Opera 1 ...
- HTML 文本格式化实例
一,文本格式化:此例演示如何在一个 HTML 文件中对文本进行格式化. <html> <body> <b>This text is bold</b> & ...
- 学习zepto.js(对象方法)[1]
zepto也是使用的链式操作,链式操作:函数返回调用函数的对象. 但并不是所有的对象方法都可以进行链式操作,举几个例子:.size(),.html()|.text()//不传参数的情况下; 若非特殊说 ...
- Jquery属性获取——attr()与prop()
今天在项目中使用<select></select>下拉菜单时,使用juery操作,使页面加载完菜单默认选中的值为2,我一开始的操作如下: <!--html部分--> ...
- chrome developer tool—— 断点调试篇
断点,调试器的功能之一,可以让程序中断在需要的地方,从而方便其分析.也可以在一次调试中设置断点,下一次只需让程序自动运行到设置断点位置,便可在上次设置断点的位置中断下来,极大的方便了操作,同时节省了时 ...
- SharePoint 2013 修改表单认证登录页面
前 言 之前的博客我们介绍了如何为SharePoint配置表单登陆,但是,登陆页面是丑.很丑.非常丑.特别非常丑!我们现在就介绍一下如何定制SharePoint表单登陆页面! SharePoint 表 ...
- Emacs学习心得之 基础操作
作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Emacs学习心得之 基础操作 1.前言与学习计划2.Emacs基础操作 一. 前言与学习计 ...
- 生成iOSAPP的二维码
1.打开iTunes,在"应用"里面搜索要找的APP 2.右键要生成二维码的APP,选择"拷贝链接" 3.百度一个二维码生成器 4.把刚才拷贝的链接粘贴进去,点 ...
- 如何自定义ViewGroup
依照惯例,先从一个例子说起. 很简单,3张扑克牌叠在一起显示.这个布局效果该如何实现呢?有的同学该说了,这很简单啊,用RelativeLayout或FrameLayout,然后为每一个扑克牌设置mar ...
- Android 获取图片exif信息
使用android api读取图片的exif信息 布局代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...