L2-010 排座位 (25分) java

布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位。无论如何,总不能把两个死对头排到同一张宴会桌旁!这个艰巨任务现在就交给你,对任何一对客人,请编写程序告诉主人他们是否能被安排同席。

输入格式:

输入第一行给出3个正整数:N≤\le100),即前来参宴的宾客总人数,则这些人从1到N编号;M为已知两两宾客之间的关系数;K为查询的条数。随后M行,每行给出一对宾客之间的关系,格式为:宾客1 宾客2 关系,其中关系为1表示是朋友,-1表示是死对头。注意两个人不可能既是朋友又是敌人。最后K行,每行给出一对需要查询的宾客编号。

这里假设朋友的朋友也是朋友。但敌人的敌人并不一定就是朋友,朋友的敌人也不一定是敌人。只有单纯直接的敌对关系才是绝对不能同席的。

输出格式:

对每个查询输出一行结果:如果两位宾客之间是朋友,且没有敌对关系,则输出No problem;如果他们之间并不是朋友,但也不敌对,则输出OK;如果他们之间有敌对,然而也有共同的朋友,则输出OK but...;如果他们之间只有敌对关系,则输出No way

输入样例:

7 8 4
5 6 1
2 7 -1
1 3 1
3 4 1
6 7 -1
1 2 1
1 4 1
2 3 -1
3 4
5 7
2 3
7 2

输出样例:

No problem
OK
OK but...
No way
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import javax.management.monitor.Monitor;


//** 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("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add ceck for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
	static boolean hasNext()throws IOException {
		return tokenizer.hasMoreTokens();
	}
	static String nextLine() throws IOException{
		return reader.readLine();
	}
	static char nextChar() throws IOException{
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}
	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
	static void close() throws IOException {
		reader.close();
	}
}
class Writer{
	static BufferedWriter writer;
	static void init(OutputStream outputStream) {
		writer = new BufferedWriter(new OutputStreamWriter(outputStream));
	}
	static void print(Object object) throws IOException {
		writer.write(object.toString());
	}
	static void println(Object object) throws IOException {
		writer.write(object.toString());
		writer.write("\n");
	}
	static void close() throws IOException {
		// TODO Auto-generated method stub
		writer.close();
	}
}
public class Main {
	
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		Writer.init(System.out);
		solve();
		Reader.close();
		Writer.close();
	}
	static int[][]map = new int[101][101];
	static int[]pre = new int[101];
	private static void solve() throws IOException {
		int n = Reader.nextInt();
		int m = Reader.nextInt();
		int k = Reader.nextInt();
		for (int i = 0; i < pre.length; i++) {
			pre[i] = i;
		}
		for (int i = 0; i < m; i++) {
			int t1 = Reader.nextInt();
			int t2 = Reader.nextInt();
			int r = Reader.nextInt();
			map[t1][t2] = map[t2][t1] = r;
			if (r==1) {
				union(t1,t2);
			}
		}
		for (int i = 0; i < k; i++) {
			int t1 = Reader.nextInt();
			int t2 = Reader.nextInt();
			if (map[t1][t2]==1) {
				Writer.println("No problem");
			}else if(map[t1][t2]==0) {
				Writer.println("OK");
			}else {
				if (find(t1)==find(t2)) {
					Writer.println("OK but...");
				}else {
					Writer.println("No way");
				}
			}
		}
	}
	static void union(int x,int y ) {
		int fx = find(x);
		int fy = find(y);
		if (fx!=fy) {
			pre[fx] = fy;
		}
	}
	private static int find(int x) {
		while (x!=pre[x]) {
			x = pre[x];
		}
		return x;
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章