PTA 分而治之 Java

PTA 分而治之 Java

分而治之,各个击破是兵家常用的策略之一。在战争中,我们希望首先攻下敌方的部分城市,使其剩余的城市变成孤立无援,然后再分头各个击破。为此参谋部提供了若干打击方案。本题就请你编写程序,判断每个方案的可行性。

输入格式:
输入在第一行给出两个正整数 N 和 M(均不超过10 000),分别为敌方城市个数(于是默认城市从 1 到 N 编号)和连接两城市的通路条数。随后 M 行,每行给出一条通路所连接的两个城市的编号,其间以一个空格分隔。在城市信息之后给出参谋部的系列方案,即一个正整数 K (≤ 100)和随后的 K 行方案,每行按以下格式给出:

Np v[1] v[2] … v[Np]

其中 Np 是该方案中计划攻下的城市数量,后面的系列 v[i] 是计划攻下的城市编号。

输出格式:
对每一套方案,如果可行就输出YES,否则输出NO

输入样例:
10 11
8 7
6 8
4 5
8 4
8 1
1 2
1 4
9 8
9 1
1 10
2 4
5
4 10 3 8 4
6 6 1 7 5 4 9
3 1 8 4
2 2 8
7 9 8 7 6 5 4 2
输出样例:
NO
YES
YES
NO
NO

乍一看,并查集,判断连通数是否等于剩余城市数
仔细一想,所有的路都走不通了不就是所有城市都被孤立了吗…
可惜最后一个测试用例超时…

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int N = Reader.nextInt();
		int M = Reader.nextInt();
		// 读取数据
		linked_list[] path = new linked_list[N + 1]; // 记录有路通向外面的城市
		for (int i = 1; i < path.length; i++) {
			path[i] = new linked_list();
		}
		for (int i = 0; i < M; i++) {
			int s = Reader.nextInt();
			int e = Reader.nextInt();
			path[s].list.add(e); // 只需要记录一个方向即可
			path[s].exits = path[e].exits = 1; // 标记是有路的城市
		}
		int K = Reader.nextInt();
		for (int i = 0; i < K; i++) {
			// 读取攻打计划
			int Np = Reader.nextInt();
			int[] plan = new int[N + 1]; // 记录要攻打的城市
			for (int j = 0; j < Np; j++) {
				plan[Reader.nextInt()] = 1;
			}
			
			boolean isOK = true; // 判断是否可行
			
			for (int j = 1; j < path.length && isOK; j++) {
				// 如果该城市不是本来就孤立,且不在攻打的计划上
				if (path[j].exits == 1 && plan[j] != 1) {
					// 那么查找是否有城市和该城市相连接
					for (int item : path[j].list) {
						// 如果找到了一个不在攻打计划上的城市和该城市相连,则表示计划不可行
						if (plan[item] != 1) {
							isOK = false;
							break;
						}
					}
				}
			}
			if (isOK) {
				System.out.println("YES");
			} else {
				System.out.println("NO");
			}
		}
	}

	static class linked_list {
		int exits = 0;
		LinkedList<Integer> list = new LinkedList<Integer>();
	}
}

// Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	static void init(File file) {
		try {
			reader = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static char nextChar() throws IOException {
		return next().toCharArray()[0];
	}

	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}

	static Double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
}

发布了27 篇原创文章 · 获赞 2 · 访问量 1470

猜你喜欢

转载自blog.csdn.net/Samil_Hy/article/details/104208704