题目链接:https://vjudge.net/contest/345791#problem/O

【问题描述】

  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.

输入:

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.

输出:

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

样例输入;

D>B
A>D
E<C
A>B
B>C
B>E
A>B
E>A
C<B
D<B 样例输出:
ECBDA
impossible

试题分析:
  给出5个大小关系,求其中一种满足从小到大的排列方式。因为输入并没有保证一定可以确定每两个Plate之间的大小关系,无法准确确定大小关系。题意只需要输出一种满足升序的排列即可,即用拓扑排序。
代码如下:
 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<string>
#include<map>
#define mem(a, b) memset(a, b, sizeof(a))
const int MAXN = ;
using namespace std; char s[MAXN];
int in[MAXN], out[MAXN];
int head[MAXN], cnt;
map<char, int> mp;
map<int, char> mpp;
string ans; struct Edge
{
int to, next;
}edge[MAXN]; void add(int a, int b)
{
cnt ++;
edge[cnt].to = b;
edge[cnt].next = head[a];
head[a] = cnt;
} void topo_sort()
{
queue<int> Q;
for(int i = ; i <= ; i ++)
if(!in[i])
Q.push(i);
while(!Q.empty())
{
int a = Q.front();
Q.pop();
ans += mpp[a];
for(int i = head[a]; i != -; i = edge[i].next)
{
int to = edge[i].to;
in[to] --;
if(!in[to])
{
Q.push(to);
}
}
}
if(ans.length() != )
printf("impossible\n");
else
cout << ans << endl;
} int main()
{
mp['A'] = , mp['B'] = , mp['C'] = , mp['D'] = , mp['E'] = ;
mpp[] = 'A', mpp[] = 'B', mpp[] = 'C', mpp[] = 'D', mpp[] = 'E'; mem(head, -), cnt = ;
ans = "";
for(int i = ; i <= ; i ++)
{
scanf("%s", s);
int x = mp[s[]], y = mp[s[]];
if(s[] == '<') //A < B则连边 表示A比B小
{
add(x, y);
out[x] ++;
in[y] ++;
}
else
{
add(y, x);
out[y] ++;
in[x] ++;
}
}
topo_sort();
return ;
}

【作业】Kitchen Plates(拓扑排序)的更多相关文章

  1. Codeforces Gym-102219 2019 ICPC Malaysia National J. Kitchen Plates (暴力,拓扑排序)

    题意:给你5个\(A,B,C,D,E\)大小关系式,升序输出它们,如果所给的大小矛盾,输出\(impossible\). 题意:当时第一眼想到的就是连边然后排序,很明显是拓扑排序(然而我不会qwq,之 ...

  2. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  3. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  4. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  5. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  6. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  7. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  8. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

  9. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

随机推荐

  1. hive(2)数据类型和文件格式

    基本的数据类型 Hive支持关系型数据中大多数基本的数据类型,同时也支持关系型数据库中很少出现的三种集合数据类型. 集合数据类型 Hive中的列支持使用struct.map.array集合数据类型,下 ...

  2. python技巧获取26个英语字母

    import string string.ascii_uppercase # 获取26个大写字母 string.ascii_lowercase # 获取26个小写字母 string.ascii_let ...

  3. [C++] wchar_t关键字使用方法

    char 是单字符类型,长度为一个字节 wchar_t 是宽字符类型,长度为两个字节,主要用在国际 Unicode 编码中 举例: #include<iostream> using nam ...

  4. css中的浮动与定位

    传送门:https://www.cnblogs.com/junwuyao/p/7435257.html

  5. appium 多线程还是多进程(转)

    https://www.cnblogs.com/zouzou-busy/p/11440175.html 在前面我们都是使用一个机器进行测试,在做app自动化的时候,我们要测不同的机型,也就是兼容性测试 ...

  6. ch341a编程器写操作超时失败

    当点击自动编写‘提示写操作超时失败’要怎么样才能解决,下面我给大家分享一下!   方法/步骤     首先我们点击操作   选择操作选项   看看箭头所指的几个地方是不是都没打上勾   我们把这几个地 ...

  7. Shell脚本——make命令和Makefile文件【转】

    https://blog.csdn.net/twc829/article/details/72729799 make命令是一个常用的编译命令,尤其在C/C++开发中,make命令通过makefile文 ...

  8. 树莓派VNC

    sudo raspi-config Interfacing Options -> VNC 1.停止VNC窗口: vncserver -kill:1 2.修改密码 vncpasswd 3.重启服务 ...

  9. 文献阅读 | Molecular Architecture of the Mouse Nervous System

    文章亮点: 按level来管理和分析数据,文章有不同stage,每个stage有不同subtype,这应该是一个真tree,而不只是一个进化树,文章里出现最多的进化树把所有的stage都整合了. 空间 ...

  10. PathVariable传过来的中文乱码是怎么回事

    今天在写一个spring mvc程序,由于客户原来设计的原因,不能传id过来,只能传名称,结果发现收到的数据是乱码. 百度之后,找到别人的解决方案,试了下没问题,转载一下.使用的web服务器是Tomc ...