【作业】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又想了一个新点子:他将制作一个全球 ...
随机推荐
- Linux 文件系统引起的云盘文件系统异常导致 MySQL 数据页损坏事故恢复复盘
事故的起因是因为当我访问某个数据库的某个表的时候,MySQL 立即出现崩溃并且去查看 MySQL 的错误日志出现类似信息 --09T05::.232564Z [ERROR] InnoDB: Space ...
- OpenFOAM在原有算例上新建算例(只拷贝0,system,constant)
原视频下载地址: https://yunpan.cn/cMpyBHSEvC7T4 (提取码:dca4)
- python使用ThreadPoolExecutor每秒并发5个
import time from concurrent.futures import ThreadPoolExecutor from functools import partial from log ...
- ubuntu之路——day4(今天主要看了神经网络的概念)
感谢两位老师做的免费公开课: 第一个是由吴恩达老师放在网易云课堂的神经网络和深度学习,比较偏理论,使用numpy包深入浅出的介绍了向量版神经网络的处理方式,当然由于视频有点老,虽然理论很好但是工具有点 ...
- C#求任意两整数之和
2019.9.11 作业要求: 求出任意两整数之和 解决方案: using System; using System.Collections.Generic; using System.Linq; u ...
- 谷歌分析(Google Analytics) 是什么
谷歌分析(Google Analytics) 是什么 一.总结 一句话总结: 谷歌分析,即大家俗称的ga,全称google analytics,是谷歌推出的网站流量分析工具,可以说是当前业界最强大的流 ...
- posh-git
https://github.com/dahlbyk/posh-git#step-2-import-posh-git-from-your-powershell-profile $profile.All ...
- PCR | RT-PCR 的原理及应用
生物的东西必须要主动去了解,否则视野容易受到限制,尤其是分子生物学的核心技术. PCR - 聚合酶链式反应 The Complete Guide to PCR (How it Works, Prime ...
- 乌龙茶生产过程中挥发性成分吲哚的形成 | Formation of Volatile Tea Constituent Indole During the Oolong Tea Manufacturing Process
吲哚是啥?在茶叶成分中的地位?乌龙茶?香气,重要的前体,比如色氨酸Trp.IAA. Indole is a characteristic volatile constituent in oolong ...
- gitlab搭建与基本使用【转】
一.git.github.gitlab的区别Git是版本控制系统,Github是在线的基于Git的代码托管服务.GitHub是2008年由Ruby on Rails编写而成.GitHub同时提供付费账 ...