zoukankan      html  css  js  c++  java
  • POJ 1094 SORTING IT ALL OUT

     拓扑排序和机智的判环判合法。

    如果每次入队的点大于1个即为有多个拓扑序。如果该次没有点入队就是环。

    没时间写代码了,粘的题解。

    #include<stdio.h>
    #include<string.h>
    int map[27][27],indegree[27],q[27];
    int TopoSort(int n)
    {
    int c=0,temp[27],loc,m,flag=1,i,j;
    for(i=1;i<=n;i++)
    temp[i]=indegree[i];
    for(i=1;i<=n;i++)
    {
    m=0;
    for(j=1;j<=n;j++)
    if(temp[j]==0) { m++; loc=j; }
    if(m==0) return 0;
    if(m>1) flag=-1;
    q[c++]=loc;
    temp[loc]=-1;
    for(j=1;j<=n;j++)
    if(map[loc][j]==1) temp[j]--;
    }
    return flag;
    }

    int main()
    {
    int m,n,i,sign;
    char str[5];
    while(scanf("%d%d",&n,&m))
    {
    if(m==0&&n==0) break;
    memset(map,0,sizeof(map));
    memset(indegree,0,sizeof(indegree));
    sign=0;
    for(i=1;i<=m;i++)
    {
    scanf("%s",str);
    if(sign) continue;
    int x=str[0]-'A'+1;
    int y=str[2]-'A'+1;
    map[x][y]=1;
    indegree[y]++;
    int s=TopoSort(n);
    if(s==0)
    {
    printf("Inconsistency found after %d relations. ",i);
    sign=1;
    }
    if(s==1)
    {
    printf("Sorted sequence determined after %d relations: ",i);
    for(int j=0;j<n;j++)
    printf("%c",q[j]+'A'-1);
    printf(". ");
    sign=1;
    }
    }
    if(!sign)
    printf("Sorted sequence cannot be determined. ");
    }
    return 0;
    }

  • 相关阅读:
    macOS 上配置 Lua
    Oracle.ManagedDataAccess.dll
    offer
    Costura.Fody
    日志系统
    实战框架ABP
    什么是算法?
    HTTP状态码->HTTP Status Code
    How to untar a TAR file using Apache Commons
    python实践3:cursor() — 数据库连接操作
  • 原文地址:https://www.cnblogs.com/ziliuziliu/p/5065212.html
Copyright © 2011-2022 走看看