拓扑排序 +Floyd(poj 1094)
题意:字母表前n个字母,有m组他们中的大小关系,判断n个字母是否构成唯一序列:
1.Sorted sequence determined after xxx relations: yyy...y.
2.Sorted sequence cannot be determined.
3.Inconsistency found after xxx relations.
思路:判断环好判断,但是判断是否唯一就搞不懂了,后来看了下别人的,
用的是Floyd传递闭包:如果能构成唯一序列,则定点1~n,必定分布着0~n-1的入度,
才能保证0个入度的定点排在第一位,1个入度的地点排在第2位,以此类推。
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set> #define c_false ios_base::sync_with_stdio(false); cin.tie(0)
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define zero_(x,y) memset(x , y , sizeof(x))
#define zero(x) memset(x , 0 , sizeof(x))
#define MAX(x) memset(x , 0x3f ,sizeof(x))
#define swa(x,y) {LL s;s=x;x=y;y=s;}
using namespace std ;
#define N 100 const double PI = acos(-1.0);
typedef long long LL ; int topo[N],t;
int G[N][N];
int n,m,flag,pos,i,num;
char s[N];
char output[N]; int Floyd(){
int i, j, k;
for(i = ; i<n ;i++)
for(k = ; k<n; k++)
for(j = ; j<n; j++)
if(G[k][i] && G[i][j])
G[k][j] = ;
for(i = ; i< n; i++){
if(G[i][i] == )
return ;
}
return ;
} int Topsort(){
int cou, ind, k ,i;
int degree[N],used[N];
num = ;
zero(used);
zero(degree);
for(i = ; i< n ;i++)
for(k = ; k< n ;k++)
if(G[i][k])
degree[k]++;
for(i = ; i< n; i++){
cou = ;
for(k = ; k< n ; k++)
if(degree[k] == && !used[k]){
output[num++] = k + 'A';
used[k] = ;
cou++;
ind = k;
}
if(cou > ) return ;
if(cou == ) return ;
for(k = ; k < n;k++)
if(G[ind][k]) degree[k]--;
}
output[num] = '\0';
return ;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(cin>>n>>m){
if(n == && m == ) break;
flag = ;
zero(G);
for(i = ;i < m; i++){
scanf("%s",s);
G[s[]-'A'][s[]-'A'] = ;
if(flag != ) continue;
flag = Floyd();
if(flag == )
pos = i;
if(flag == )
flag = Topsort();
else continue;
if(flag == )
pos = i;
}
pos++;
if(flag == ){
printf("Sorted sequence determined after %d relations: %s.\n",pos,output);
continue;
}
if(flag == ){
printf("Inconsistency found after %d relations.\n",pos);
continue;
}
printf("Sorted sequence cannot be determined.\n");
}
return ;
}
拓扑排序 +Floyd(poj 1094)的更多相关文章
- 【改了一天的拓扑排序】POJ 1094——Sorting It All Out
来源:点击打开链接 不知道怎么回事,wa了整整一天..在绝望的时候AC了. 重点是分步处理和三种情况的判断. 1.判断是否成环,成环了直接输出错误信息. 2.然后一条边一条边的加入,进行拓扑排序,如果 ...
- 拓扑排序(poj 1094)
前置知识:拓扑排序 详细注释都在代码里 //该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. //是典型的拓扑排序,但输出格式上确有三种形式: // 1.该字母序列有序,并依 ...
- [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 ...
- Sorting It All Out (拓扑排序+floyd)
An ascending sorted sequence of distinct values is one in which some form of a less-than operator is ...
- 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 ...
- POJ 1094 (传递闭包 + 拓扑排序)
题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...
- POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)
题意:给出n个字符,m对关系,让你输出三种情况: 1.若到第k行时,能判断出唯一的拓扑序列,则输出: Sorted sequence determined after k re ...
- 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 ...
随机推荐
- 开放地址法实现HashTable
前注:本文不是讲解Java类库的Hashtable实现原理,而是根据计算机哈希表原理自己实现的一个Hashtable. HashTable内部是用数组存放一个(Key-Value pair)键值对的引 ...
- LeetCode Implement pow(x, n).
这个题目我也没有思路,同学们可以查看这个http://www.cnblogs.com/NickyYe/p/4442867.html 下面是我改进后的代码 第一种方法: class Solution { ...
- AX Dynamics 去中文字符长度:中文字符当2个字符处理
static void jw_testStrByteLen(Args _args){ str _str = "A你好"; System.Te ...
- Delphi七个版本
第一次Zack Urlocker给我看一款尚未发布的名为Delphi的产品时,我意识到她将改变我的工作—还有许多其他软件开发者的工作. 我过去为使用Windows C++ 库而挣扎,而Delphi过去 ...
- Microsoft source-code annotation language (SAL) 相关
More info see: https://msdn.microsoft.com/en-us/library/hh916383.aspx Simply stated, SAL is an inexp ...
- SQL笔记-第三章,数据的增删改
1.数据的插入 简单的INSERT语句 INSERT INTO T_Person(FName,FAge,FRemark) VALUES(‘Tom’,18,’USA’) 简化的INSERT语句(只对部分 ...
- XidianOJ 1176 ship
题目描述 The members of XDU-ACM group went camp this summer holiday. They came across a river one day. T ...
- centos6.x下手工安装二进制Docker v1.1x
Docker在 centos 6.x 下面默认最新的版本是1.7, 然而这个并不符合我的实际需求, 尤其我需要 docker-compose 来作为编配工具部署swarm, 所以只有使用二进制的安装包 ...
- 如何做一个avalon组件
在avalon1.5中改用更直观的自定义标签来声明组件,废掉ms-widget,引入更强大的生命周期管理,可以让组件任意套嵌. 组件是由JS,HTML,CSS构成 JS 以AMD形式组织,引入HTML ...
- 在VMware上安装Ubuntu14.04 , Docker
最近在公司做了一个关于 How to use Docker to deploy a java-based website 的KT,总结如下: 准备材料(Linux+64bit) 1. Tomcat安装 ...