首页 > 其他分享 >P1347 排序

P1347 排序

时间:2024-03-21 13:57:37浏览次数:21  
标签:ends int 30 b1 排序 P1347

原题链接

题解

1.\(A<B\) \(\to\) 建立一条A向B的边
2.由于数据范围小,所以可以输入一次进行一次拓扑遍历
3.如果存在矛盾,说明存在环
4.对于拓扑排序进行层次标记,如果最大层等于n,代表每个字母层次分明,有先后次序

code

#include<bits/stdc++.h>
using namespace std;
vector<int> G[30];
int in_copy[30]={0};
int in[30]={0};
struct node
{
    int id,layer;
};
int n,m;
int tuopu()
{
    queue<node> q;
    for(int i=0;i<n;i++) if(!in_copy[i]&&G[i].size()) q.push({i,1});

    int maxlayer=0;
    while(q.size())
    {
        int now=q.front().id,layer=q.front().layer;
        q.pop();

        maxlayer=max(maxlayer,layer);
        for(auto next:G[now])
        {
            in_copy[next]--;
            if(!in_copy[next]) q.push({next,layer+1});
        }
    }

    for(int i=0;i<26;i++) if(in_copy[i])return -1;

    //printf("%d  %d\n",maxlayer,n);
    return (maxlayer==n);
}

void print()
{
    queue<int> q;
    for(int i=0;i<26;i++) if(!in[i]&&G[i].size()) q.push(i);

    while(q.size())
    {
        int now=q.front();
        q.pop();
        printf("%c",now+65);
        for(auto next:G[now])
        {
            in[next]--;
            if(!in[next]) q.push(next);
        }
    }
}
int main()
{
    cin>>n>>m;

    int ends=0;
    for(int i=1;i<=m;i++)
    {
        char a,op,b;
        cin>>a>>op>>b;
        if(ends) continue;
        int a1=a-'A',b1=b-'A';
        G[a1].emplace_back(b1);
        in[b1]++;
        for(int i=0;i<26;i++) in_copy[i]=in[i];

        int tag=tuopu();

        if(tag==-1)
        {
            printf("Inconsistency found after %d relations.\n",i);
            ends=1;
        }
        else if(tag==1)
        {
            printf("Sorted sequence determined after %d relations: ",i);
            print();
            puts(".");
            ends=1;
        }
    }

    if(!ends) printf("Sorted sequence cannot be determined.");
    return 0;
}

标签:ends,int,30,b1,排序,P1347
From: https://www.cnblogs.com/pure4knowledge/p/18087207

相关文章

  • 【数据结构和算法初阶(C语言)】二叉树的顺序结构--堆的实现/堆排序/topk问题详解---二
     目录 ​编辑1.二叉树的顺序结构及实现1.1二叉树的顺序结构2堆的概念及结构3堆的实现3.1堆的代码定义3.2堆插入数据3.3打印堆数据3.4堆的数据的删除3.5获取根部数据3.6判断堆是否为空3.7堆的销毁 4.建堆以及堆排序 4.1堆排序---是一种选择排序4.2升......
  • qsort实现函数排序(2)
    qsort实现结构体排序#include<stdio.h>#include<stdlib.h>#include<string.h>structstu{ charname[20]; intage;};intcmp_by_name(void*p1,void*p2){ returnstrcmp(((structstu*)p1)->name,((structstu*)p2)->name);}voidprint(s......
  • 插入排序
    插入排序1.解决的问题在已经排好序的序列中,插入一个新元素,让序列依旧保持有序,如优先级队列2.核心知识0个或者1个元素,已经是排好序的交换位置的条件(升序):当前元素比后者大(sequeue[i]>sequeue[i+1])当前元素比前者小(sequeue[i]<sequeue[i-1])2层循环第1层:准......
  • c#实现图的拓扑排序
    原文链接:https://blog.csdn.net/MfuuJava/article/details/132933517拓扑排序是一种在有向无环图(DAG)中对节点进行排序的算法。在C#中,我们可以使用深度优先搜索(DFS)和拓扑排序算法来解决这个问题。深度优先代码:usingSystem;usingSystem.Collections.Generic;classGraph......
  • 归并排序算法 java实现
    publicstaticvoidmain(String[]args){int[]arr={9,5,7,3,1,6,8,4,2};mergeSort(arr,0,arr.length-1);}/***归并排序*注意:归并的拆分数组和合并数组是从左到右依次进行的,网上很多文章都是错误的*并不是左右一起拆分,网上很多文章都是这样的......
  • 11--插入排序
    算法描述:从当前位置开始,从后往前找比当前数字小的,插入到这个小的数字的后面,在找的过程中,如果发现一个比当前数字大,同时将这个数字往后挪动,其中从后往前是重点。插入排序的时间复杂度是,特别地,若完全有序则时间复杂度为空间复杂度为,并且它是稳定的。插入排序的特点:越有序越快......
  • 冒泡、选择排序;二维数组;函数三要素,形参实参
    冒泡排序法012max08,12,13,98,12,13,98,12,9,131318,12,98,9,121228,993第一轮从前往后两两比较,4个元素比较3次,得出最大值为13。第二轮,3个元素比较2次,最大值为12。第三轮,2个元素比较1次,最大值为9。通过简单较少的数据推导得出结论,i个元素需要比较i-1轮,第j轮需要比较i-1......
  • C# List排序
    先是Y_FaceItem类usingDlibDotNet;usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassY_FaceItem:MonoBehaviour{publicfloatfinalValue;publicTexture2DfaceTextrue;publicintid;publicstring......
  • Python基础----冒泡排序和二分查找(持续更新中)
    冒泡排序(共3个版本)在冒泡排序中,一共有3个版本,经常使用的是v3.0,v3.0版本的精彩之处就在于b=false和b=true只要把这点搞懂,那么冒泡排序就没什么难的!!!li=[5,4,7,8,2,1]#冒泡排序的v1.0版本forjinrange(len(li)-1):foriinrange(len(li)-1):......
  • 【堆排序】(大根堆)
    敬信仰赤诚,敬少年滚烫的情钟voidheapify(inta[],intn,inti){//根据当前节点i进行堆调整,保证以i为根节点的子树符合最大堆的性质intlargest=i;//假设当前节点为最大值的索引intleft=2*i+1;//计算左孩子节点索引intright=2*......