布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位。无论如何,总不能把两个死对头排到同一张宴会桌旁!这个艰巨任务现在就交给你,对任何一对客人,请编写程序告诉主人他们是否能被安排同席。
输入格式:
输入第一行给出3个正整数:N
(≤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
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
// 打表作朋友之间的关系
vector<set<int> > maps(110);
int N,M,K;
int p1,p2,rel;
int main(){
cin>>N>>M>>K;
for (int i=0;i<M;i++){
//一号人物。二号人物,关系
cin>>p1>>p2>>rel;
//如果二者是敌人 就把负的对方存入到自己的空间,如果是朋友则直接存对方
if(rel==-1){
maps[p1].insert(-p2);
maps[p2].insert(-p1);
}else{
maps[p1].insert(p2);
maps[p2].insert(p1);
}
}
/*
set<int>:: iterator it;
vector<set<int> >::iterator iter;
set<int>vec_temp;
for( iter =maps.begin();iter!=maps.end();iter++){
vec_temp=*iter;
for(it =vec_temp.begin();it!=vec_temp.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
*/
//用来查找是否是朋友
set<int>::iterator it;
//用来查找是否是敌人
set<int>::iterator it2;
//用来遍历循环
set<int>::iterator it3;
//用来查找是否有朋友的朋友
set<int>::iterator it4;
for(int i=0;i<K;i++){
cin>>p1>>p2;
//用来取出两张对照表
set<int>sp1,sp2;
sp1=maps[p1];
sp2=maps[p2];
it=sp1.find(p2);
it2=sp1.find(-p2);
for(it3=sp1.begin();it3!=sp1.end();it3++){
it4=sp2.find(*it3);
if (it4!=sp2.end()){
break;
}
}
if(it != sp1.end()){
printf("No problem\n");
}else if (it2 != sp1.end()&&it4==sp2.end()){
printf("No way\n");
}else if (it4!=sp2.end()&&it2 != sp1.end()){
printf("OK but...\n");
}else{
printf("OK\n");
}
}
return 0;
}
#python 没写好,有一个点超时了,然后去换C++写了
inputs=input().split()
maps=[[]for i in range(110)]
# print(inputs[2])
for i in range(int(inputs[1])):
p1,p2,relation=map(int,input().split())
# print(p1,p2,relation)
if relation==1:
maps[p1].append(p2)
maps[p2].append(p1)
elif relation==-1:
maps[p1].append(-p2)
maps[p2].append(-p1)
for i in range(int(inputs[2])):
p1, p2 = map(int, input().split())
mp1=maps[p1]
mp2=maps[p2]
relations=[val for val in mp1 if val in mp2]
# 第二种取交集的方法 感觉还更慢了 list(set(mp1)&set(mp2))
# relations=[val for val in mp1 if val in mp2]
if -p1 in mp2 and -p2 in mp1 and len(relations)<=0 :
print("No way")
elif p1 in mp2 and p2 in mp1:
print("No problem")
elif len(relations)>0:
print("OK but...")
else:
print("OK")
# print(maps)