拓扑排序入门

实际上挺早之前便了解了拓扑排序,但一直不是很清楚他有什么用的。最近做了一道拓扑排序的题目,大概对这类问题有了一些了解吧。拓扑排序适用于一种特殊的图,这种图有以下特征。
1:该图为有向图。
2:该图有严格的顺序,不存在后面的被指向的节点去指向前面的节点。
即不存在a->b->c->a这一条。
拓扑排序有两个作用,检查是否是拓扑图和输出拓扑序。
话不多少,例题来见
CodeForces - 510C
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: “Fox”). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.

After checking some examples, she found out that sometimes it wasn’t true. On some papers authors’ names weren’t sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!

She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.

Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s i ≠ t i. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s i and t i according to their order in alphabet.

Input
The first line contains an integer n (1 ≤ n ≤ 100): number of names.

Each of the following n lines contain one string name i (1 ≤ |name i| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.

Output
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters ‘a’–‘z’ (i. e. first output the first letter of the modified alphabet, then the second, and so on).

Otherwise output a single word “Impossible” (without quotes).

Examples
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz

这题的意思就是让我们重新排列字典序,然后判断是否解是合理的。什么是合理的呢,就是比如你a->b->c,后面不存在c->a的这种情况。代码有详解。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
queue<int> q;
char ap[105][105];
int p[30][30];
queue<int> aov;
int vis[30];
int main()
{
	memset(ap,0,sizeof(ap));
	memset(p,0,sizeof(p));
	memset(vis,0,sizeof(vis));
	/*
		ap是读进去的字符串。
		p代表的是字母之间的关系,即图的关系。
		vis代表每个字母入度 
	*/
	int n;
	int pand=0;
	//有一种特殊情况要特判,就是如aeo在ae的前面的情况,就是前面字符串是比后面字符串长,而且两字符串共有部分一样。 
	scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%s",ap[i]);
	for(int i=0;i<n-1;i++)    //我们只要相邻的比就可以了,不用每个与后面所有的比,层次关系很明显的。 
	{
		for(int j=0;;j++)     //中间会有结束条件 
		{
			if(ap[i][j]!=ap[i+1][j])        //同位字母不同的情况 
			{
				if(ap[i+1][j]=='\0')pand=1;   //特判 
				else if(ap[i][j]=='\0');
				else if(p[ap[i][j]-'a'][ap[i+1][j]-'a']==0){ 
				 //a字母对应0,b字母对应1...防止产生a指向b多次被重复统计,因为下面拓扑排序时每次入度仅去1. 
					p[ap[i][j]-'a'][ap[i+1][j]-'a']=1;
					vis[ap[i+1][j]-'a']++;
				}
				break;
			}
		}
	}
	for(int i=0;i<26;i++)
	{
		if(vis[i]==0)
		{
			//拓扑排序第一步,入度为0进容器。 
			q.push(i);
		}
	}
	while(!q.empty())
	{
		int each=q.front();
		q.pop();
		aov.push(each);
		//拓扑排序第二步,取容器里的数放入拓扑序中,并消去其存在感。 
		for(int i=0;i<26;i++)
		{
			if(p[each][i]==1)
			{
				if(--vis[i]==0)q.push(i);
			}
		}
	}
	//判断是否是拓扑图以及特判 
	if(aov.size()!=26||pand==1)printf("Impossible");
	else
	{
	//输出拓扑序 
		while(!aov.empty())
		{
			printf("%c",aov.front()+'a');
			aov.pop();
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AOV拓扑排序算法是一种用来判断有向无环图(DAG)的方法。拓扑排序是通过将AOV网络的所有顶点按照一定的顺序排列,使得所有的前驱和后继关系都能满足的过程。如果能够成功地通过拓扑排序将所有顶点都排入一个拓扑有序的序列中,那么该AOV网络必定不存在有向环;反之,如果得不到所有顶点的拓扑有序序列,则说明该AOV网络存在有向环,此AOV网络所代表的工程是不可行的。 具体的AOV拓扑排序算法如下: 1. 找到所有没有前驱的顶点,将这些顶点添加到拓扑有序序列中,并移除与它们相邻的边。 2. 重复步骤1,直到所有顶点都被添加到拓扑有序序列中,或者无法找到没有前驱的顶点为止。 3. 如果成功地将所有顶点添加到拓扑有序序列中,则该序列即为AOV网络的拓扑有序序列。如果存在有向环,则无法找到所有顶点的拓扑有序序列。 通过上述算法,我们可以判断一个AOV网络是否为有向无环图。如果成功地得到了拓扑有序序列,说明该AOV网络是有向无环图;反之,如果无法得到拓扑有序序列,则说明该AOV网络存在有向环,不满足有向无环图的条件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [图算法入门3:活动网络-AOV网络和拓扑排序](https://blog.csdn.net/fangfanglovezhou/article/details/125226214)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值