NYOJ--20(搜索)-题目----------------------------- 吝啬的国度

package search;

/*吝啬的国度
 时间限制:1000 ms  |  内存限制:65535 KB
 
 描述
 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来。
 现在,Tom在第S号城市,他有张该国地图,他想知道如果自己要去参观第T号城市,
 必须经过的前一个城市是几号城市(假设你不走重复的路)。

 输入
 第一行输入一个整数M表示测试数据共有M(1<=M<=5)组
 每组测试数据的第一行输入一个正整数N(1<=N<=100000)和一个正整数S(1<=S<=100000),N表示城市的总个数,S表示参观者所在城市的编号
 随后的N-1行,每行有两个正整数a,b(1<=a,b<=N),表示第a号城市和第b号城市之间有一条路连通。
 输出
 每组测试数据输N个正整数,其中,第i个数表示从S走到i号城市,必须要经过的上一个城市的编号。(其中i=S时,请输出-1)
 样例输入
 1
 10 1
 1 9
 1 8
 8 10
 10 3
 8 6
 1 2
 10 4
 9 5
 3 7
 样例输出
 -1 1 10 10 9 8 3 1 1 8

 思路:一看到这题,我第一感觉就是邻接表+深搜,但是不知道为什么一直都是RE!
 后来才发现应该是用IO输入处理的时候,数组开在main里头,内存过大就RE了,我是这样猜的!
 于是换成Scanner,再测试就是WA了,好苦逼!思考原因,好久才发现,是因为result这个记录结果的数组在一开始没有初始化!
 因为,这题是允许多组数据的,前一组数据给出的结果对后一组数据是有影响的,因为深搜里有一个if的判断!
 总体,勉强通过此题!
 */
//方法一:深搜!
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;

public class search_20 {

	private static city cs[] = new city[100100];
	private static int S;
	private static int result[] = new int[100100];

	// 城市类
	public static class city {

		int go, to;
		city next;

		public city() {
		}

		public city(int go, int to) {
			this.go = go;
			this.to = to;
		}
	}

	public static void main(String[] args) {

		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));

		int T = sc.nextInt();

		while (T-- > 0) {

			// 初始化result
			Arrays.fill(result, 0);

			int N = sc.nextInt();
			S = sc.nextInt();

			// 初始化
			for (int i = 1; i < N + 1; i++)
				cs[i] = new city();

			// 创建邻接表
			for (int i = 1; i < N; i++) {
				int go = sc.nextInt();
				int to = sc.nextInt();

				city co = cs[go];
				while (co.next != null)
					co = co.next;
				co.next = new city(go, to);

				city ct = cs[to];
				while (ct.next != null)
					ct = ct.next;
				ct.next = new city(to, go);

			}

			// 自己到自己,就是-1
			result[S] = -1;
			dfs(cs[S].next);// 深搜
			for (int i = 1; i < N + 1; i++) {
				System.out.print(result[i] + " ");
			}
		}
		sc.close();
	}

	// 深搜
	private static void dfs(city go) {

		while (go != null) {
			// 如果获取到了结果,那么直接next
			if (result[go.to] == 0) {
				result[go.to] = go.go;
				// 搜到最后为止
				dfs(cs[go.to].next);
			}
			go = go.next;
		}
	}
}
//方法二:广搜,内存消耗是方法一的一倍,唉,尽力了!
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.TreeMap;

public class search_bfs_20 {

	// 存储起点与其各个可以去的城市
	private static TreeMap<Integer, LinkedList<Integer>> map = new TreeMap<Integer, LinkedList<Integer>>();
	private static boolean visited[];// 用于记录哪些城市是被访问过了
	private static int result[];// 记录结果

	public static void main(String[] args) {

		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));

		int T = sc.nextInt();
		while (T-- > 0) {

			int N = sc.nextInt();
			int S = sc.nextInt();

			//初始化
			for (int i = 0; i <= N; i++)
				map.put(i, new LinkedList<Integer>());
			visited = new boolean[N + 1];
			result = new int[N + 1];

			for (int i = 0; i < N - 1; i++) {
				int x = sc.nextInt();
				int y = sc.nextInt();

				map.get(x).add(y);
				map.get(y).add(x);
			}
			
			bfs(S);//广搜
			result[S] = -1;
			for (int i = 1; i < result.length; i++) {
				System.out.print(result[i] + " ");
			}
		}
		sc.close();
	}

	private static void bfs(int S) {

		LinkedList<Integer> queue = new LinkedList<Integer>();
		queue.add(S);//先将起点放入队列
		visited[S] = true;

		while (!queue.isEmpty()) {

			int s = queue.remove();
			LinkedList<Integer> place = map.get(s);
			for (int i = 0; i < place.size(); i++) {
				int p = place.get(i);//获取到与s城市相通的所有城市
				if (!visited[p]) {
					result[p] = s;
					visited[p] = true;
					queue.add(p);
				}
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值