POJ 1270 Following Orders 拓扑排序全输出

Description

Order is an important concept in mathematics and in computer science. For example, Zorn‘s Lemma states: ``a partially ordered set in which every chain has an upper bound contains a maximal element.‘‘ Order is also important in reasoning about the fix-point semantics of programs.

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

The 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

For 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
题意:给出一些字母 , 和字母间关系 ,让你输出所有符合关系的排序分析:相当于是按字典序输出所有的拓扑排序情况。

鉴于要输出所有情况,那么我在拓扑排序时给每个字母设置一个等级,在用next_permutation找到所有排列,只要复合拓扑排序规则,这个排列一定是等级递增关系,直接输出。本以为这样做会T,没想到数据太弱,0msAC。
  1 #include<cstring>
  2 #include<cstdio>
  3 #include<iostream>
  4 #include<algorithm>
  5 #include<queue>
  6 #include<map>
  7 #include<vector>
  8 #include<cstdlib>
  9
 10 using namespace std;
 11
 12 char a[105],b[105];
 13 int vis[105];
 14 int in[105];
 15 vector<int>out[105];
 16 vector<int>chat;
 17 vector<int>temp;
 18
 19 void init()
 20 {
 21     for(int i=0;i<26;i++)
 22     {
 23         in[i]=0;
 24         out[i].clear();
 25         vis[i]=-2;
 26     }
 27     chat.clear();
 28     temp.clear();
 29 }
 30
 31 void toposort()
 32 {
 33     int num=0;
 34     int cur=0;
 35     for(int i=0;i<chat.size();i++) if(vis[chat[i]]==-2) num++;
 36
 37     while(num)
 38     {
 39
 40         for(int i=0;i<chat.size();i++)
 41         {
 42             if(in[chat[i]]==0&&vis[chat[i]]!=-1)
 43             {
 44                 in[chat[i]]--;
 45                 temp.push_back(chat[i]);
 46             }
 47         }
 48
 49         for(int i=0;i<temp.size();i++)
 50         {
 51             int k=temp[i];
 52             vis[k]=cur;
 53             for(int j=0;j<out[k].size();j++)
 54             {
 55                 in[out[k][j]]--;
 56             }
 57         }
 58
 59         num-=temp.size();
 60         temp.clear();
 61         cur++;
 62
 63     }
 64 }
 65
 66 bool judge()
 67 {
 68     int maxn=0;
 69     for(int i=0;i<chat.size();i++)
 70     {
 71         if(vis[chat[i]]!=-1)
 72         {
 73             if(vis[chat[i]]>=maxn) maxn=vis[chat[i]];
 74             else return false;
 75         }
 76     }
 77     return true;
 78 }
 79
 80 int main()
 81 {
 82     int black=1;
 83     while(gets(a)!=NULL)
 84     {
 85         gets(b);
 86
 87         if(black==0) printf("\n");
 88         init();
 89
 90         int len=strlen(a);
 91         for(int i=0;i<len;i++)
 92         {
 93             if(a[i]>=‘a‘&&a[i]<=‘z‘)
 94             {
 95                 chat.push_back(a[i]-‘a‘);
 96             }
 97         }
 98
 99         len=strlen(b);
100         char k=‘#‘;
101         for(int i=0;i<len;i++)
102         {
103             if(b[i]>=‘a‘&&b[i]<=‘z‘)
104             {
105                 if(k==‘#‘) k=b[i];
106                 else
107                 {
108                     in[b[i]-‘a‘]++;
109                     out[k-‘a‘].push_back(b[i]-‘a‘);
110                     k=‘#‘;
111                 }
112             }
113         }
114
115         for(int i=0;i<chat.size();i++)
116             if(!in[chat[i]]&&!out[chat[i]].size()) vis[chat[i]]=-1;
117
118         toposort();
119
120         sort(chat.begin(),chat.end());
121         do
122         {
123             if(judge())
124             {
125                 for(int i=0;i<chat.size();i++)
126                     printf("%c",chat[i]+‘a‘);
127                 puts("");
128             }
129         }while(next_permutation(chat.begin(),chat.end()));
130
131         black=0;
132     }
133 }
时间: 2024-08-09 02:46:42

POJ 1270 Following Orders 拓扑排序全输出的相关文章

poj 1270 Following Orders(拓扑排序+dfs)

大致题意:每个样例包含两行,第一行输入n个字符,可能是无序的.第二行输入成对的a b,代表a要在b前面.输出所有的符合这样的序列. 思路:很明显的拓扑排序.要输出所有的序列,那么就从入度为0的点进行dfs,每次选择一个入度为0的点,加入输出序列并把与它相邻的点的入度减一.dfs结束后要把状态再改回来. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include

UVA 124 &amp; POJ 1270 Following Orders(拓扑排序)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=60 http://poj.org/problem?id=1270 Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3806   Accepted: 1507 Description Or

[ACM] POJ 3687 Labeling Balls (拓扑排序,逆向建边)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

POJ 2367 Genealogical tree 拓扑排序入门

Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so that a Martian can have one parent as well as ten. Nobody will be surpris

POJ 1270 Following Orders

Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4902   Accepted: 1982 Description Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma states: ``a partially ordered set in which

[ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 toN in such a way that: No two balls share

POJ 2367 (裸拓扑排序)

http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我也是刚刚才接触,想法还是挺简单的.实现起来也不复杂. 1 #include <stdio.h> 2 #include <string.h> 3 4 int Indegree[101],n; 5 6 bool mp[101][101]; 7 8 int topsort() 9 { 10

poj 1270 Following Orders 枚举排列

题意: 给一个字符集和一些字符之间的小于关系,求字符集上的所有可能排列. 分析: 暴力枚举可以分为枚举子集,枚举排列,枚举组合,这题是个简单的枚举排列,枚举过程中用小于关系剪枝即可. 代码: //poj 1270 //sep9 #include <iostream> #include <algorithm> using namespace std; char vars[64],constraint[256],ans[64]; int g[128][128],vis[256]; in

poj 2367 Genealogical tree(拓扑排序)

本题链接:点击打开链接 本题大意: 首先输入一个N,表示有N行,也表示有N个点,编号从1到N,接下来有N行,第i行输入的数据代表这些编号要在i的前面,每行输入0表示结束,求一种符合题意的拓扑序列.解题思路: 此题就是基本的拓扑排序,弄清楚题意应该就不难了吧,具体请参考代码: #include<stdio.h> #include<string.h> #define INF 0x3f3f3f3f #define MAX 220 int indegree[MAX]; int head[M