POJ 1094 Sorting It All Out(经典拓扑+邻接矩阵)
//判环:当入度为0的顶点==0时,则有环(inconsistency)
//判序:当入度为0的顶点仅为1时,则能得到有序的拓扑排序,否则无序
//边输入边判断,用continue来做到:得出结果后,对后续的输入不作处理
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std; const int MAXN=30;
int g[MAXN][MAXN];
int degree[MAXN],L[MAXN],n,m;
char ch[5]; int toposort()
{
int tot=0,point,in,flag=1;//flag==1:有序,flag==-1:无序(即不确定)
int t[MAXN]; //由于是边输入边判断,所以用t数列来保存当前(0到i)的顶点入度情况,以此避免修改degree数组
for(int i=1;i<=n;i++)
t[i]=degree[i];
for(int i=1;i<=n;i++)
{
in=0;
for(int j=1;j<=n;j++)
if(!t[j])
{
in++;
point=j;
}
if(in==0) return 0;
if(in>1) flag=-1;
L[tot++]=point; //入度为0的点入队
t[point]=-1; //删点
for(int j=1;j<=n;j++)
if(g[point][j]==1)
t[j]--; //删边
}
return flag;
} void init_input_judge_output()
{
int sign=0;
memset(degree,0,sizeof(degree));
memset(L,0,sizeof(L));
memset(g,0,sizeof(g));
for(int i=1;i<=m;i++)
{
scanf("%s",ch);
if(sign) continue;
int a=ch[0]-'A'+1;
int b=ch[2]-'A'+1;
g[a][b]=1;
degree[b]++;
int judge=toposort();
if(judge==0)
{
printf("Inconsistency found after %d relations.\n",i);
sign=1;
}
if(judge==1)
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=0;j<n;j++)
printf("%c",L[j]+'A'-1);
printf(".\n");
sign=1;
}
}
if(!sign)
printf("Sorted sequence cannot be determined.\n");
} int main()
{
while(scanf("%d%d",&n,&m)&&n+m)
{
init_input_judge_output();
}
return 0;
}
POJ 1094 Sorting It All Out(经典拓扑+邻接矩阵)的更多相关文章
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
- POJ 1094 Sorting It All Out【拓扑排序】
题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26801 Accepted: 92 ...
- POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)
题意:给出n个字符,m对关系,让你输出三种情况: 1.若到第k行时,能判断出唯一的拓扑序列,则输出: Sorted sequence determined after k re ...
- POJ 1094 Sorting It All Out 【拓扑排序】
<题目链接> 题目大意: 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用上之后依旧无法确定唯一的排序. 解题分析 ...
- POJ - 1094 Sorting It All Out(拓扑排序)
https://vjudge.net/problem/POJ-1094 题意 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用 ...
- POJ 1094 Sorting It All Out【拓扑排序 / 比较字母大小】
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38100 Accepted: 13453 ...
- 题解报告:poj 1094 Sorting It All Out(拓扑排序)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
随机推荐
- 干货云集 WOT 2017全球架构与运维技术峰会揭密技术难点
WOT,World Of Tech专注互联网IT技术领域,是一场不容错过的技术盛会!WOT 2017全球架构与运维技术峰会三大章节,15大技术专场,60+国内外一线互联网精英大咖站台,打造兼顾技术视野 ...
- IOS网络请求中文转码
-(void)get { NSString *urlStr = @"http://120.25.226.186:32812/login2?username=小码哥&pwd=520it ...
- Object转换为字符并去空格
<div id="txt" style="display:none">1."不积跬步,无以至千里"的古语说明( A ) A.没有 ...
- C# 无边框窗口实现拖动
原文地址:http://blog.csdn.net/sky___ice/article/details/11533321 Form1.Designer.cs: // //Form1 // this.M ...
- java基础<迷你DVD系统>
一.写在main方法中 import java.util.*; public class MyDVDMgr { public static void main(String[] args){ Scan ...
- FZU 2088 最长队名
Problem 2088 最长队名 Problem Description Jack所在的班级决定组团报名参加FZU校赛.为了体现班级的团结和睦,班长决定用班级所有人的名字连起来组成一个史上最长最醒 ...
- ios_swift开发资源整理
目录 1.苹果官方资源 2.国内外视频网站推荐 3.中文文档 4.demo网站 5.开发工具推荐 6.国内外开发网站论坛 7.技术博客推荐 8.书籍推荐 9.第三方框架推荐 10.第三方发布平台 11 ...
- hdu1029
#include<iostream>#include<string.h>using namespace std;int main(){ int n,i; int t; int ...
- scala实现快速排序
scala> def qSort(a: List[Int]): List[Int] = { | ) a | else qSort( a.filter(a.head > _ )) ++ | ...
- 【转】CSS
css概念 http://www.cnblogs.com/moveofgod/archive/2012/09/18/2691101.html css八大功能 http://developer.51ct ...