HDU1285(拓扑排序(模板))

拓扑排序:
思想:1、在有向图中选择一个没有前驱的顶点并且输出
2、从图中删除该节点和所有以它为尾的弧。
重复上述步骤,知道全部顶点都已经输出,或者当前图中不存在无前驱的顶点为止。后面一种情况说明有向图中存在环。

HDU1285:http://acm.hdu.edu.cn/showproblem.php?pid=1285

package D0731;

import java.util.*;

//图谱排序(邻接矩阵实现图存储)
public class HDU1285_2 {

	static int n, m;
	static int[] indegree;// 顶点的入度
	static int[] result;// 保存最后排好序的结果
	static int[][] G;// 邻接矩阵存储
	static Queue<Integer> que;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			n = sc.nextInt();
			m = sc.nextInt();
			G = new int[n + 1][n + 1];
			indegree = new int[n + 1];

			while (m-- > 0) {
				int u = sc.nextInt();
				int v = sc.nextInt();
				if (G[u][v] == 0) {// 这个条件一定要,否则WA,避免重边的情况比如a b可能出现两次的情况
					indegree[v]++;
					G[u][v] = 1;
				}
			}
			topsort();
			// 输出
			System.out.print(result[1]);
			for (int i = 2; i <= n; i++) {
				System.out.print(" "+result[i]);
			}
			System.out.println();
		}
	}

	private static void topsort() {
		result = new int[n + 1];
		que = new PriorityQueue<Integer>();
		int index = 0;
		for (int i = 1; i <= n; i++) {
			if (indegree[i] == 0)
				que.add(i);
		}
		while (!que.isEmpty()) {
			int u = que.poll();
			result[++index] = u;
			for (int i = 1; i <= n; i++) {
				if (G[u][i] == 1) {
					indegree[i]--;
					if (indegree[i] == 0)
						que.add(i);
				}
			}
		}
	}

}


 

 

package D0731;

import java.util.*;

//拓扑排序(使用邻接表实现)
public class HDU1285_3 {

	static int n, m;
	static int[] indegree;// 顶点的入度
	static int[] result;// 保存最后排好序的结果
	static List<ArrayList<Integer>> G;// 邻接表。
	static Queue<Integer> que;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			n = sc.nextInt();
			m = sc.nextInt();
			G = new ArrayList<ArrayList<Integer>>();
			for(int i = 1;i<=n+1;i++)
				G.add(new ArrayList<Integer>());
			indegree = new int[n + 1];
			while (m-- > 0) {
				int u = sc.nextInt();
				int v = sc.nextInt();
				if (!G.get(u).contains(v)) {// 这个条件一定要,否则WA,避免重边的情况比如a b可能出现两次的情况
					indegree[v]++;
					G.get(u).add(v);
				}
			}
			topsort();
			// 输出
			System.out.print(result[1]);
			for (int i = 2; i <= n; i++) {
				System.out.print(" " + result[i]);
			}
			System.out.println();
		}
	}

	private static void topsort() {
		result = new int[n + 1];
		que = new PriorityQueue<Integer>();
		int index = 0;
		for (int i = 1; i <= n; i++) {
			if (indegree[i] == 0)
				que.add(i);
		}
		while (!que.isEmpty()) {
			int v = que.poll();
			result[++index] = v;
			for (int i : G.get(v)) {
				indegree[i]--;
				if (indegree[i] == 0)
					que.add(i);
			}
		}
	}

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怎么演

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值