洛谷P3367并查集
传送门
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#define re register
using namespace std;
const int maxn = 10005;
const int maxm = 200005;
inline int read(){
char ch = getchar();
int f = 1 , x = 0 ;
while(ch > '9' || ch < '0'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0'; ch = getchar();}
return x * f;
}
int n,m,z,x,y;
int fa[maxn];
inline int find(int x){
if(x != fa[x]) fa[x] = find(fa[x]);
return fa[x];
}
inline void init(){
for(re int i = 1 ; i <= n ; ++i)
fa[i] = i;
}
int main(){
n = read(); m = read();
init();
for(re int i = 1 ; i <= m ; ++i) {
z = read(); x = read(); y = read();
if(z == 1) {
int f1 = find(x) , f2 = find(y);
if(f1 != f2)
fa[f1] = f2;
}
else {
int f1 = find(x) , f2 = find(y);
if(f1 == f2) printf("Y\n");
else printf("N\n");
}
}
return 0;
}
洛谷P3367并查集的更多相关文章
- 洛谷 P3367 并查集 【模板题】
题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入输出格式 输入格式: 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数Zi.Xi.Yi 当Zi=1 ...
- 洛谷 P3367 并查集模板
#include<cstdio> using namespace std; int n,m,p; ]; int find(int x) { if(father[x]!=x) father[ ...
- P1536 村村通(洛谷)并查集
隔壁的dgdger带我看了看老师的LCA教程,我因为学习数学太累了(就是懒),去水了一下,感觉很简单的样子,于是我也来写(水)个博客吧. 题目描述 某市调查城镇交通状况,得到现有城镇道路统计表.表中列 ...
- 洛谷P3367 【模板】并查集
P3367 [模板]并查集 293通过 551提交 题目提供者HansBug 标签 难度普及- 提交 讨论 题解 最新讨论 不知道哪错了 为啥通不过最后三个节点 题解 不懂为什么MLE 最后一个数 ...
- 洛谷 P3367 【模板】并查集
P3367 [模板]并查集 题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入输出格式 输入格式: 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数 ...
- P3367 并查集【模板】 洛谷
https://www.luogu.org/problem/show?pid=3367#sub 题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入输出格式 输入格式: 第一行包含两个整 ...
- 洛谷P3367 【模板】并查集 模板 找baba
链接https://www.luogu.org/problem/P3367 #include<bits/stdc++.h> using namespace std; ; int fa[ra ...
- 【反向并查集、联通图】P1197 [JSOI2008]星球大战
题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧 ...
- 并查集与最小生成树Kruskal算法
一.什么是并查集 在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集的合并及查询问题.有一个联合-查找算法(union-find algorithm)定义了两个用于次数据结构的操作: Fi ...
随机推荐
- com.android.support:appcompat-v7 版本号问题
supportLibVersion 的头数字是和targetSdkVersion 版本一样的. ext { supportLibVersion = '22.2.1'} compile "co ...
- 【纪中集训2019.3.13】fft
题意: 描述 一共有\(n+m\)道题,其中\(n\)道答案是\(A\),\(m\)道答案是\(B\): 你事先知道\(n和m\),问在最优情况下的期望答错次数,对\(998244353\)取模: 范 ...
- supervisor 添加新配置不生效的问题
supervisorctl reread supervisorctl reload (不运行这一步会导致启动不了) supervisorctl start xxx:* 提示:No config upd ...
- 设计模式之单例模式实现(C++)
#ifndef SINGLETON_H #define SINGLETON_H #include <cassert> #include <memory> #include &l ...
- pymc
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- 数据分析与展示---Matplotlib基本绘图函数
一:基本绘图函数(这里介绍16个,还有许多其他的) 二:pyplot饼图plt.pie的绘制 三:pyplot直方图plt.hist的绘制 (一)修改第二个参数bins:代表直方图的个数,均分为多段, ...
- Broker流量均衡 prefer reassign
0.均衡流量的步骤 现在的kafka集群,只要遇到过weibo_common_act2 topic的节点在ZK中丢失,就要prefer一次流量,否则不均匀. 总结均衡流量的一般步骤: 通过hpm查询b ...
- [USACO4.3]逢低吸纳Buy Low, Buy Lower
https://daniu.luogu.org/problemnew/show/2687 求方案数: if(f[j]+1==f[i] && a[j]>a[i]) s[i]+=s[ ...
- Redis实战(七)Redis开发与运维
Redis用途 1.缓存 Redis提供了键值过期时间设置, 并且也提供了灵活控制最大内存和内存溢出后的淘汰策略. 可以这么说, 一个合理的缓存设计能够为一个网站的稳定保驾护航. 2.排行榜系统 Re ...
- #import 指令
[#import 指令] #import指令用于从一个类型库中结合信息.该类型库的内容被转换为C++类,主要用于描述COM界面. 语法 #import "文件名" [属性] #im ...