家庭关系  

时间限制(普通/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. wamp安装注意点!

    安装wamp前或者重装系统后,默认没有依赖的组件VC11,需要先安装才能运行 下载地址:http://www.microsoft.com/en-us/download/details.aspx?id= ...

  2. smarty 模板 数字自动添加

    section: section的产生是为解决foreach的不足的,与foreach一样,它用于设计模板内的循环块,它较为复杂,可极大程序上满足程序需要,所以在程序中我习惯使用它而不使用foreac ...

  3. register 不允许 block 模式,而默认的是

    Exception in thread "main" java.nio.channels.IllegalBlockingModeException at java.nio.chan ...

  4. mvc4 to mvc5 and EF5 to EF6

    今天把 后台的mvc 升级到了mvc5和ef6 .出错很正常. 下面是一些错误信息. [A]System.Web.WebPages.Razor.Configuration.HostSection 无法 ...

  5. spoj 3885

    简单的博弈题,用dp解: 每个人只能拿1,l,k个硬币: dp[i][j]表示第j个人拿是否能拿第i枚硬币: 代码: #include<cstdio> #define maxn 10000 ...

  6. Js 表单序列化

    http://www.w3cmm.com/javascript/serialize-form.html

  7. django作models的UPDATE时,注意有过滤外键的情况

    就是如果要过滤的时候,存在外键,则需要取到原始字段的名称,不然就是ID. 原始名称以__(双下划线)引用. 如: def path2db(release_version, develop_versio ...

  8. hibernate的速度问题--hibernate.jdbc.fetch_size和 hibernate.jdbc.batch_size

    hibernate的速度问题 这点我也疑惑过,最初应用hibernate的项目,我也感觉速度很慢,知道后来才知道问题的所在.       其实hibernate的速度性能并不差,比起jdbc来说,又是 ...

  9. Android开源项目发现--- 工具类文件处理篇(持续更新)

    1.ZIP java压缩和解压库 项目地址:https://github.com/zeroturnaround/zt-zip 文档介绍:https://github.com/zeroturnaroun ...

  10. [LeetCode#12] Roman to Integer

    Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...