【作业】Kitchen Plates(拓扑排序)
题目链接: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(拓扑排序)的更多相关文章
- Codeforces Gym-102219 2019 ICPC Malaysia National J. Kitchen Plates (暴力,拓扑排序)
题意:给你5个\(A,B,C,D,E\)大小关系式,升序输出它们,如果所给的大小矛盾,输出\(impossible\). 题意:当时第一眼想到的就是连边然后排序,很明显是拓扑排序(然而我不会qwq,之 ...
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- 有向无环图的应用—AOV网 和 拓扑排序
有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
- 图——拓扑排序(uva10305)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Java排序算法——拓扑排序
package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- 拓扑排序 - 并查集 - Rank of Tetris
Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...
随机推荐
- AtCoder Grand Contest 009 题解
传送门 为啥这场题目少一点啊-- \(A\) 易知增加的数前面肯定比后面大,那么我们倒着做,然后维护一下最小要加多少就可以了 typedef long long ll; const int N=1e5 ...
- 《挑战30天C++入门极限》C++的iostream标准库介绍(3)
C++的iostream标准库介绍(3) C语言提供了格式化输入输出的方法,C++也同样,但是C++的控制符使用起来更为简单方便,在c++下有两中方法控制格式化输入输出. 1.有流对象的成员函 ...
- Java springMVC前端传入字符串、后台接收Date的错误解决
今天在一个基于SSM的项目里出现以下报错 Cannot convert value of type [java.lang.String] to required type [java.util.Dat ...
- Image.FromFile 之后无法删除这个文件
Image.FromFile 之后无法删除这个文件 pictrue图片是从文件加载的,现在想换张图片,更改之前要删除原有的文件,在删除原有的文件出现了异常 string path = @" ...
- Python之pygame学习绘制文字制作滚动文字
pygame绘制文字 ✕ 今天来学习绘制文本内容,毕竟游戏中还是需要文字对玩家提示一些有用的信息. 字体常用的不是很多,在pygame中大多用于提示文字,或者记录分数等事件. 字体绘制基本分为以下几个 ...
- mysql排序字段为空的排在最后面
排序字段为orderid; 1.使用order by orderid desc实现降序时,orderid 为null数据的会排在数据的最后面: 但是,order by orderid升序时,order ...
- 在Eclipse IDE进行Struts开发时提示错误:java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.FilterDispatcher的解决办法
If you have... included all necessary jars Configured build path correctly added them all in deploym ...
- Bayesian Statistics for Genetics | 贝叶斯与遗传学
Common sense reduced to computation - Pierre-Simon, marquis de Laplace (1749–1827) Inventor of Bayes ...
- 肠道微生物研究进展 | Microbiology | Human Gut Microbiome | human gut microbiota
之前我有过一篇16s基本概念和数据分析的文章.16S 基础知识.分析工具和分析流程详解 可以分成两部分,生物层面和技术层面. 生物层面: 1. 肠道微生物里面包含了哪些微生物?显然包含了所有层面的微生 ...
- layui表格工具条,如何动态控制按钮的展示?
<script type="text/html" id="toolTpl"> {{# if(d.agrgrtsts == 'A'){ }} < ...