求助!!!后面4个点RE
查看原帖
求助!!!后面4个点RE
520071
小黎黎黎黎黎楼主2021/8/25 10:22
import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
	static int idx,n,m,res,t,N=100005;
	static boolean[] vis=new boolean[N];
	static int[] h=new int[N],ne=new int[N],e=new int[N],q=new int[N];
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer st = new StreamTokenizer(br);
	static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
	public static void main(String[] args) throws IOException {
		st.nextToken();n=(int)st.nval;
		st.nextToken();m=(int)st.nval;
		List<P> list=new ArrayList<>();
		Arrays.fill(h,-1);
		for (int i = 0; i < m; i++) {
			st.nextToken();int a=(int)st.nval;
			st.nextToken();int b=(int)st.nval;
			list.add(new P(a, b));
		}
		Collections.sort(list);
		for (int i = 0; i < list.size(); i++) {
			int a=list.get(i).a,b=list.get(i).b;
			add(a,b);
		}
		dfs(1);
		bw.newLine();
		Arrays.fill(vis,false);
		bfs(1);
		bw.flush();
	}
	private static void bfs(int u) throws IOException {
		// TODO Auto-generated method stub
		int tt=-1,hh=0;
		q[++tt]=u;
		vis[u]=true;
		bw.write(u+" ");
		while(hh<=tt) {
			int t=q[hh++];
			for (int i = h[t]; i != -1; i = ne[i]) {
				int j=e[i];
				if (!vis[j]) {
					vis[j]=true;
					bw.write(j+" ");
					q[++tt]=j;
				}
			}
		}
	}
	private static void dfs(int u) throws IOException {
		// TODO Auto-generated method stub
		vis[u]=true;
		bw.write(u+" ");
		for (int i = h[u]; i != -1 ; i = ne[i]) {
			int j=e[i];
			if (!vis[j]) {
				dfs(j);
			}
		}
	}
	static void add(int a,int b) {
		e[idx]=b;
		ne[idx]=h[a];
		h[a]=idx++;
	}
}
class P implements Comparable<P>{
	int a,b;

	/**
	 * @param a
	 * @param b
	 */
	public P(int a, int b) {
		super();
		this.a = a;
		this.b = b;
	}

	@Override
	public int compareTo(P o) {
		// TODO Auto-generated method stub
		if (this.a!=o.a) {
			return this.a-o.a;
		}
		else {
			return o.b-this.b;
		}
	}
	
}
2021/8/25 10:22
加载中...