拓扑排序模板

	static boolean topsort() {//拓扑排序模板
		int hh = 0, tt = -1;
		for(int i = 1; i <= n; i ++ ) {
			if(d[i] == 0){//入边为0
				q[++ tt] = i;
			}
		}
		
		while(hh <= tt) {
			int t = q[hh ++];//取出一个入边为0的点
			for(int i = h[t]; i != -1; i = ne[i]) {//遍历这个点的出边
				int j = e[i];
				if(-- d[j] == 0) //如果该点j减去i这条入边后,入边为0
					q[++ tt] = j; //就放进队列
			}
		}
		
		return tt == n - 1;
	}

例题
3696. 构造有向无环图

import java.io.*;
import java.util.*;
import java.util.logging.StreamHandler;
public class Main {
	static int N = 200010;
	static int M = 200010;
	static int []h = new int [N];
	static int []e = new int [M];
	static int []ne = new int [M];
	static int [] q = new int [N];
	static int []d = new int [N];//点i的入边数量
	static int pos[] = new int[M];//存放拓扑排序后各点的位置
	static int n,m,idx;
	static Edge edge[] = new Edge[N];
	static BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
	static boolean topsort() {//拓扑排序模板
		int hh = 0, tt = -1;
		for(int i = 1; i <= n; i ++ ) {
			if(d[i] == 0){//入边为0
				q[++ tt] = i;
			}
		}
		
		while(hh <= tt) {
			int t = q[hh ++];//取出一个入边为0的点
			for(int i = h[t]; i != -1; i = ne[i]) {//遍历这个点的出边
				int j = e[i];
				if(-- d[j] == 0) //如果该点j减去i这条入边后,入边为0
					q[++ tt] = j; //就放进队列
			}
		}
		
		return tt == n - 1;
	}
	static void add(int a, int b) {
		e[idx] = b;
		ne[idx] = h[a];
		h[a] = idx ++;
	}
    public static void main(String[] args) throws IOException {
        //Scanner sc = new Scanner(System.in);
    	
    	
        int T = Integer.parseInt(re.readLine());
        while(T --> 0) {
        	idx = 0;
            int cnt = 0;
        	Arrays.fill(h, -1);
        	Arrays.fill(d, 0);
        	String split[] = re.readLine().split(" "); 
        	n = Integer.parseInt(split[0]);
        	m = Integer.parseInt(split[1]);
        	while(m --> 0) {
        		String split2[] = re.readLine().split(" "); 
            	int t = Integer.parseInt(split2[0]);
            	int x = Integer.parseInt(split2[1]);
            	int y = Integer.parseInt(split2[2]);
        		if(t == 0) {
        			edge[cnt ++] = new Edge(x,y);
        		}else {
        			add(x,y);
        			d[y] ++;
        		}
        	}
        	
        	if(!topsort()) {
        		System.out.println("NO");
        	}else {
        		System.out.println("YES");
        		for(int i = 1; i <= n; i ++ ) {
        			for(int j = h[i]; j != -1; j = ne[j]) {
        				System.out.println(i + " " + e[j]);
        			}
        		}
        		for(int i = 0; i < n; i ++ )  pos[q[i]] = i;
        		for(int i = 0; i < cnt; i ++ ) {
        			int a = edge[i].a;
        			int b = edge[i].b;
        			if(pos[a] > pos[b]) {
        				int t = a;
        				a = b;
        				b = t;
        			}
        			System.out.println(a + " " + b);
        		}
        	}
        }
    	//sc.close();
        pw.close();
    }
}
class Edge{
	int a,b;

	public Edge(int a, int b) {
		super();
		this.a = a;
		this.b = b;
	}
	
	public Edge() {
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值