【并查集模板】 【洛谷P2978】 【USACO10JAN】下午茶时间
P2978 [USACO10JAN]下午茶时间Tea Time
题目描述
N (1 <= N <= 1000) cows, conveniently numbered 1..N all attend a tea time every day. M (1 <= M <= 2,000) unique pairs of those cows have already met before the first tea time. Pair i of these cows who have met is specified by two differing integers A_i and
B_i (1 <= A_i <= N; 1 <= B_i <= N). The input never indicates that cows have met each other more than once.
At tea time, any cow i and cow j who have met a mutual friend cow k will meet sometime during that tea time and thus expand their circle of known cows.
Determine whether Q (1 <= Q <= 100) pairs of cows have met after tea times are held for long enough that no new cow meetings are occurring. Query j consists of a pair of different cows X_j and Y_j (1 <= X_j <= N; 1 <= Y_j <= N).
For example, suppose that out of cows 1 through 5, we know that 2 has met 5, 2 has met 3, and 4 has met 5; see (a) below.
2---3 2---3 2---3
\ |\ | |\ /|
1 \ --> 1 | \ | --> 1 | X |
\ | \| |/ \|
4---5 4---5 4---5
(a) (b) (c)
In the first tea time, cow 2 meets cow 4, and cow 3 meets cow 5; see (b) above. In the second tea time, cow 3 meets cow 4; see (c) above.
N(1 <= N <= 1000)头奶牛,编号为1..N,在参加一个喝茶时间活动。在喝茶时间活动开始之前,已经有M(1 <= M <= 2,000)对奶牛彼此认识(是朋友)。第i对彼此认识的奶牛通过两个不相同的整数Ai和Bi给定(1<= Ai <= N; 1 <= Bi <= N)。输入数据保证一对奶牛不会出现多次。 在喝茶时间活动中,如果奶牛i和奶牛j有一个相同的朋友奶牛k,那么他们会在某次的喝茶活动中去认识对方(成为朋友),从而扩大他们的社交圈。 请判断,在喝茶活动举办很久以后(直到没有新的奶牛彼此认识),Q(1
<= Q <= 100)对奶牛是否已经彼此认识。询问j包含一对不同的奶牛编号Xj和Yj(1 <= Xj <= N; 1 <= Yj <= N)。 例如,假设共有1..5头奶牛,我们知道2号认识5号,2号认识3号,而且4号认识5号;如下图(a)。
2---3 2---3 2---3
\ |\ | |\ /|
1 \ --> 1 | \ | --> 1 | X |
\ | \| |/ \|
4---5 4---5 4---5
(a) (b) (c)
在某次的喝茶活动中,2号认识4号,3号认识5号;如上图(b)所示。接下来的喝茶活动中,3号认识4号,如上图(c)所示。
输入输出格式
输入格式:
Line 1: Three space-separated integers: N, M, and Q
Lines 2..M+1: Line i+1 contains two space-separated integers: A_i and B_i
- Lines M+2..M+Q+1: Line j+M+1 contains query j as two space-separated integers: X_j and Y_j
行1:三个空格隔开的整数:N,M,和Q
行2..M+1:第i+1行包含两个空格隔开的整数Ai和Bi
行M+2..M+Q+1:第j+M+1行包含两个空格隔开的整数Xj和Yj,表示询问j
输出格式:
- Lines 1..Q: Line j should be 'Y' if the cows in the jth query have met and 'N' if they have not met.
行1..Q:如果第j个询问的两头奶牛认识, 第j行输出“Y”。如果不认识,第j行输出“N”
输入输出样例
5 3 3
2 5
2 3
4 5
2 3
3 5
1 5
Y
Y
N
说明
感谢@蒟蒻orz神犇 提供翻译。
并查集模板题。不多加赘述,包括了路径压缩、寻找代表元素、集合合并,其他的操作都很好想。看懂这道题并查集就会了(会不会做题还请移步另一道题:小胖的奇偶)。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstdlib> const int INF = 9999999;
const int MAXN = 2000 + 10;
const int MAXM = 1000 + 10; int m,n,q;
int fa[MAXN]; int find(int x)
{
return fa[x] == x? fa[x] : fa[x] = find(fa[x]);
} int main()
{
scanf("%d%d%d", &n, &m, &q);
for(int i = 1;i <= n; i++)
{
fa[i] = i;
}
for(int i = 1;i <= m;i++)
{
int a,b;
scanf("%d%d", &a, &b);
fa[find(a)] = find(b);
}
for(int i = 1;i <= q; i++)
{
int a,b;
scanf("%d%d", &a, &b);
if(find(a) == find(b))
{
printf("Y\n");
}
else
{
printf("N\n");
}
}
return 0;
}
【并查集模板】 【洛谷P2978】 【USACO10JAN】下午茶时间的更多相关文章
- 洛谷 P2978 [USACO10JAN]下午茶时间Tea Time
P2978 [USACO10JAN]下午茶时间Tea Time 题目描述 N (1 <= N <= 1000) cows, conveniently numbered 1..N all a ...
- 种类并查集(洛谷P2024食物链)
题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的一种,但是我 ...
- 并查集【洛谷P1197】 [JSOI2008]星球大战
P1197 [JSOI2008]星球大战 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系 ...
- 【数论】卢卡斯定理模板 洛谷P3807
[数论]卢卡斯定理模板 洛谷P3807 >>>>题目 [题目] https://www.luogu.org/problemnew/show/P3807 [输入格式] 第一行一个 ...
- HDU 1213 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...
- 【2018寒假集训Day 8】【并查集】并查集模板
Luogu并查集模板题 #include<cstdio> using namespace std; int z,x,y,n,m,father[10001]; int getfather(i ...
- POJ-图论-并查集模板
POJ-图论-并查集模板 1.init:把每一个元素初始化为一个集合,初始化后每一个元素的父亲节点是它本身,每一个元素的祖先节点也是它本身(也可以根据情况而变). void init() { for ...
- KMP字符串匹配 模板 洛谷 P3375
KMP字符串匹配 模板 洛谷 P3375 题意 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.(如果 ...
随机推荐
- NSLayoutConstraint-代码实现自动布局的函数用法说明
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainbownight.blog.51cto.com/1336585/13161 ...
- 杂项-公司:Google
ylbtech-杂项-公司:Google 谷歌公司(Google Inc.)成立于1998年9月4日,由拉里·佩奇和谢尔盖·布林共同创建,被公认为全球最大的搜索引擎公司.谷歌是一家位于美国的跨国科技企 ...
- java软引用、弱引用(转摘)
本文转自网络,源地址:https://www.jianshu.com/p/b56731447179 一.引用对象类型定义 首先,引用对象在Java定义中有三种类型,从弱到强依次为:软引用.弱引用与虚引 ...
- 08_springmvc数据回显和@ModelAttribute注解详解
一.数据回显 提交后,如果出现错误,将刚才提交的数据回显到刚才的提交页面. 二.pojo数据回显方法 1.springmvc默认对pojo数据进行回显. pojo数据传入controller方法后,s ...
- Struts2入门问题
一 使用Struts 2 开发程序的基本步骤 加载Struts2 类库 配置web.xml文件 开发视图层页面 开发控制层Action 配置struts.xml文件 部署.运行项目 第一步先导架包:在 ...
- Mybatis Plus 入坑(含最新3.X配置)
简介 Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 使用它可以简化单表的操作, 节省开发时间, 国人 ...
- [Swoole系列入门教程 6] TCP协议和粘包
- PAT甲级——A1039 Course List for Student
Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists o ...
- JS的闭包问题
1.什么是“闭包” 是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 2.闭包的应用场景 (1)保护变量的安全实现JS私有属性和私有方法 (2)在 ...
- Luogu P3106 [USACO14OPEN]GPS的决斗Dueling GPS's(最短路)
P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题意 题目描述 Farmer John has recently purchased a new car online, ...