家庭关系  

时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte
总提交: 38            测试通过: 9

描述

给定若干家庭成员之间的关系,判断2个人是否属于同一家庭,即2个人之间均可以通过这些关系直接或者间接联系。

输入

输入数据有多组,每组数据的第一行为一个正整数n(1<=n<=100),表示有100个关系描述,接下来有n行,每行的描述方式为:
p1 p2 c
其中p1、p2和c均为一串文本,表示每个人的姓名,p1和p2为c的父亲和母亲。
最后一行包含2个字符串a和b,为待判断的两个人的姓名。
每个人的姓名由大小写字母组成,长度不超过80。

若n为0,表示输入结束。

输出

如果a和b在同一个家庭中,则输出Yes
否则输出No

样例输入

2
Barbara Bill Ted
Nancy Ted John
John Barbara
3
Lois Frank Jack
Florence Bill Fred
Annie Fred James
James Jack
0

样例输出

Yes
No

题目上传者

crq

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
const int maxn = 1000;
int gn; int f[maxn]; int getfather(int x) {
if(x == f[x]) return x;
else return f[x] = getfather(f[x]);
} void work(int x, int y, int z) {
int t1 = getfather(x);
int t2 = getfather(y);
if(t1 != t2) {
f[t1] = t2;
}
int t3 = getfather(z);
if(t2 != t3) {
f[t3] = t2;
}
} int main()
{
int i, j;
map<string, int> mymap;
map<string, int>::iterator it;
while(scanf("%d", &gn) != EOF && gn)
{
int cnt = 0;
for(i = 0; i < maxn; i++) f[i] = i;//!!!!
mymap.clear();
string str[3];
for(i = 0; i < 3; i++) str[i].clear();
for(i = 0; i < gn; i++) {
for(j = 0; j < 3; j++) {
cin >> str[j];
it = mymap.find(str[j]);
if(it == mymap.end()){
mymap[str[j]] = ++cnt;
}
}
int a[3];
a[0] = mymap[str[0]];
a[1] = mymap[str[1]];
a[2] = mymap[str[2]];
work(a[0], a[1], a[2]);
}
string s, t;
cin >> s >> t;//所有判读的人不在集合里面.
int start = 0;
int endx = 0;
start = mymap[s];
// printf("start = %d\n", start);
endx = mymap[t];
// printf("endx = %d\n", endx);
if(start == 0 || endx == 0) {//WA了好多次.
printf("No\n");
continue;
}
int t1 = getfather(start);
int t2 = getfather(endx);
if(t1 == t2)
cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}

TOJ3660家庭关系(并查集+hash+图的连通性)的更多相关文章

  1. PAT甲题题解-1126. Eulerian Path (25)-欧拉回路+并查集判断图的连通性

    题目已经告诉如何判断欧拉回路了,剩下的有一点要注意,可能图本身并不连通. 所以这里用并查集来判断图的联通性. #include <iostream> #include <cstdio ...

  2. 又见关系并查集 以POJ 1182 食物链为例

    简单的关系并查集一般非常easy依据给出的关系搞出一个有向的环,那么两者之间的关系就变成了两者之间的距离. 对于此题: 若u.v不在一个集合内,则显然此条语句会合法(暂且忽略后两条.下同). 那么将f ...

  3. poj 1182 食物链(关系并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62824   Accepted: 18432 Description ...

  4. PATL2-007. 家庭房产-并查集

    L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定每个人的家庭成员和其自己名下的房产,请你统计出每个 ...

  5. Find them, Catch them(POJ 1703 关系并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38668   Accepted: ...

  6. poj 2492(关系并查集) 同性恋

    题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...

  7. poj 1182 (关系并查集) 食物链

    题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...

  8. BZOJ1050 [HAOI2006]旅行comf[并查集判图连通性]

    ★ Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求 一条路径,使得路径 ...

  9. tyvj1017 - 冗余关系 ——并查集

    题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1017 并查集 #include <cstdio> #include <cstdlib& ...

随机推荐

  1. CSS3美化表单 移动端可用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  2. C#一个字符串的加密与解密

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...

  3. 浅析tornado web框架

    tornado简介 1.tornado概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Py ...

  4. ubuntu14.04安装wine以及国际版QQ

    公社(http://www.linuxidc.com/linux/2014-06/103768.htm)说: Wine (“Wine Is Not an Emulator” 的首字母缩写)是一个能够在 ...

  5. Xcode中报错或警告信息整理,持续更新...

     整理报错和警告信息,为以后自己查看方便用! [报错1] 信息:Static table views are only valid when embedded in UITableViewContro ...

  6. python学习随笔

    1 高阶函数的使用: import math def add(x, y, f): return f(x) + f(y) sq = math.sqrt print add(25, 9,sq) 2. ma ...

  7. Codeforces Round #154 (Div. 2) : B

    一个很简单的题: 方法一: 二分. 代码: #include<cstdio> #include<algorithm> #define maxn 100005 using nam ...

  8. JSP页面间传递参数的5种方法

    JSP页面间传递参数是经常需要使用到的功能,有时还需要多个JSP页面间传递参数.下面介绍一下实现的方法. (1)直接在URL请求后添加 如:< a href="thexuan.jsp? ...

  9. 【HDOJ】2414 Chessboard Dance

    简单DFS. /* 2414 */ #include <cstdio> #include <cstring> #include <cstdlib> ; ][]; i ...

  10. 【HDOJ】1823 Luck and Love

    二维线段树.wa了几次,不存在输出-1,而不再是一位小数. #include <cstdio> #include <cstring> #define MAXN 105 #def ...