UVa872 - Ordering(拓扑排序)

来源:互联网 发布:上众易网 知天下事 编辑:程序博客网 时间:2024/06/05 02:34

Ordering

Background

   Order is an important concept in mathematics and in computer science. For example, Zorns 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 Zorns Lemma nor fix-point semantics, but does involve order.

Problem

   Given a list of variable constraints of the form A < B, you are to write a program that prints all orderings of the variables that are consistent with the constraints. For example, given the contraints A < Band A < C there are two orderings of the variables AB and C that are consistent with these constraints:ABC and ACB.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

   The input consists of two lines: a list of variables on one line, followed by a list of constraints of the form A < B on the next line. Variables and contraints are separated by single spaces.
   All variables are single character, upper-case letters. There will be at least two variables, and no more than 20 variables. There will be at least one constraint, and no more than 50 constraints. There will be no more than 300 orderings consistent with the contraints in a specification.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

   All orderings consistent with the constraints should be printed. Orderings are printed in alphabetical order, one per line. Characters on a line are separated by a space. If no ordering is possible, the output is a single line with the word NO.

Sample Input

1

A B F G
A<B B<F

Sample Output

A B F G
A B G F
A G B F
G A B F

 

输出所有满足条件的拓扑排序,用并查集判断图中是否存在环

#include <cstdio>#include <cstring>#include <cctype>#include <map>using namespace std;const int N = 220;const int M = 30;char buf[N];bool vis[N];int t, n;int g[M][M];map<char, int> chMap;map<int, char> intMap;char ans[M];int p[N], r[N];void input();void toposort(int dep);bool isCycle();int find(int x);bool Union(int u, int v);void solve();int main(){#ifndef ONLINE_JUDGE    freopen("e:\\uva_in.txt", "r", stdin);#endif    int cas;    scanf("%d", &cas);    gets(buf);    while (cas--) {        input();        solve();        if (cas) printf("\n");    }    return 0;}void input(){    gets(buf);    gets(buf);    chMap.clear();    intMap.clear();    for (int i = 0, len = strlen(buf); i < len; i++) {        if (isalpha(buf[i])) {            int size = chMap.size();            chMap[buf[i]] = size;            intMap[size] = buf[i];        }    }    memset(g, 0x00, sizeof(g));    gets(buf);    for (int i = 0, len = strlen(buf); i < len; i += 4) {        char tmp[4];        sscanf(&buf[i], "%s", tmp);        int u = chMap[tmp[0]], v = chMap[tmp[2]];        g[u][v] = 1;    }    n = chMap.size();}int find(int x){    int root = x;    while (p[root] != root) {        root = p[root];    }    while (p[x] != root) {        int tmp = x;        x = p[x];        p[tmp] = root;    }    return root;}bool Union(int u, int v){    int pu = find(u), pv = find(v);    if (pu == pv) return false;    if (r[pu] < r[pv]) p[pu] = pv;    else {        p[pv] = pu;        if (r[pu] == r[pv]) r[pu]++;    }    return true;}bool isCycle(){    for (int i = 0; i < n; i++) {        p[i] = i;        r[i] = 0;    }    for (int i = 0; i < n; i++) {        for (int j = 0; j < n; j++) {            if (g[i][j]) {                if (!Union(i, j)) return false;            }        }    }    return true;}void toposort(int dep){    if (dep == n) {        for (int i = 0; i < n; i++) {            if (i) printf(" ");            printf("%c", ans[i]);        }        printf("\n");        return;    }    for (int i = 0; i < n; i++) {        if (vis[i]) continue;        int j;        for (j = 0; j < n; j++) {            if (j != i && !vis[j] && g[j][i]) break;        }        if (j >= n) {            vis[i] = true;            ans[dep] = intMap[i];            toposort(dep + 1);            vis[i] = false;        }    }}void solve(){    if (!isCycle()) {        printf("NO\n");        return;    }    memset(vis, false, sizeof(vis));    toposort(0);}



0 0