首页 > 其他分享 >POJ 1094Sorting It All Out(拓扑排序)

POJ 1094Sorting It All Out(拓扑排序)

时间:2023-04-13 21:37:35浏览次数:44  
标签:cnt 30 int 1094Sorting head edge POJ include Out


题目地址:http://poj.org/problem?id=1094

这个题改了一下午。。代码越改越挫。。凑活着看吧。。


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;
int d[30], head[30], n, cnt, a[30], num, s, hash1[30], y;
struct node
{
    int u, v, w;
    int next;
} edge[100000];
void add(int u, int v, int w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int tops()
{
    int i, j, flag=0, x, u, z=0;
    s=0;
    while(1)
    {
        x=0;
        for(j=1; j<=n; j++)
        {
            if(hash1[j]&&d[j]==0)
            {
                a[s]=j;
                u=j;
                d[j]--;
                x++;
                s++;
            }
        }
        if(x==0&&s==n)
        {
            if(z==0)
            flag=1;
            break;
        }
        if(x==0&&s<y)
        {
            flag=2;
            break;
        }
        if(x==0&&s==y)
        {
            break;
        }
        if(x>=1)
        {
            if(x>1)
                z=1;
            for(i=0; i<s; i++)
            {
                for(j=head[a[i]]; j!=-1; j=edge[j].next)
                {
                    if(edge[j].w==1)
                    {
                        edge[j].w--;
                        d[edge[j].v]--;
                    }
                }
            }
        }
    }
    return flag;
}
int main()
{
    int m, i, flag, k, b[1000], dd[1000], yy;
    char c1, c2, c;
    //freopen("1.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF&&n&&m)
    {
        cnt=0;
        num=0;
        flag=0;
        memset(hash1,0,sizeof(hash1));
        while(m--)
        {
            cnt=0;
            num++;
            getchar();
            scanf("%c%c%c",&c1,&c,&c2);
            //printf("--1%c %c\n",c1,c2);
            b[num-1]=c1-'A'+1;
            //printf("--2%c %c\n",c1,c2);
            dd[num-1]=c2-'A'+1;
            //printf("--3%c %c\n",c1,c2);
            memset(head,-1,sizeof(head));
            memset(d,0,sizeof(d));
            //printf("--4%c %c\n",c1,c2);
            for(i=0; i<num-1; i++)
            {
                d[dd[i]]++;
                add(b[i],dd[i],1);
            }
            d[c2-'A'+1]++;
            add(c1-'A'+1,c2-'A'+1,1);
            hash1[c1-'A'+1]++;
            hash1[c2-'A'+1]++;
            y=0;
            for(i=1; i<=n; i++)
            {
                if(hash1[i])
                {
                    y++;
                }
            }
            if(!flag)
            {
                yy=tops();
                if(yy==1)
                {
                    k=num;
                    flag=1;
                }
                else if(yy==2)
                {
                    k=num;
                    flag=2;
                }
            }
        }
        if(flag==0)
            printf("Sorted sequence cannot be determined.\n");
        else if(flag==1)
        {
            printf("Sorted sequence determined after %d relations: ",k);
            for(i=0; i<n; i++)
                printf("%c",a[i]+'A'-1);
            printf(".\n");
        }
        else
            printf("Inconsistency found after %d relations.\n",k);
    }
    return 0;
}



标签:cnt,30,int,1094Sorting,head,edge,POJ,include,Out
From: https://blog.51cto.com/u_16070138/6188411

相关文章

  • POJ 2584 T-Shirt Gumbo(网络流之最大流)
    题目地址:POJ2584大水题。。不多说。。上代码#include<iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<ctype.h>#include<queue>#include<map>#include<algorithm>usingn......
  • POJ 2001 Shortest Prefixes(字典树)
    题目地址:POJ2001考察的字典树,利用的是建树时将每一个点只要走过就累加。最后从根节点开始遍历,当遍历到只有1次走过的时候,就说明这个地方是最短的独立前缀。然后记录下长度,输出即可。代码如下:#include<iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#inc......
  • POJ 1182 食物链(种类并查集)
    题目地址:POJ1182一道很经典的种类并查集的题目。利用互相之间的关系来进行权值的维护。代码如下:#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<stdlib.h>#include<math.h>#include<ctype.h>#include<queue>#in......
  • POJ 1703 Find them, Catch them(种类并查集)
    题目地址:POJ1703种类并查集水题。代码如下:#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<stdlib.h>#include<math.h>#include<ctype.h>#include<queue>#include<map>#includ......
  • POJ 1988 Cube Stacking (种类并查集)
    题目地址:POJ1988   这道题的查找合并的方法都能想的到,就是一点没想到,我一直天真的以为查询的时候,输入后能马上输出,这样的话在合并的时候就要所有的结点值都要算出来,但是经过路径压缩之后,没办法全部都处理到,如果不压缩妥妥的TLE。。于是看了看网上的题解。才发现自己是多......
  • Visual Stadio 编译提示 The BaseOutputPath/OutputPath property is not set for pr
    完整的错误信息是:TheBaseOutputPath/OutputPathpropertyisnotsetforproject'xx.csproj'.PleasechecktomakesurethatyouhavespecifiedavalidcombinationofConfigurationandPlatformforthisproject.Configuration='Debug'Plat......
  • Java常用实体类介绍:POJO、Domain、DO、DTO、VO
    POJOPOJO是PlainOldJavaObject的简称,它指的是一个没有限制或要求下的纯平对象。POJO用于表示没有任何框架或技术限制的纯数据对象。在Java开发中,POJO通常用于简化复杂对象和降低对象的耦合度,是面向对象编程中"高内聚、低耦合"设计思想的体现。示例代码:@Datapublic......
  • org.dom4j.DocumentException: Connection timed out: connect Nested exception: Con
    今天用dom4j解析xml文件时遇到了一个问题org.dom4j.DocumentException:Connectiontimedout:connectNestedexception:Connectiontimedout:connect atorg.dom4j.io.SAXReader.read(SAXReader.java:484) atorg.dom4j.io.SAXReader.read(SAXReader.java:321) atcom......
  • 用quasar+vue3+组合式api VueRouter实现路由嵌套(二级路由)
    前言:本项目使用的是quasar创建,vue3的组合式api语法。部分语法不同,但不影响理解,修改语法后可以在vue2/选项式api项目中运行。效果图:文件目录结构和代码如下:   文中用到的标题栏数据如下:consttitles=ref([{name:"首页",path:"home",children:[]},{......
  • CS231N assignment 2#3 _ dropout 学习笔记 & 解析
    dropout定义&作用&基本实现如课程所说,dropout最大的意义在于防止过拟合.我们还记得,dropout在网络架构上介于激活函数之后,下一层输入之前.想法很简单,就是将隐含层的某些数据屏蔽掉,直接从以输入到下一层,概率为p. 需要注意的是,dropout是仅针对训练而言的,测试......