poj1094-Sorting It All Out-拓扑排序

题意:

1).给你一些大写字母,共n个;大写字母间有m条关系;

2).举例:关系:A<B,意思就是A要排在B的前面(也就是说可能B排在A的前面

3).输出:有三种情况:

    1.n个字母在前 i 条关系下可以确定排序关系;

    2.n个字母在执行到第 i 条命令时出现错误,即回路:A间接或直接小于B 且 B间接或直接小于A;

    3.m条命令不能确定n个字母的排序。

 

思路:

    直观感觉就是拓扑排序,需要注意的是,每读入一条命令就要拓扑排序一次。

    如果你对拓扑排序不够了解,请看这篇博客:点击查看

    而三种情况判断的顺序更为重要!!

    情况1和2可以同时判断,最后判断情况3!!

    意思是:

        如果执行到第 i 条命令时,可以确定情况1 或2,则直接输出;

        如果m条命令执行完了,还不能确定字母顺序,则输出情况3

 

题目链接:

  点击做题

AC代码:

#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<map>  
#include<queue>  
#include<set>  
#include<string>  
#include<stack>  
#include<cmath>  
#define test printf("***\n")  
#define mm1(a) memset((a),-1,sizeof((a)))  
#define mm0(a) memset((a),0,sizeof((a)))  
#define mmx(a) memset((a),0x3f,sizeof((a)))  
#define ka getchar();getchar()  
#define ka1 getchar()  
#define iis std::ios::sync_with_stdio(false)  
using namespace std;  
typedef long long LL;  
typedef unsigned long long uLL;  
const int N = 105;  
const int M = 4100005;  
const int X = 999983;  
const int INF = 1e9;  
const double eps = 1e-8;  
const int mod = 1e9 + 7;  
//#define DEBUG  
int n,m,sum;  
int tot,flag;  
int in[N],head[N],tin[N];  
int ar[N];  
struct lp  
{  
    int u,v,nex;  
    lp(){}  
    lp(int a,int b,int c):  
    u(a),v(b),nex(c){}  
}cw[N*N];  
inline void add(int a,int b){  
    cw[++tot]=lp(a,b,head[a]);  
    head[a]=tot;  
}  
int tuopu(){  
    queue<int>Q;  
    while(!Q.empty())Q.pop();  
    for(int i=0;i<=n;++i)tin[i]=in[i];  
    for(int i=1;i<=n;++i){  
        if(tin[i]==0){  
            Q.push(i);  
        }  
    }  
    int t=0;  
    int ans=0;  
    while(!Q.empty()){  
        if(Q.size()>1)ans=-1;  
        int u=Q.front();Q.pop();  
        ar[t++]=u;  
        for(int i=head[u];i!=-1;i=cw[i].nex){  
            int v=cw[i].v;  
            tin[v]--;  
            if(tin[v]==0){  
                Q.push(v);  
            }  
        }  
    }  
    if(t!=n)return 1;  
    return ans;  
}  
inline void init(){  
    tot=-1;  
    mm0(in);  
    mm1(head);  
}  
int main(int argc, char const *argv[])  
{  
    #ifdef DEBUG  
        freopen("D:in.in", "r", stdin);  
        freopen("D:out.out", "w", stdout);    
    #endif  
    char a,b;  
    while(~scanf("%d %d",&n,&m)&&(n+m)){  
        init();  
        int ans=1;  
        for(int i=0;i<m;++i){  
            char s[5];  
            scanf("%s",s);  
            a=s[0];b=s[2];  
            if(ans==0)continue;  
            in[b-'A'+1]++;  
            add(a-'A'+1,b-'A'+1);  
            int ok=tuopu();  
            if(ok==1){  
                ans=0;  
                printf("Inconsistency found after %d relations.\n",i+1);  
            }else if(ok==0){  
                ans=0;  
                printf("Sorted sequence determined after %d relations: ",i+1);  
                for(int i=0;i<n;++i){  
                    printf("%c", ar[i]+'A'-1);  
                }  
                printf(".\n");  
            }  
        }  
        if(ans){  
            printf("Sorted sequence cannot be determined.\n");  
        }  
    }  
    return 0;  
}  
View Code

 

posted @ 2018-04-13 16:33  Cwolf9  阅读(157)  评论(0编辑  收藏  举报