洛谷 P3367 并查集模板
#include<cstdio>
using namespace std;
int n,m,p;
int father[];
int find(int x)
{
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
void unionn(int i,int j)
{
father[j]=i;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
father[i]=i;
for(int i=;i<=m;i++)
{
int x,y,z,r1,r2;
scanf("%d%d%d",&z,&x,&y);
if(z==)
{
r1=find(x);
r2=find(y);
if(r1!=r2)
unionn(r1,r2);
}
if(z==)
{
if(find(x)==find(y))
printf("Y\n");
else printf("N\n");
}
} return ;
}
洛谷的网站编排对题目粘贴不友好,所以没题目
链接在这:https://www.luogu.org/problem/show?pid=3367
洛谷 P3367 并查集模板的更多相关文章
- 洛谷 P3367 并查集 【模板题】
题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入输出格式 输入格式: 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数Zi.Xi.Yi 当Zi=1 ...
- 洛谷P3367并查集
传送门 #include <iostream> #include <cstdio> #include <cstring> #include <algorith ...
- P1536 村村通(洛谷)并查集
隔壁的dgdger带我看了看老师的LCA教程,我因为学习数学太累了(就是懒),去水了一下,感觉很简单的样子,于是我也来写(水)个博客吧. 题目描述 某市调查城镇交通状况,得到现有城镇道路统计表.表中列 ...
- 洛谷 P3367 【模板】并查集
P3367 [模板]并查集 题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入输出格式 输入格式: 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数 ...
- 【并查集模板】 【洛谷P2978】 【USACO10JAN】下午茶时间
P2978 [USACO10JAN]下午茶时间Tea Time 题目描述 N (1 <= N <= 1000) cows, conveniently numbered 1..N all a ...
- HDU 1213 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- 洛谷 P4148 简单题 KD-Tree 模板题
Code: //洛谷 P4148 简单题 KD-Tree 模板题 #include <cstdio> #include <algorithm> #include <cst ...
- 【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 ...
随机推荐
- Mysql之锁的基本介绍
数据库锁定机制简单来说,就是数据库为了保证数据的一致性,而使各种共享资源在被并发访问变得有序所设计的一种规则.对于任何一种数据库来说都需要有相应的锁定机制,所以MySQL自然也不能例外.MySQL数据 ...
- C#中Unity对象的注册方式与生命周期解析
1.示例代码 请详细阅读 static void Main(string[] args) { { Console.WriteLine("----------全局设置----------&qu ...
- Python3 MySQL
首先安装pymysql pip install pymysql 准备数据库:创建一个数据库testdb mysql实例: import pymysql #打开数据库连接,使用数据库所在的IP127. ...
- centos下安装nginx(转载)
http://blog.csdn.net/u010246789/article/details/51501710 有声明,不能转载,所以,就把地址弄了过来
- VS2017 Git failed with a fatal error. error: open(".vs/xxxxxx/v15/Server/sqlite3/db.lock"): Permission denied fatal: Unable to process path .vs/xxxxxx/v15/Server/sqlite3/db.lock
具体错误信息:Git failed with a fatal error. error: open(".vs/xxxxxx/v15/Server/sqlite3/db.lock") ...
- 安恒pwn魔法
魔法这是比较基础的一道栈溢出: 首先看下开启的防护机制 Checksec magicc发现只有nx防护 我们载入ida发现溢出点 Buf实际溢出空间为0x16,构造exp import time fr ...
- 一个关于integer表示范围的问题
1:在做字符串parse为integer 类型市,一直出现问题就是当我parse “”2851663837”报错, 但是当我parse “1417585794” 可以.原来是超出integer 类型 ...
- django学习记录1
在看django的基础知识,还是按照以前一样来总结,还是晚上再统一总结好了,边看边总结好像效果不大. 第一部分是生成数据库,用python manage.migrate命令来生成django的基础架构 ...
- c# System.Collections接口图
- Linux磁盘管理——日志文件系统与数据一致性
参考:Linux磁盘管理——Ext2文件系统 数据不一致 上图是Ext2结构图,其他FS结构类似. 一般来说,我们将 inode table 与 data block 称为数据区:至于其他例如 sup ...