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.该序列字母次序之间 ...
随机推荐
- Eclipse安装egit Github教程
网址:http://download.eclipse.org/egit/updates 教程: http://jingyan.baidu.com/article/4853e1e529483c1909f ...
- CDR服装设计-用CorelDRAW排钻如何把圈摆均匀
服装设计一直都是一个很火热的行业,也是一个比较高端的行业,随着时代的步伐,以前的人都是用手绘的方式来设计服装,现在不一样了,电脑可以说普及到了每一个家庭,让软件以更快的速度,更准确的数据来设计服装中的 ...
- react 父组件给子组件传值
父组件 import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child fr ...
- 1. Python中的基本数据类型、运算、变量
本文利用的是Python 3.x版本,建议学习3.x版本 Python中的基本数据类型.运算.变量 1. 基本数据类型 1.1 整数 py可以处理任意大小的整数,例如123,1234567891324 ...
- BZOJ 1232 USACO 2008 Nov. 安慰奶牛Cheer
[题解] 对于每一条边,我们通过它需要花费的代价是边权的两倍加上这条边两个端点的点权. 我们把每条边的边权设为上述的值,然后跑一边最小生成树,再把答案加上最小的点权就好了. #include<c ...
- PAT 1103 Integer Factorization
The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...
- 【[Offer收割]编程练习赛12 C】矩形分割
[题目链接]:http://hihocoder.com/problemset/problem/1495 [题意] [题解] 把每个方块都再分成3*3的小块; 这样; 对于一个方块来说 如果是'\' 则 ...
- BUPT2017 springtraining(16) #2 ——基础数据结构
题目在这里 A.似乎是个并查集+??? B.10W的范围,似乎可以暴力来一发二分+sort? 但我猜正解可以O(nlogn)? C.单调队列入门题目 #include <cstdio> ] ...
- UVALIVE 6958 Indoorienteering
题目大意:有不超过14个点组成的完全图,给出邻接矩阵,问是否存在长度为W的欧拉回路? 数据范围:n<=14, w<=1e15: standard input/output 7 s, 256 ...
- noip模拟赛 钻石
分析:用裸暴力可以得60分,每次dfs,看第i个盒子到底有没有钻石就行了.其实这很像0/1背包问题,只是多了一个m的限制.这要怎么办呢?因为概率是可以加减的,所以可以先不考虑m的限制,求出概率,然后d ...