LibreOJ #109. 并查集
题目描述
这是一道模板题。
维护一个 nnn 点的无向图,支持:
- 加入一条连接 uuu 和 vvv 的无向边
- 查询 uuu 和 vvv 的连通性
由于本题数据较大,因此输出的时候采用特殊的输出方式:用 000 或 111 代表每个询问的答案,将每个询问的答案一次从左到右排列,把得到的串视为一个二进制数,输出这个二进制数 mod 998244353\text{mod} ~ 998244353mod 998244353 的值。
输入格式
第一行包含两个整数 n,mn,mn,m,表示点的个数和操作的数目。
接下来 mmm 行每行包括三个整数 op,u,v\text{op},u,vop,u,v。
- 如果 op=0\text{op} = 0op=0,则表示加入一条连接 uuu 和 vvv 的无向边;
- 如果 op=1\text{op} = 1op=1,则表示查询 uuu 和 vvv 的连通性。
输出格式
一行包括一个整数表示答案。
样例
样例输入
3 6
1 1 0
0 0 1
1 0 1
1 1 2
0 2 1
1 2 1
样例输出
5
样例解释
答案串为 101101101。
数据范围与提示
n≤4000000,m≤8000000n\le 4000000,m\le 8000000n≤4000000,m≤8000000
#include <ctype.h>
#include <cstdio>
void read(int &x)
{
x=;bool f=;
char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=;ch=getchar();}
while(isdigit(ch)) {x=x*+ch-'';ch=getchar();}
x=f?(~x)+:x;
}
int n,m;
int fa[],cnt,ans[],l;
int find_(int x)
{
return fa[x]==x?x:fa[x]=find_(fa[x]);
}
int pd(int x,int y)
{
int a=find_(x),b=find_(y);
return a==b?:;
}
int quic(int m,int n)
{
long long r=,base=m%;
while(n)
{
if(n&)
r=r*base%;
base=base*base%;
n>>=;
}
return r;
}
int main()
{
read(n);read(m);
for(int i=;i<=n;i++) fa[i]=i;
for(int op,u,v;m--;)
{
read(op);
read(u);
read(v);
if(op==)
{
int fx=find_(u),fy=find_(v);
fa[fy]=fx;
}
else ans[++cnt]=pd(u,v);
}
int Ans=,left=,k=;
while(ans[left]==) left++;
for(int i=left;i<=cnt;i++) Ans=(Ans+quic(,k++)*ans[i])%;
printf("%d",Ans);
return ;
}
LibreOJ #109. 并查集的更多相关文章
- 【LOJ 109 并查集】 并查集
题目描述 这是一道模板题. 维护一个 n 点的无向图,支持: 加入一条连接 u 和 v 的无向边 查询 u 和 v 的连通性 由于本题数据较大,因此输出的时候采用特殊的输出方式:用 0 或 1 代表每 ...
- LOJ #109. 并查集
内存限制:256 MiB时间限制:2000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论 1 测试数据 题目描述 这是一道模板题. 维护一个 nnn 点 ...
- CF722C. Destroying Array[并查集 离线]
链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...
- CF469D Two Set (并查集)
Codeforces Round #268 (Div. 2)D Codeforces Round #268 (Div. 1)B CF468B D. Two Sets time limit per te ...
- Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads 并查集+优先队列
D. Fools and Foolproof Roads You must have heard all about the Foolland on your Geography lessons. ...
- Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包
A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A B C D 水 模拟 并查集 优先队列
A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
随机推荐
- robotframework:appium切换webview后,在第一个页面操作成功,跳转到第二个页面后,执行命令失败
问题: 在用robot写手机淘宝app的自动化时,打开手机淘宝后,点击天猫国际,跳转到天猫国际页面,天猫国际页面是H5, 需要切换到对应的webview,切换到webview后,点击美妆菜单,跳转到美 ...
- hadoop2 Ubuntu 下安装部署
搭建Hadoop环境( 我以hadoop 2.7.3 为例, 系统为 64bit Ubuntu14.04 ) hadoop 2.7.3 官网下载 , 选择自己要安装的版本.注意每个版本对应两个下载选项 ...
- [msf]CentOS VPS创建pptpd 并搭建msf
安装pptpd服务 vps下 下载 centos 6 一键安装包 wget --no-check-certificate https://raw.githubusercontent.com/teddy ...
- Not enough free disk space on disk '/boot'(转载)
转自:http://m.oschina.net/blog/277224 # 解决 出现此情况是因为你的boot分区是单独分区的,像我只给了100M,以前装ubuntu时没有出现,所以当出现这个提示时, ...
- php大小写转换函数
1.将字符串转换成小写 strtolower(): 该函数将传入的字符串参数所有的字符都转换成小写,并以小定形式放回这个字 符串.例: <?php $str = "I want T ...
- c++常见面试题30道
1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new对应free只会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,new ...
- 3dmax学习资料记录
max2015 官方文档 http://help.autodesk.com/view/3DSMAX/2015/CHS/?guid=GUID-D015E335-EFB3-43BF-AB27-C3CB09 ...
- UVA1328 Period【KMP/周期串/循环节】By cellur925
鲜有的在luogu发题解以及使用LaTex??? 就丢链接跑了.
- Hibernate中表与表之间的关联多对多,级联保存,级联删除
第一步:创建两个实体类:用户和角色实体类,多对多关系,并让两个实体类之间互相关联: 用户实体类: package com.yinfu.entity; import java.util.HashSet; ...
- C语言归并排序(合并排序)算法及代码
归并排序也称合并排序,其算法思想是将待排序序列分为两部分,依次对分得的两个部分再次使用归并排序,之后再对其进行合并.仅从算法思想上了解归并排序会觉得很抽象,接下来就以对序列A[0], A[l]…, A ...