Sorting It All Out(拓扑排序)
http://poj.org/problem?id=1094
1.判断所给关系是否为合法的拓扑序列,若存在环,输出
"Inconsistency found after %d relations."但仍继续输入。
2.判断是否可以确定序列,若不可以确定,继续输入,若可以确定输出
"Sorted sequence determined after %d relations: %s."后,继续输入数据。
3.若输入完数据仍不可以确定序列,输出"Sorted sequence cannot be determined.".
#include<stdio.h>
#include<string.h>
const int N=;
int map[N][N],in[N],vis[N];
int n,m,flag,top;
char s[N];
void topo()
{
int cnt,i,j,pos;
top = ;
int d[N];//存每个点的度数
flag = ;
memset(vis,,sizeof(vis));
memcpy(d,in,n*sizeof(int));
for (i = ; i < n; i ++)
{
cnt = ;
for (j = ; j < n; j ++)
{
if (d[j]==)
{
cnt++;
pos = j;
}
}
if (cnt==)//存在环
{
flag = ;
return ;
}
if (cnt > )//不确定序列
{
flag = ;
}
--d[pos];
s[top++] = pos+'A';
for (int k = ; k < n; k ++)
{
if(map[pos][k]==)
--d[k];//更新度数
}
}
s[top++] = '\0'; }
int main()
{
while(~scanf("%d%d%*c",&n,&m))
{
if (n== && m==)
break;
flag = ;
memset(in,,sizeof(in));
memset(map,,sizeof(map));
char a,b,c;
int i;
for (i = ; i <= m; i ++)
{
scanf("%c%c%c%*c",&a,&b,&c);
if (flag)//控制输入
continue;
if (!map[a-'A'][c-'A'])
{
map[a-'A'][c-'A'] = ;
in[c-'A']++;
}
topo();
if (flag==)
{
flag = ;//以便能继续输入下面的点
printf("Inconsistency found after %d relations.\n",i);
}
else if (flag==)
{
printf("Sorted sequence determined after %d relations: %s.\n",i,s);
}
else if (flag==)//无法确定序列,继续输入
{
flag = ;//继续拓扑
}
}
if (flag==)//输入完也无法确定序列
printf("Sorted sequence cannot be determined.\n");
}
return ;
}
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 ...
- [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 ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- 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 ...
- PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...
随机推荐
- 顺序表查找及其优化(Java)
顺序表查找(线性查找): private static void Ordersearch(int[] arr,int num) { for (int i = 0; i < arr.length; ...
- Oreacle 语句
SELECT * FROM = and CREATETIME >to_date('2019-01-01' , 'yyyy-mm-dd hh24:mi:ss') and CREATETIME &l ...
- Python 之pygame飞机游戏
import pygame from pygame.locals import * import time import random # 我机 class HeroPlane(object): de ...
- js基本类型的包装对象
var test = "test"; test.a = "hello"; console.log(test.a); 在JavaScript中,“一切皆对象”,数 ...
- Fedora 和 RedHat 以及 SUSE 中 YUM 工具的使用
参考博客:https://www.cnblogs.com/good-study/p/9928587.html 一.yum命令概述: 1.简介: yum命令时在Fedora和RedHat以及SUSE中基 ...
- 远程连接阿里云服务器ping不通ip解决方案
搭建了阿里云服务器,发现本地ping不通,查看半天才发现,原来是在阿里云上的安全组少了些东西. 在出入方向上新建一个安全组,就可以搞定了.
- Oracle,sqlserver,mySQl的区别和联系:
1.日期处理方式 2.对保留字和关键字的处理方式: Oracle,sqlserver,mySQl的保留字不可以用作列字段,关键字可以,但他们对关键字的处理方式又不同: Oracle:关键字作为列时:用 ...
- chrony配置介绍
rhel7 文档https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Adminis ...
- Linux之iptables(一、防火墙的概念)
防火墙的概念 一.安全技术 入侵检测与管理系统(Intrusion Detection Systems):特点是不阻断任何网络访问,量化.定位来自内外网络的威胁情况,主要以提供报告和事后监督为主,提供 ...
- Cent os常见操作命令
1.查看防火墙状态:firewall-cmd –-state 2.关闭防火墙:systemctl stop firewalld.service 3.禁止防火墙开机启动:systemctl disabl ...