Skip to content

1090 危险品装箱

集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里。比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸。

本题给定一张不相容物品的清单,需要你检查每一张集装箱货品清单,判断它们是否能装在同一只箱子里。

输入格式:

输入第一行给出两个正整数:N (≤104) 是成对的不相容物品的对数;M (≤100) 是集装箱货品清单的单数。

随后数据分两大块给出。第一块有 N 行,每行给出一对不相容的物品。第二块有 M 行,每行给出一箱货物的清单,格式如下:

K G[1] G[2] ... G[K]
K G[1] G[2] ... G[K]

其中 K (≤1000) 是物品件数,G[i] 是物品的编号。简单起见,每件物品用一个 5 位数的编号代表。两个数字之间用空格分隔。

输出格式:

对每箱货物清单,判断是否可以安全运输。如果没有不相容物品,则在一行中输出 Yes,否则输出 No

输入样例:

tex
6 3
20001 20002
20003 20004
20005 20006
20003 20001
20005 20004
20004 20006
4 00001 20004 00002 20003
5 98823 20002 20003 20006 10010
3 12345 67890 23333
6 3
20001 20002
20003 20004
20005 20006
20003 20001
20005 20004
20004 20006
4 00001 20004 00002 20003
5 98823 20002 20003 20006 10010
3 12345 67890 23333

输出样例:

tex
No
Yes
Yes
No
Yes
Yes

Solution:

WARNING

测试点 2、3 运行超时

java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String[] arr = in.readLine().split(" ");
		int N = Integer.parseInt(arr[0]);
		int M = Integer.parseInt(arr[1]);
		Map<String, LinkedList<String>> map = new HashMap<String, LinkedList<String>>();
		for (int i = 0; i < N; i++) {
			String[] box = in.readLine().split(" ");
			map.computeIfAbsent(box[0], k -> new LinkedList<String>());
			map.get(box[0]).add(box[1]);
			map.computeIfAbsent(box[1], k -> new LinkedList<String>());
			map.get(box[1]).add(box[0]);
		}
		find: for (int i = 0; i < M; i++) {
			String[] search = in.readLine().split(" ");
			for (int j = 1; j < search.length; j++) {
				for (int j2 = 1; j2 < search.length; j2++) {
					if (j != j2 && map.containsKey(search[j]) && map.get(search[j]).contains(search[j2])) {
						System.out.println("No");
						continue find;
					}
				}
			}
			System.out.println("Yes");
		}
	}

}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String[] arr = in.readLine().split(" ");
		int N = Integer.parseInt(arr[0]);
		int M = Integer.parseInt(arr[1]);
		Map<String, LinkedList<String>> map = new HashMap<String, LinkedList<String>>();
		for (int i = 0; i < N; i++) {
			String[] box = in.readLine().split(" ");
			map.computeIfAbsent(box[0], k -> new LinkedList<String>());
			map.get(box[0]).add(box[1]);
			map.computeIfAbsent(box[1], k -> new LinkedList<String>());
			map.get(box[1]).add(box[0]);
		}
		find: for (int i = 0; i < M; i++) {
			String[] search = in.readLine().split(" ");
			for (int j = 1; j < search.length; j++) {
				for (int j2 = 1; j2 < search.length; j2++) {
					if (j != j2 && map.containsKey(search[j]) && map.get(search[j]).contains(search[j2])) {
						System.out.println("No");
						continue find;
					}
				}
			}
			System.out.println("Yes");
		}
	}

}

Released under the MIT License.