NYOJ 349 Sorting It All Out (拓扑排序 )

本文介绍了一种利用拓扑排序解决特定序列问题的方法。通过分析输入的关系,判断是否能够确定一个升序排列的序列,或者是否存在不一致的情况。文章详细解释了拓扑排序的基本思想,并提供了一个具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

描述

An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.

  • 输入
    Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.
  • 输出
    For each problem instance, output consists of one line. This line should be one of the following three: Sorted sequence determined after xxx relations: yyy...y. Sorted sequence cannot be determined. Inconsistency found after xxx relations. where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence. 
  • 样例输入
    4 6
    A<B
    A<C
    B<C
    C<D
    B<D
    A<B
    3 2
    A<B
    B<A
    26 1
    A<Z
    0 0
  • 样例输出
    Sorted sequence determined after 4 relations: ABCD.
    Inconsistency found after 2 relations.
    Sorted sequence cannot be determined.

分析:

首先补充一下拓扑排序的思想:

(1)从有向图中选择一个没有前驱(入度为0)的顶点并输出它。

(2)从图中删除该节点,并且删去从该节点出发的全部有向边。

(3)重复上述操作,知道图中不在存在没有前驱的顶点为止。

这样操作的结果有两种:一种是图中全部定点被输出,这说明图中不存在有向回路;另一种是图中顶点未被全部输出,剩余的顶点均有前驱节点,这说明图中存在有向回路。

这就是一个典型的拓扑排序的应用,如果能够排好序的话,说明就是可以的,如果形成环的话,说明能够构成回路也就是存在冲突,否则就是不能够排好序。

代码:

#include<stdio.h>
#include<iostream>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
int du[30];
char ch[30];
int n,m,k;
vector<int> v[30];
int init()
{
    memset(du,0,sizeof(du));
    memset(v,0,sizeof(v));
}
int topSort()
{
    int op=1;
    k=0;
    queue<int >q;
    int du1[30];
    for(int i=0; i<n; i++)///要把入度的函数复制一下,不然会影响下次的排序
    {
        du1[i]=du[i];
        if(du1[i]==0)
            q.push(i);
    }
    while(!q.empty())
    {
        //cout<<"  ----"<<q.size()<<endl;
        if(q.size()>1) op=0;///相当于有多个入度为0的点,也就是还没有排好序
        int a=q.front();
        q.pop();
        char c=a+'A';
        ch[k++]=c;
        // cout<<"k="<<k<<endl;
        for(int i=0; i<v[a].size(); i++)
        {
            int b=v[a][i];
            du1[b]--;
            if(du1[b]==0)
                q.push(b);
        }
    }
    if(k<n)///形成环,如果没有形成环且能排好序的话,肯定每一个点都要入队一次
        return -1;
    return op;///op==1,已经排好序;op=1,还没有排好
}
int main()
{
    char ch2,ch1;
    int a,b;
    while(~scanf("%d%d",&n,&m),n,m)
    {
        init();
        int flag=0;
        for(int mm=1; mm<=m; mm++)
        {
            scanf(" %c<%c",&ch1,&ch2);
            if(flag!=0) continue;///已经确定当前的序列是有序或者已经发生冲突
            a=ch1-'A';
            b=ch2-'A';
            // cout<<a<<"   "<<b<<endl;
            v[a].push_back(b);///单向
            du[b]++;
            flag=topSort();///每次加入一个都要进行一次判断,看能否满足某个条件
            if(flag==1)///已经排好序
            {
                printf("Sorted sequence determined after %d relations: ",mm);
                for(int k1=0; k1<k; k1++)
                {
                    printf("%c",ch[k1]);
                }
                printf(".\n");
            }
            if(flag==-1)///发生冲突
            {
                printf("Inconsistency found after %d relations.\n",mm);
            }
        }
        if(flag==0)///到最后还没有排好序
        {
            printf("Sorted sequence cannot be determined.\n");
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/cmmdc/p/6780805.html

资源下载链接为: https://pan.quark.cn/s/9648a1f24758 这个HTML文件是一个专门设计的网页,适合在告白或纪念日这样的特殊时刻送给女朋友,给她带来惊喜。它通过HTML技术,将普通文字转化为富有情感和创意的表达方式,让数字媒体也能传递深情。HTML(HyperText Markup Language)是构建网页的基础语言,通过标签描述网页结构和内容,让浏览器正确展示页面。在这个特效网页中,开发者可能使用了HTML5的新特性,比如音频、视频、Canvas画布或WebGL图形,来提升视觉效果和交互体验。 原本这个文件可能是基于ASP.NET技术构建的,其扩展名是“.aspx”。ASP.NET是微软开发的一个服务器端Web应用程序框架,支持多种编程语言(如C#或VB.NET)来编写动态网页。但为了在本地直接运行,不依赖服务器,开发者将其转换为纯静态的HTML格式,只需浏览器即可打开查看。 在使用这个HTML特效页时,建议使用Internet Explorer(IE)浏览器,因为一些老的或特定的网页特效可能只在IE上表现正常,尤其是那些依赖ActiveX控件或IE特有功能的页面。不过,由于IE逐渐被淘汰,现代网页可能不再对其进行优化,因此在其他现代浏览器上运行可能会出现问题。 压缩包内的文件“yangyisen0713-7561403-biaobai(html版本)_1598430618”是经过压缩的HTML文件,可能包含图片、CSS样式表和JavaScript脚本等资源。用户需要先解压,然后在浏览器中打开HTML文件,就能看到预设的告白或纪念日特效。 这个项目展示了HTML作为动态和互动内容载体的强大能力,也提醒我们,尽管技术在进步,但有时复古的方式(如使用IE浏览器)仍能唤起怀旧之情。在准备类似的个性化礼物时,掌握基本的HTML和网页制作技巧非常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值