佈置宴席最微妙的事情,就是給前來參宴的各位賓客安排座位。無論如何,總不能把兩個死對頭排到同一張宴會桌旁!這個艱鉅任務現在就交給你,對任何一對客人,請編寫程序告訴主人他們是否能被安排同席。
輸入格式:
輸入第一行給出3個正整數:N
(≤\le≤100),即前來參宴的賓客總人數,則這些人從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;
}
}