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 ...
随机推荐
- HDU3336——KMP算法
题意是问所有前缀出现的次数和,mod10007: 想一想next数组代表什么意思,是从当前失配位置走到上一个匹配位置的后面,next[i]的值说明以当前位置为结尾,长度为next[i]的后缀,与以开头 ...
- poj2262
Goldb ...
- hdu 5433 Xiao Ming climbing(bfs+三维标记)
Problem Description Due to the curse made by the devil,Xiao Ming is stranded on a mountain and can ...
- pyqt 动态显示时间方法例子学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import sys,datetime from PyQt4.QtC ...
- Ubuntu 系统 文件操作命令
文件和目录的操作 用户主目录下有一个 Desktop (对应,桌面)mkdir dir1 建立一个目录cd 不添加参数,默认回到主目录(用户目录)touch a.txt 建立一个文件mv a.txt ...
- vim 快捷键大全
一.移动光标 1.左移h.右移l.下移j.上移k 2.向下翻页ctrl + f,向上翻页ctrl + b 3.向下翻半页ctrl + d,向上翻半页ctrl + u 4.移动到行尾$,移动到行首0(数 ...
- Masonry的一些使用。
除了等距的有问题(懒得改了),其他用过挺正常的,我自己也是刚使用,有问题还请海涵. 地址:http://pan.baidu.com/s/1boyxu8Z
- Ubutu命令 笔记积累
1 man command 查询帮助 查询结果会有 name synopsis(概要) description 2 terminal 中快捷键: Ctrl +u 撤销 Ctrl +l 清屏 ...
- C++Primer笔记(1)
1.初始化 在C++中,初始化与赋值操作是完全不同的两个操作.初始化不是赋值,初始化的含义是创建变量时赋予其一个初始值,而赋值的含义是把对象的当前值擦除,而以一个新值来代替. 初始化的方式有: ; } ...
- (转)最小二乘法拟合圆公式推导及vc实现[r]
(下文内容为转载,不过已经不清楚原创的是哪里了,特此说明) 转自: http://www.cnblogs.com/dotLive/archive/2006/10/09/524633.html 该网址下 ...