POJ1094-Sorting It All Out

本文介绍了一种使用拓扑排序解决排序关系确定问题的方法。通过构建有向图,并逐条加入关系边,每加入一条边就进行一次拓扑排序来判断是否能够确定排序关系或者发现矛盾。

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

题目链接:点击打开链接

 

Sorting It All Out

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 38356 Accepted: 13525

Description

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

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.

Output

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.

Sample Input

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

Sample Output

Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.

 

题目大意:给出以下关系,①处理到第几个时能确定所有的关系,②处理到第几个时能发现矛盾,③没有矛盾所有点都能确定

 

思路:先是想到并查集,但是谁比谁大不好比较。后来构建有向图,每读入一组关系,就拓扑排序一次。

 

AC代码:

#include<iostream>
#include<cstdio>
#include<set>
#include<queue>
#include<string.h>
using namespace std;

int n, m, cnt;

int indegree[1010], temp[1010];

vector<int> G[1010];//邻接表
queue<int> q;
char ans[31];

bool check(int x, int y) {
	for(int i = 0; i < G[x].size(); i++) {
		if(G[x][i] == y)
			return true;
	}
	return false;
}

int topo() {
	for(int i = 0; i < n; i++) {//这里是跑所有的点
		if(!indegree[i])
			q.push(i);
	}
	bool unsure = false;//不确定的情况
	cnt = 0;
	while(!q.empty()) {//因为是跑所有的点,一旦出现有的点没有涉及,入读为0的点必然大于1
		if(q.size() > 1)
			unsure = true;
		int u = q.front(); //取队列首个顶点u
		ans[cnt++] = u + 'A';
		q.pop();
		for(int i = 0; i < G[u].size(); i++) {
			int v = G[u][i];
			indegree[v]--; //入度减一 
			if(indegree[v] == 0) {//入度为0,入队 
				q.push(v);
			}
		} 
	}
	if(cnt < n)//有环,矛盾
		return 2;
	if(unsure)
		return 3;
	return 1;
}

char s[3];
int main() {
	int step, flag;
	while(~scanf("%d %d", &n, &m)) {
		memset(G, 0, sizeof(G));
		while(!q.empty()) q.pop();
		if(n == 0 && m == 0)
			break;
		bool ok = false;
		memset(indegree, 0, sizeof(indegree));
		for(int i = 0; i < m; i++) {
			scanf("%s", s);
			if(ok)//就算知道了结果,也要读入所有数据
				continue;
			int l = s[0]-'A';
			int r = s[2]-'A';
			if(!check(l, r)) {//有过了,就不用再加入了
				G[l].push_back(r);
				indegree[r]++;
			}
			memcpy(temp, indegree, sizeof(indegree));//把当前入度情况记录下来
			flag = topo();//拓扑判断一下
			memcpy(indegree, temp, sizeof(temp));//再把入读情况放进入,进行下一条的时候,上个入读情况还是保留的
			if(flag != 3) {
				step = i+1;//记录当前是第几条关系
				ok = true;
			}
		}//三种情况
		if(flag == 2) {
			printf("Inconsistency found after %d relations.\n", step);
		} else if(flag == 3){
			printf("Sorted sequence cannot be determined.\n");
		} else {
			ans[cnt] = '\0';
			printf("Sorted sequence determined after %d relations: %s.\n", step, ans);
		} 
	}	
}

 

转载于:https://www.cnblogs.com/ACMerszl/p/9572969.html

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值