Poj 1270 Following Orders【Dfs拓扑排序】

Following Orders

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 4571

 

Accepted: 1855

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

Source

Duke Internet Programming Contest 1993,uva 124

 

题目大意:

第一行输入一行字母,表示节点列表。第二行输入一行约束条件,每两个字母表示字母X要在字母Y的前边出现。输出全部符合条件的拓扑序,按照字典序从小到大输出。


思路:


1、从度为0的节点开始深搜,有关边删除,对应点度-1.选择当前度为0的节点,进行下一层深搜。


2、深搜回溯的时候,将边建回,对应的就是度+1.


3、从小到大枚举字母,满足字典序从小到大输出。


AC代码:


#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
vector<int >mp[10000];
int vis[150000];
int vis2[150000];
int degree[150000];
int degree2[150000];
int ans[150000];
char a[150000];
char b[150000];
int cnt;
void Dfs(int u,int cur)
{
    ans[cur]=u;
    if(cur==cnt-1)
    {
        for(int i=0;i<=cur;i++)
        {
            printf("%c",ans[i]+'a');
        }
        printf("\n");
    }
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        degree2[v]--;
    }
    for(int i=0;i<26;i++)
    {
        if(vis[i]==1&°ree2[i]==0&&vis2[i]==0)
        {
            vis2[i]=1;
            degree2[i]=-1;
            Dfs(i,cur+1);
            vis2[i]=0;
            degree2[i]=0;
        }
    }
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        degree2[v]++;
    }
}
void top()
{
    for(int i=0;i<26;i++)
    {
        if(degree[i]==0&&vis[i]==1)
        {
            for(int j=0;j<26;j++)
            {
                degree2[j]=degree[j];
            }
            degree2[i]=-1;
            memset(vis2,0,sizeof(vis2));
            Dfs(i,0);
        }
    }
}
void init()
{
    memset(vis2,0,sizeof(vis2));
    memset(vis,0,sizeof(vis));
    memset(degree2,0,sizeof(degree2));
    memset(degree,0,sizeof(degree));
    memset(ans,0,sizeof(ans));
    memset(degree,0,sizeof(degree));
    for(int i=0;i<=26;i++)mp[i].clear();
}
int main()
{
    while(gets(a))
    {
        init();
        gets(b);
        int n=strlen(a);
        cnt=0;
        for(int i=0;i<n;i++)
        {
            if(a[i]==' ')continue;
            else
            {
                vis[a[i]-'a']=1;
                cnt++;
            }
        }
        n=strlen(b);
        int u=-1,v=-1;
        for(int i=0;i<n;i++)
        {
            if(b[i]==' ')continue;
            if(u==-1)
            {
                u=b[i]-'a';
            }
            else
            {
                v=b[i]-'a';
                mp[u].push_back(v);
                degree[v]++;
                u=-1;
                v=-1;
            }
        }
        top();
        printf("\n");
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值