Gym - 102219J : Kitchen Plates(拓扑排序)

http://codeforces.com/gym/102219/problem/J

You are given 5 different sizes of kitchen plates. Each plate is marked with a letter A, B, C, D, or E. You are given 5 statements comparing two different plates, you need to rearrange the plates from smallest size to biggest size. For example: the sizes of these plates.

Input

The input consist of 5 lines. In each line there will be 3 characters, the first and last character will be either A, B, C, D, or E and the middle character will be either > or <

describing the comparison between two plates sizes. No two plates will be equal.

Output

The output consist of 5

characters, the sorted order of balls from smallest to biggest plate. Otherwise, if the statements are contradicting print impossible. If there are multiple answers, print any of them.

Input

D>B
A>D
E<C
A>B
B>C

Output

ECBDA

Input

B>E
A>B
E>A
C<B
D<B

Output

impossible

 题意分析:

给出5个A, B, C, D, E两者之间的关系,求五者的从小到大排序。

解题思路:

拓扑排序

#include <stdio.h>
#include <string.h>
#define N 10
char str[10], ans[N];
int indegree[N], e[N][N];
bool book[N];
int main()
{
	while(scanf("%s", str)!=EOF){
		memset(e, 0, sizeof(e));
		memset(indegree, 0, sizeof(indegree));
		memset(book, false, sizeof(book));
		
		if(str[1]=='<'){
			indegree[str[0]-'A']++;
			e[str[2]-'A'][str[0]-'A']++;
		}else{
			indegree[str[2]-'A']++;
			e[str[0]-'A'][str[2]-'A']++;
		}
		for(int i=1; i<=4; i++)
		{
			scanf("%s", str);
			if(str[1]=='<'){
			indegree[str[0]-'A']++;
			e[str[2]-'A'][str[0]-'A']++;
			}else{
				indegree[str[2]-'A']++;
				e[str[0]-'A'][str[2]-'A']++;
			}
		}
		int len=0;
		//temp=1;
		while(1)
		{
			int temp=0;
			for(int i=0; i<5; i++)	
				if(indegree[i]==0 && book[i]==false)
				{
					temp=1;
					book[i]=true;
					ans[len++]=i+'A';
					for(int j=0; j<5; j++)
					{
						if(e[i][j])
						{
							e[i][j]--;
							indegree[j]--;
						}
					}
					break;
				}
			if(temp==0)
				break;
		}
		if(len==5)
		{
			for(int i=len-1; i>=0; i--)
				printf("%c", ans[i]);
			printf("\n");
		}
		else printf("impossible\n");
				
	} 
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值