NYOJ 349 Sorting It All Out (拓扑排序 )
描述
An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.
- 输入
Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input. - 输出
For each problem instance, output consists of one line. This line should be one of the following three: Sorted sequence determined after xxx relations: yyy...y. Sorted sequence cannot be determined. Inconsistency found after xxx relations. where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence. - 样例输入
4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0 - 样例输出
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
分析:
首先补充一下拓扑排序的思想:
(1)从有向图中选择一个没有前驱(入度为0)的顶点并输出它。
(2)从图中删除该节点,并且删去从该节点出发的全部有向边。
(3)重复上述操作,知道图中不在存在没有前驱的顶点为止。
这样操作的结果有两种:一种是图中全部定点被输出,这说明图中不存在有向回路;另一种是图中顶点未被全部输出,剩余的顶点均有前驱节点,这说明图中存在有向回路。
这就是一个典型的拓扑排序的应用,如果能够排好序的话,说明就是可以的,如果形成环的话,说明能够构成回路也就是存在冲突,否则就是不能够排好序。
代码:
#include<stdio.h>
#include<iostream>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
int du[30];
char ch[30];
int n,m,k;
vector<int> v[30];
int init()
{
memset(du,0,sizeof(du));
memset(v,0,sizeof(v));
}
int topSort()
{
int op=1;
k=0;
queue<int >q;
int du1[30];
for(int i=0; i<n; i++)///要把入度的函数复制一下,不然会影响下次的排序
{
du1[i]=du[i];
if(du1[i]==0)
q.push(i);
}
while(!q.empty())
{
//cout<<" ----"<<q.size()<<endl;
if(q.size()>1) op=0;///相当于有多个入度为0的点,也就是还没有排好序
int a=q.front();
q.pop();
char c=a+'A';
ch[k++]=c;
// cout<<"k="<<k<<endl;
for(int i=0; i<v[a].size(); i++)
{
int b=v[a][i];
du1[b]--;
if(du1[b]==0)
q.push(b);
}
}
if(k<n)///形成环,如果没有形成环且能排好序的话,肯定每一个点都要入队一次
return -1;
return op;///op==1,已经排好序;op=1,还没有排好
}
int main()
{
char ch2,ch1;
int a,b;
while(~scanf("%d%d",&n,&m),n,m)
{
init();
int flag=0;
for(int mm=1; mm<=m; mm++)
{
scanf(" %c<%c",&ch1,&ch2);
if(flag!=0) continue;///已经确定当前的序列是有序或者已经发生冲突
a=ch1-'A';
b=ch2-'A';
// cout<<a<<" "<<b<<endl;
v[a].push_back(b);///单向
du[b]++;
flag=topSort();///每次加入一个都要进行一次判断,看能否满足某个条件
if(flag==1)///已经排好序
{
printf("Sorted sequence determined after %d relations: ",mm);
for(int k1=0; k1<k; k1++)
{
printf("%c",ch[k1]);
}
printf(".\n");
}
if(flag==-1)///发生冲突
{
printf("Inconsistency found after %d relations.\n",mm);
}
}
if(flag==0)///到最后还没有排好序
{
printf("Sorted sequence cannot be determined.\n");
}
}
return 0;
}
NYOJ 349 Sorting It All Out (拓扑排序 )的更多相关文章
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- poj 1094 Sorting It All Out_拓扑排序
题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...
随机推荐
- 30分钟 带你浅入requirejs源码
因为最近项目想现实一个单页功能,用的是react ,然后看了一下react route,挖槽 gzip后16k? 然后我简单写了一个纯单页(不支持多页的单页,所有入口都经过rewrite跑到index ...
- MyEclipse主题设置
1. 打开网页: http://eclipsecolorthemes.org/ 选择自己喜欢的主题,并下载(下载epf文件) 我下载的是 Vibrant Ink 2. 下载完成后,打开myeclips ...
- tarball
环境:Linux系统 命令:tar 关键:tar打包出来的文件有没有进行压缩所得到的文件称谓不同 仅是打包,得到的文件我们称为tarfile 包含压缩,得到的文件我们称为tarball
- Week3 Teamework from Z.XML-团队分工及贡献分分配办法
引言:团队项目即将开展,本文将就团队分工,以及分数分配办法进行阐述 一.团队分工 本周我们团队进行了初步的分工,结果如下: PM: 李孟 Dev:毛宇 薛亚杰 肖俊鹏 罗凡 Test:周敏轩 马辰 李 ...
- 转---详细的Android开发环境搭建教程
五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程 引言 在windows安装Android的开发环境不简单也说不上算复杂,本文写给第一次想在自己Windows上建立A ...
- Zebra - zebra command to get printer status
/// <summary> /// determine whether the network printer is in pause. /// </summary> /// ...
- 【bzoj3033】太鼓达人 DFS欧拉图
题目描述 给出一个整数K,求一个最大的M,使得存在一个每个位置都是0或1的圈,圈上所有连续K位构成的二进制数两两不同.输出最大的M以及这种情况下字典序最小的方案. 输入 一个整数K. 输出 一个整数M ...
- android开发中常犯的几个错误整理
新手程序猿,在开发中难免会犯各种各样的错误,以下是整理的一些android开发中常见的错误,一起来看看吧. 1.避免将多个类放在一个文件夹里面,除非是一次性使用的内部类. 就是一个文件,最好给分它同名 ...
- JS判断页面是否加载完成
用 document.readyState == "complete" 判断页面是否加载完成 传回XML 文件资料的目前状况. 基本语法intState = xmlDocument ...
- Codeforces数据结构(水题)小结
最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...