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 ...
随机推荐
- object_funs.py
#__init__ 构造方法,双下划线 #__del__ 析构方法,在对象就要被垃圾回收前调用.但发生调用 #的具体时间是不可知的.所以建议尽量避免使用__del__ print('-------ex ...
- exception.py
try: raise Exception except Exception as e: print(e) try: raise Exception('comment') except Exceptio ...
- 【前端】CentOS 7 系列教程之二: 安装 git 最新版
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_2.html 这一篇我们来安装git高版本. 卸载yum安装的旧版本 yum remove git 安装 ...
- bzoj2560串珠子——子集DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2560 转载: 很明显的状压dp 一开始写的dp可能会出现重复统计的情况 而且难以去重 假设 ...
- CS231n 2016 通关 第二章-KNN 作业分析
KNN作业要求: 1.掌握KNN算法原理 2.实现具体K值的KNN算法 3.实现对K值的交叉验证 1.KNN原理见上一小节 2.实现KNN 过程分两步: 1.计算测试集与训练集的距离 2.通过比较la ...
- Table View Programming Guide for iOS---(三)----Overview of the Table View API
Overview of the Table View API 表格视图API概述 The table view programming interface includes several UIKit ...
- 2.27 MapReduce Shuffle过程如何在Job中进行设置
一.shuffle过程 总的来说: *分区 partitioner *排序 sort *copy (用户无法干涉) 拷贝 *分组 group 可设置 *压缩 compress *combiner ma ...
- 标准WPS框架下的空间信息处理服务部署方法
笔者第一篇博客里面曾介绍过将专题图制作功能发布为WPS,但随着后面的研究,才发现那时候发布的不是真正WPS框架下的服务,而只是通过Servlet将其发布为可调用的网络服务,所以今天再具体介绍一下真正的 ...
- E20170510-hm
prototype n. 原型,雏形,蓝本; omit (omitted) vt. 省略; 遗漏; autonomous adj. 自治的; 有自主权的; fold ...
- 862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...