zoukankan      html  css  js  c++  java
  • AcWIng343 排序(传递闭包)

    使用floyd求取传递闭包,每次都进行判断

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N=27;
    int g[N][N];
    bool st[N];
    int n,m;
    int d[N][N];
    void floyd(){
        int i,j,k;
        for(k=1;k<=n;k++){
            for(i=1;i<=n;i++){
                for(j=1;j<=n;j++)
                    d[i][j]|=(d[i][k]&&d[k][j]);
            }
        }
    }
    int check(){
        int i,j;
        for(i=1;i<=n;i++)
            if(d[i][i])
            return 1;
        for(i=1;i<=n;i++){
            for(j=1;j<i;j++){
                if(!d[i][j]&&!d[j][i])
                    return 0;
            }
        }
        return 2;
    }
    char get_min(){
        int i,j;
        for(i=1;i<=n;i++){
            if(!st[i]){
                int flag=0;
                for(j=1;j<=n;j++){
                    if(!st[j]&&d[j][i]){
                        flag=1;
                        break;
                    }
                }
                if(!flag){
                    st[i]=1;
                    return 'A'+i-1;
                }
            }
        }
    }
    int main(){
        int i;
        while(cin>>n>>m){
            if(!n&&!m)
                break;
            memset(d,0,sizeof d);
            int type=0;
            char s[5];
            int t;
            for(i=1;i<=m;i++){
                scanf("%s",s);
                if(!type){
                    int tmp1=s[0]-'A'+1,tmp2=s[2]-'A'+1;
                    d[tmp1][tmp2]=1;
                    floyd();
                    type=check();
                    if(type)
                        t=i;
                }
            }
            if(!type){
                cout<<"Sorted sequence cannot be determined."<<endl;
            }
            else if(type==1){
                printf("Inconsistency found after %d relations.
    ", t);
            }
            else{
                memset(st, 0, sizeof st);
                printf("Sorted sequence determined after %d relations: ", t);
                for (int i = 1; i <= n; i ++ ) printf("%c", get_min());
                printf(".
    ");
            }
        }
    }
    View Code
  • 相关阅读:
    理解消息循环和窗口过程(转)
    对话框和控件编程(转)
    俄罗斯方块
    男生女生配(抽屉原理)
    翻转吧,字符串
    数塔
    Pseudoprime numbers伪素数(快速幂+判定素数)
    shǎ崽 OrOrOrOrz
    As Easy As A+B
    求素数(筛选法)
  • 原文地址:https://www.cnblogs.com/ctyakwf/p/12829638.html
Copyright © 2011-2022 走看看