zoukankan      html  css  js  c++  java
  • POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094

    #include <cstdio>
    #include <cstring>
    #include <vector>
    using namespace std;
    int in[27],out[27];
    char index[27];
    bool vis[27];
    int mem[27][27];
    int n,m;
    int tlen;
    bool floyd(){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                for(int k=0;k<n;k++){
                    if(mem[i][k]&&mem[k][j])mem[i][j]=1;
                }
            }
        }
        for(int i=1;i<n;i++){
            if(mem[i][i])return false;
        }
        return true;
    }
    bool calc(){
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
            if(mem[i][j]==1){
                out[i]++;
                in[j]++;
            }}
        }
        for(int i=0;i<n;i++){
            if(in[i]+out[i]!=n-1)return false;
        }
        return true;
    }
    bool topologicalsort(){
        memset(vis,0,sizeof(vis));
        int len=0;
        bool fl=false;
        while(len<n){
            fl=false;
            for(int i=0;i<n;i++){
                if(in[i]==0&&!vis[i]){
                    fl=true;
                    vis[i]=true;
                    index[len]=i+'A';
                    len++;
                    for(int j=0;j<n;j++){
                        if(mem[i][j])in[j]--;
                    }
                    break;
                }
            }
            if(!fl)return false;
        }
        return true;
    }
    int main(){
        char ch,ch2;
        while (scanf("%d %d",&n,&m)==2&&n){
            bool fl=false;
            getchar();
            memset(mem,0,sizeof(mem));
            memset(index,0,sizeof(index));
            for(int i=1;i<=m;i++){
                scanf(" %c< %c",&ch,&ch2);
                in[ch2-'A']++;
                out[ch-'A']++;
                mem[ch-'A'][ch2-'A']=1;
                getchar();
                if(fl)continue;
                if(floyd()){
                    if(calc()){
                    if(topologicalsort()){
                        fl=true;
                        index[n]=0;
                        printf("Sorted sequence determined after %d relations: %s.
    ",i,index);
                        continue;
                    }
                    }
                }
                else {
                    printf("Inconsistency found after %d relations.
    ",i);
                    fl=true;
                    continue;
                }
            }
            if(!fl)printf("Sorted sequence cannot be determined.
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    【leetcode】Binary Search Tree Iterator
    【leetcode】Palindrome Partitioning II
    【leetcode】Best Time to Buy and Sell Stock III
    【leetcode】Best Time to Buy and Sell Stock II
    【leetcode】Longest Consecutive Sequence
    【leetcode】Factorial Trailing Zeroes
    【leetcode】Simplify Path
    【leetcode】Generate Parentheses
    【leetcode】Combination Sum II
    【leetcode】Combination Sum
  • 原文地址:https://www.cnblogs.com/xuesu/p/4755002.html
Copyright © 2011-2022 走看看