潜在好友(CSU1868)

Description

小X在搬砖写一个论坛,这个时候老板突然想到一个功能,让小X今天赶快实现。大概就是如果某个人是你好友的好友那么他的头像上面会有特殊的标志。小X想不到较好的办法来解决如何验证两个人是不是好友的好友,现在向你求助。

Input

第一行是一个整数T(1<=T<=100),代表数据组数。 每组数据第一行是两个整数n,m(1<=n,m<=10000),代表这两个人的好友的数量。 之后n行是第一个人的好友id 再之后m行是第二个人的好友id (1<=id<=1e9)

Output

如果第二个人是第一个人的好友的好友输出Yes否则No

Sample Input

2
1 1
1
2
2 3
1
2
2
3
4

Sample Output

No
Yes
cin超时,scanf就过了。
#include <map>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
	map<int,int>p;
	int t, m, n, x, g;
	scanf("%d", &t);
	while(t--) {
		g = 0;
		p.clear();
		scanf("%d %d", &m, &n);
		while(m--) {
			scanf("%d", &x);
			p[x] = 1;
		}
		while(n--) {
			scanf("%d", &x);
			if(p[x]) {
				g = 1;
			}
		}
		if(g) {
			cout<<"Yes"<<endl;
		} else {
			cout<<"No"<<endl;
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/adusts/article/details/80502528