狗带拓扑

来源:互联网 发布:国外域名交易论坛 编辑:程序博客网 时间:2024/05/01 18:06
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>#include <cmath>#include <map>#include <set>using namespace std;const double pi = 3.14159265358979323846264338327950288419716939937511;const double eps = 1e-8;const int INF = 8000000;const int maxn = 110;bool vis[maxn];int c[maxn],n;int topo[maxn],t;bool G[maxn][maxn];bool dfs(int u){    c[u]=-1;//been visited    for(int v=1;v<=n;v++){        if(G[u][v]){            if(c[v]<0){            //cout<<"yuanyuanyuan\n";                return false;            }else if(!c[v]&&!dfs(v)){                return false;            }        }    }    c[u]=1;    topo[t--]=u;    return true;}bool topoSort(){    t = n;    memset(c, 0, sizeof(c));    for(int u=1;u<=n;u++){        if(!c[u]){            if(!dfs(u)){            //cout<<"wuwuuwuw\n";                return false;            }        }    }    return true;}void out(){    int i;    for(i=n;i>=1;i--){        cout<<(char)('A'+topo[i]-1);    }}int main(){freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);    int m;    while(scanf("%d%d",&n,&m)&&n){        string in;        int i;        t = n;        //memset(c,0,sizeof(c));        memset(G,0,sizeof(G));        memset(vis,false,sizeof(vis));        bool flag = true;        bool possible = true;        int determined;        int ans = 0;        for(i=1;i<=m;i++){            if(ans == n){                flag = false;                printf("Sorted sequence determined after %d relations:",i-1);                out();                 printf(".\n");                ans=INF;            }            cin>>in;                        if(flag){            if(vis[in[0]-'A']==false){                    ans ++;                    vis[in[0]-'A'] = true;}if(vis[in[2]-'A']==false){ans++;vis[in[2]-'A'] = true;}                if(in[1]=='>'){                    G[in[0]-'A'+1][in[2]-'A'+1]=true;                }else{                    G[in[2]-'A'+1][in[0]-'A'+1]=true;                }                   if(!topoSort()){                    possible = false;                    determined = i;                }            }        }        if(!possible){            printf("Inconsistency found after %d relations.\n", determined);            continue;        }        if(ans<n){        printf("Sorted sequence cannot be determined.\n");        continue;}    }    return 0;}

0 0