题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序。此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号;如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路);如果到最后还是不能确定拓扑序列,就输出指定的字符串。

拓扑排序:对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若 ∈E(G),则u在线性序列中出现在v之前。

分析:首先,拓扑排序的算法还是挺直观的。简单的拓扑排序算法:先找到任意一个没有入边的顶点,然后是显示该顶点,并将它和它的边一起从图中删除。然后,对图的其余部分应用同样的方法处理。如果找不到没有入边的顶点,说明存在回路。这里的实现过程由于要边输入边检测,所以要注意先一次性读取完输入。还要注意图不是强连通的,以及消去一个顶点后的图不是强连通图的这种情况。做的时候感觉要考虑很多情况,但做完了反而想不起那么多来。呵呵,看代码吧!

import java.util.Scanner;

public class Main {
static int n,m;
static int[][] a; //邻接矩阵
static int [] degree; //每个节点的入度
static boolean[] vis; //记录是否被访问
/**
* 拓扑排序,返回排序的序列,返回1说明存在环路。
* @return
*/
public static String topo(){
String s="";
for(int i=0;i<n && isIsolatedNode(i);i++){
int temp=findInDegreeZero();
if(temp!=-1){
//消去顶点后的图还是强连通就满足要求
if(countZeroDegree()==1)
s+=((char)(temp+65));
vis[temp]=true;
for(int j=0;j<n;j++)
if(a[temp][j]==1)
degree[j]--;
}else{
s="1";
break;
}
}
return s;
}
/**
* 读取入度
*/
public static void readInDegree(){
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(a[i][j]!=0 )
degree[j]++;
}
/**
* 由于采用邻接矩阵,所以要判断节点是否在要排序的N个节点中。利于查找入度为0的节点。
* @param num
* @return
*/
public static boolean isIsolatedNode(int num){
int sum=0;
for(int i=0;i<n;i++)
sum+=(a[num][i]+a[i][num]);
if(sum==0)
return false;
else
return true;
}
/**
* 计算入度为零的节点个数,超过一个则说明该图不是强连通图。
* 由于要优先判断是否存在环路,所以不能如果不是强连通图也要等判断是否存在环路。
* @return
*/
public static int countZeroDegree(){
int sum=0;
for(int i=0;i<n;i++)
if(degree[i]==0 && isIsolatedNode(i) && vis[i]==false){
sum++;
}
return sum;
}
/**
* 找到入度为0的节点,不存在就返回-1(存在环路)
* @return
*/
public static int findInDegreeZero(){
for(int i=0;i<n ;i++)
if(degree[i]==0 && vis[i]==false && isIsolatedNode(i)){
return i;
}
return -1;
} public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(true){
n=sc.nextInt();
m=sc.nextInt();
if(n+m==0)
break;
a=new int[n][n];
String result="";
String[] str=new String[m+1];
for(int i=1; i<=m; i++)
str[i]=sc.next();
int i;
for(i=1; i<=m; i++){
vis=new boolean[n];
degree=new int[n];
a[str[i].charAt(0)-65][str[i].charAt(2)-65]=1;
readInDegree();
int beginZeroDegree=countZeroDegree();
result=topo();
//如果结果字符串长度和N相同,并且图是强连通的就输出序列
if(result.length()==n && beginZeroDegree==1){
System.out.println("Sorted sequence determined after "+i+" relations: "+result+".");
break;
//如果存在环路则输出
}else if(result.equals("1")){
System.out.println("Inconsistency found after "+i+" relations.");
break;
}
}
//不存在环路,到最后还是不能确定序列
if((i-1)==m && result.length()!=n)
System.out.println("Sorted sequence cannot be determined.");
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

  1. 拓扑排序 POJ 1049 Sorting It All Out

    题目传送门 /* 拓扑排序裸题:有三种情况: 1. 输入时发现与之前的矛盾,Inconsistency 2. 拓扑排序后,没有n个点(先判断cnt,即使一些点没有边连通,也应该是n,此时错误是有环): ...

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

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

  3. 拓扑排序(Topological Sorting)

    一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...

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

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

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

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

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

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

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

  8. POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)

    题意:给出n个字符,m对关系,让你输出三种情况:     1.若到第k行时,能判断出唯一的拓扑序列,则输出:         Sorted sequence determined after k re ...

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

随机推荐

  1. WCF基础之消息协定

    通常定义消息的架构,使用数据协定就够了,但是有时必须将类型精确映射到soap消息,方法两种:1.插入自定义soap标头:2.另一种是定义消息的头和正文的安全属性.消息协定通过MessageContra ...

  2. 2.PyCharm安装和使用之HelloWorld

    百度搜一个就好了, 然后下一步下一步 重点来了,穷!   这软件需要购买不然就只能用30天, 作为屌丝的我,在网上不然的搜索: 然后终于搞定了! 屌丝的春天:http://idea.lanyus.co ...

  3. Future Promise 模式(netty源码9)

    netty源码死磕9  Future Promise 模式详解 1. Future/Promise 模式 1.1. ChannelFuture的由来 由于Netty中的Handler 处理都是异步IO ...

  4. maven3 org.codehaus.plexus.classworlds.launcher.launcher 找不到或无法加载主类

    maven3 org.codehaus.plexus.classworlds.launcher.launcher 找不到或无法加载主类 嗯,网上很多资料说是路径的问题,确实是有可能是路径的问题,而且还 ...

  5. MySQL合并多行

    select id,group_concat(re_id order by re_id separator ",") as re_idfrom tablenamegroup by ...

  6. ggplot2绘图系统

    ggplot2包实现了一个在R中基于全面一致的语法创建图形时的系统 .在ggplot2中,图是采用串联起来(+)号函数创建的.详细内容参见<ggplot2:数据分析与图形艺术>,这里只简要 ...

  7. Linux vim 中文显示乱码解决方法

    因为在windows下默认是gb编码,而我的vim默认是utf-8(gedit默认也是utf-8),所以打开会成乱码.改动了一下配置文件,使vi支持gb编码就好了.$vi ~/.vimrclet &a ...

  8. linux基础part5

    linux 基础 一.网络基础 1.ifup 网卡名称:ifdown 网卡名称:ifconfig 网卡名称 ip 子网 其修改只是临时生效,一旦关机或重启命令失效,需要修改配置文件永久生效. 2.网卡 ...

  9. js琐碎知识点

    1.javascript发展史 javascript首先由Netscape设计,为改善浏览器用户体验,名为liveScript, 网景公司被sun公司收购,为了宣传改名为javascript 后来su ...

  10. css目录

    想看css文章目录? 点击我