洛谷 P3916 图的遍历(java实现)

P3916 图的遍历

题目描述

给出N个点,M条边的有向图,对于每个点v,求A(v)表示从点v出发,能到达的编号最大的点。

输入格式

第1 行,2 个整数N,M。

接下来MM行,每行2个整数U_i,V_i,表示边(U_i,V_i)。点用1,2,⋯,N编号。

输出格式

N 个整数A*(1),A(2),⋯,A(*N)。

输入输出样例

输入 #1

4 3
1 2
2 4
4 3

输出 #1

4 4 3 4

说明/提示

• 对于60% 的数据,1≤N.M≤10^3;

• 对于100% 的数据,1≤N,M≤10^5。

题解

package p3916;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
	static int N = 100010;
	static int n,m;
	static int h[] = new int[N],e[] = new int[N],ne[] = new int[N],idx=0;
	static int a[] = new int[N];
	static void add(int a,int b) {
		e[idx] = b;ne[idx] = h[a];h[a] = idx++;
	}
	static void dfs(int u,int d) {
		if(a[u]!=0) {
			return;
		}
		a[u] = d;
		for(int i = h[u];i!=-1;i = ne[i]) {
			int j = e[i];
			dfs(j,d);
		}
		return;
	}
	public static void main(String[] args) throws Exception{
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String str[] = bf.readLine().split(" ");
		Arrays.fill(h, -1);
		n = Integer.parseInt(str[0]);m = Integer.parseInt(str[1]);
		while(m-->0) {
			str = bf.readLine().split(" ");
			int a = Integer.parseInt(str[0]),b = Integer.parseInt(str[1]);
			add(b,a);
		}
		bf.close();
		for(int i=n;i>0;i--) {
			dfs(i,i);
		}
		for(int i=1;i<=n;i++) {
			System.out.print(a[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、付费专栏及课程。

余额充值