zoukankan      html  css  js  c++  java
  • poj 1270 Following Orders (拓扑排序+回溯)

    Following Orders
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 5473   Accepted: 2239

    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

    题目大意:

    给你一个有向无环图,请你按字典序输出它所有的toposort结果。

    回溯法。

    回溯时如果使用了全局变量,要在递归出口处立即还原该全局变量,如ac代码中的vis数组和path数组。

    此题输入输出好坑啊。用gets才行,I also not kow why。

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<stack>
    typedef long long ll;
    const int maxn=10000;
    
    char str1[100],str2[300];
    int exis[26];//26个lowercase字母
    int tot;//一共存在多少字母
    int cnt;
    int to[100];
    int next[100];
    int head[26];
    int in[26];//入度
    int vis[26];
    char path[30];
    
    void dfs(int x,int m)
    {
        if(m==tot-1)
            printf("%s
    ",path);
        for(int i=head[x];i!=-1;i=next[i])
        {
            int l=to[i];
            if(!vis[l])
                in[l]--;
        }
        for(int i=0;i<26;i++)
        {
            if(!vis[i]&&exis[i]&&in[i]==0)
            {
                vis[i]=1;
                path[m+1]='a'+i;
                dfs(i,m+1);
                path[m+1]='';
                vis[i]=0;
            }
        }
        for(int i=head[x];i!=-1;i=next[i])
        {
            int l=to[i];
            if(!vis[l])
                in[l]++;
        }
    }
    
    int main()
    {
        int k=0;
        while(gets(str1))
        {
            gets(str2);
            if(k>0)
                printf("
    ");
            k++;
    
            memset(exis,0,sizeof(exis));
            tot=0;
            for(int i=0;str1[i]!='';i++)
            {
                if(str1[i]>='a'&&str1[i]<='z')
                {
                    exis[str1[i]-'a']=1;
                    tot++;
                }
            }
            cnt=0;
            memset(head,-1,sizeof(head));
            memset(in,0,sizeof(in));
            for(int i=0;str2[i]!='';i+=4)
            {
                int u=str2[i]-'a',v=str2[i+2]-'a';
                to[cnt]=v;next[cnt]=head[u];head[u]=cnt++;
                in[v]++;
            }
    
            memset(vis,0,sizeof(vis));
            for(int i=0;i<30;i++)
                path[i]='';
            for(int i=0;i<26;i++)
            {
                if(exis[i]&&in[i]==0)
                {
                    vis[i]=1;
                    path[0]='a'+i;
                    dfs(i,0);
                    path[0]='';
                    vis[i]=0;
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    使用SQL语句创建SQL数据脚本(应对万网主机部分不支持导出备份数据)
    js和jquery页面初始化加载函数的方法及先后顺序
    熔断器原理
    List<T>线性查找和二分查找BinarySearch效率分析
    ASP.NET资源大全-知识分享 【转载】
    C#语法——委托,架构的血液
    SUPERSOCKET 客户端
    VS 中的几种注释方法
    计算机专业术语中英文对照
    2018服务端架构师技术图谱
  • 原文地址:https://www.cnblogs.com/acboyty/p/9974644.html
Copyright © 2011-2022 走看看