http://poj.org/problem?id=1094

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int in[27],out[27];
char index[27];
bool vis[27];
int mem[27][27];
int n,m;
int tlen;
bool floyd(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
if(mem[i][k]&&mem[k][j])mem[i][j]=1;
}
}
}
for(int i=1;i<n;i++){
if(mem[i][i])return false;
}
return true;
}
bool calc(){
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(mem[i][j]==1){
out[i]++;
in[j]++;
}}
}
for(int i=0;i<n;i++){
if(in[i]+out[i]!=n-1)return false;
}
return true;
}
bool topologicalsort(){
memset(vis,0,sizeof(vis));
int len=0;
bool fl=false;
while(len<n){
fl=false;
for(int i=0;i<n;i++){
if(in[i]==0&&!vis[i]){
fl=true;
vis[i]=true;
index[len]=i+'A';
len++;
for(int j=0;j<n;j++){
if(mem[i][j])in[j]--;
}
break;
}
}
if(!fl)return false;
}
return true;
}
int main(){
char ch,ch2;
while (scanf("%d %d",&n,&m)==2&&n){
bool fl=false;
getchar();
memset(mem,0,sizeof(mem));
memset(index,0,sizeof(index));
for(int i=1;i<=m;i++){
scanf(" %c< %c",&ch,&ch2);
in[ch2-'A']++;
out[ch-'A']++;
mem[ch-'A'][ch2-'A']=1;
getchar();
if(fl)continue;
if(floyd()){
if(calc()){
if(topologicalsort()){
fl=true;
index[n]=0;
printf("Sorted sequence determined after %d relations: %s.\n",i,index);
continue;
}
}
}
else {
printf("Inconsistency found after %d relations.\n",i);
fl=true;
continue;
}
}
if(!fl)printf("Sorted sequence cannot be determined.\n");
}
return 0;
}

  

POJ 1094 Sorting It All Out 拓扑排序 难度:0的更多相关文章

  1. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  2. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  3. [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 ...

  4. 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 ...

  5. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

  6. PKU 1094 Sorting It All Out(拓扑排序)

    题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...

  7. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

  8. [ACM] POJ 1094 Sorting It All Out (拓扑排序)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26801   Accepted: 92 ...

  9. POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29984   Accepted: 10 ...

随机推荐

  1. 大数的乘法(C++)

    题目:POJ 2398 Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13410   Accepted: ...

  2. WebApi 接口测试工具:WebApiTestClient

    文章来源:http://www.cnblogs.com/landeanfen/p/5210356.html 一.WebApiTestClient介绍 1.WebApiTestClient组件作用主要有 ...

  3. Latent Dirichlet Allocation 文本分类主题模型

    文本提取特征常用的模型有:1.Bag-of-words:最原始的特征集,一个单词/分词就是一个特征.往往一个数据集就会有上万个特征:有一些简单的指标可以帮助筛选掉一些对分类没帮助的词语,例如去停词,计 ...

  4. RCNN--对象检测的又一伟大跨越

    最近在实验室和师兄师姐在做有关RCNN的研究,发现这里面坑很深呀,在网上找了一个大牛的博客,准备下来继追OPENCV同时,再来追一个RCNN的学习笔记的博文,博文地址如下:http://blog.cs ...

  5. little alchemy攻略

    一个造物游戏: acidrain=rain+smoke airlplain=metal+bird alcohol=fruit+time algae=plant+water allergy=dust+h ...

  6. Signalr简单例子

    一.需要引用的 Js: 二.编码 用的是signalr2,需要新建Startup.cs类,编码如下: using Microsoft.Owin; using Owin; using System; u ...

  7. 扩大Tomcat内存

    在Eclipse里选:Window->Preference->Installed JREs->Edit(选中jre),在Default VM Arguments里输入-Xms256m ...

  8. android界面横屏和竖屏的切换

    关于android横屏和竖屏的切换网上给了很多种.但是有些介绍的方法都是在android旧版本上. 我现在把握用到的情况写下来用于备忘: android 版本:4.0 - 4.4 要求:android ...

  9. Linux 2.6内核Makefile浅析

    1 概述 Makefile由五个部分组成: Makefile:根目录Makefile,它读取.config文件,并负责创建vmlinux(内核镜像)和modules(模块文件). .config:内核 ...

  10. [转]C#读写TEXT文件

    1.使用FileStream读写文件 文件头: using System; using System.Collections.Generic; using System.Text; using Sys ...