POJ1270 Following Orders (拓扑排序)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 4254 | Accepted: 1709 |
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
input consists of a sequence of constraint specifications. A
specification consists of two lines: a list of variables on one line
followed by a list of contraints on the next line. A constraint is given
by a pair of variables, where x y indicates that x < y.
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
each constraint specification, all orderings consistent with the
constraints should be printed. Orderings are printed in lexicographical
(alphabetical) order, one per line.
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
收获:1.了解了stringstream.
2.用dfs输出拓扑排序的所有情况。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <sstream>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 500
#define maxm 28
char a[maxn];
char ans[maxn];
int in[maxn];
int vis[maxn];
int map1[maxn][maxn];
int total;
void dfs(int id)
{
if(id == total)
{
ans[id] = '\0';
puts(ans);
return;
}
for(int i = ; i < ; i++)
{
if(vis[i]) continue;
if(in[i] == )
{
ans[id] = 'a' + i;
vis[i] = ;
for(int j = ; j < ; j++)
if(map1[i][j]) in[j]--;
dfs(id+);
vis[i] = ;
for(int j = ; j < ; j++)
if(map1[i][j]) in[j]++;
}
}
}
char x, y;
int main()
{
int flag = ;
while(gets(a) != NULL)
{
if(flag)
puts("");
flag = ;
total = ;
stringstream ss(a);
memset(in, INF, sizeof in);
memset(vis, , sizeof vis);
while(ss >> x)
{
in[x - 'a'] = ;
total++;
}
gets(a);
stringstream sss(a);//读取一行。
memset(map1, , sizeof map1);
while(sss >> x >> y)//扫描该行的字符。
{
map1[x - 'a'][y- 'a'] = ;
in[y - 'a']++; }
dfs();
}
return ;
}
POJ1270 Following Orders (拓扑排序)的更多相关文章
- POJ1270 Following Orders[拓扑排序所有方案 Kahn]
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4885 Accepted: 1973 ...
- 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< ...
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- poj1270Following Orders(拓扑排序+dfs回溯)
题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...
- 拓扑排序+DFS(POJ1270)
[日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...
- POJ 1270 Following Orders(拓扑排序)
题意: 给两行字符串,第一行为一组变量,第二行时一组约束(每个约束包含两个变量,x y 表示 x <y).输出满足约束的所有字符串序列. 思路:拓扑排序 + 深度优先搜索(DFS算法) 课本代码 ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- 【二分+拓扑排序】Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348
目录 Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348 PROBLEM 题目描述 输入 输出 样例输入 样例输出 提示 MEA ...
随机推荐
- Linux下如何选择文件系统:EXT4、Btrfs 和 XFS
老实说,人们最不曾思考的问题之一是他们的个人电脑中使用了什么文件系统.Windows 和 Mac OS X 用户更没有理由去考虑,因为对于他们的操作系统,只有一种选择,那就是 NTFS 和 HFS+. ...
- 最小费用最大流模板 poj 2159 模板水题
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15944 Accepted: 8167 Descr ...
- hdu 5642 King's Order(数位dp)
Problem Description After the king's speech , everyone is encouraged. But the war is not over. The k ...
- ubuntu 右键新建文档
第一步.创建一个空白doc文档. 一定要是空白的,然后保存(另存为)到桌面,注意细节:1.保存到你能找到的地方,最好是桌面,以方便第二步拖动 2.注意下方的 “文件类型”,要选择doc格式. 3.注意 ...
- textChanged(*)重点
# -*- coding: cp936 -*- import sys from PyQt4 import QtCore, QtGui class MyDialog(QtGui.QDialog): de ...
- YYmodel 郭耀源 底层分析
http://www.tuicool.com/articles/meAzIny YYModel 简介与使用 http://www.jianshu.com/p/663c7b608ff5 ...
- Hash表的扩容(转载)
Hash表(Hash Table) hash表实际上由size个的桶组成一个桶数组table[0...size-1] . 当一个对象经过哈希之后.得到一个对应的value , 于是我们把这个对象放 ...
- DOS 选择跳转实现、dos + bcp 双击导入和导出数据
DOS 选择跳转实现.dos + bcp 双击导入和导出数据 option.bat @echo off :Start2 cls goto Start :Start title Frequently U ...
- CGBitmapContextCreate函数
CGBitmapContextCreate函数参数详解 函数原型: CGContextRef CGBitmapContextCreate ( void *data, size_t width, ...
- stagefright omx小结
由于stagefright和openmax运行在两个不同的进程上,所以他们之间的通讯要经过Binder进行处理,本小结不考虑音频这一块,假设视频为MP4封装的AVC编码文件. 先简单的看一下stage ...