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 ...
随机推荐
- org.apache.hadoop.hbase.NotServingRegionException: Region is not online 错误
当遇到如下错误的时候 可能以为是regionserver 挂掉或者其他原因导致连接不上regionserver 但后面提示了Hbase 表statistic_login 具体信息 Thu Jan 1 ...
- AM335x Android eMMC mkmmc-android.sh hacking
# AM335x Android eMMC mkmmc-android.sh hacking # # . 有空解读一下android的分区文件. # . 代码来源:https://github.com ...
- 【AMPPZ 2014】 The Captain
[题目链接] 点击打开链接 [算法] 按x轴排序,将相邻点连边 按y轴排序,将相邻点连边 然后对这个图跑最短路就可以了,笔者用的是dijkstra算法 [代码] #include<bits/st ...
- 使用Navicat连接MySQL出现1251错误
问题:navicat连接mysql时报错:1251-Client does not support authentication protocol requested by server; consi ...
- 读取文件中的每行数据,并且存入到list中
有一个txt文件,每行都有数据,将每行的数据转换成list列表 例如: 5,6,7,8,1 9,1,3,4 如下实现: f = open('test1.txt','r') for i in f.rea ...
- 深入理解js里面的this
闲聊两句(可以忽略): 毕业有半年了,时间还过得真快,不过还好,感觉自己相对于刚毕业那会确实成长了很多:好久没有打游戏了(自己决心要戒掉的),消磨时光的时候就看看电影或者追追电视剧,再无聊就洗洗衣服. ...
- C++ 指针p1 p2,p1-p2 与*p1-*p2的区别
p1-p2 指 指针的地址值相减,计算两个指针之间的偏移量 *p1-*p2 指 指针指向的内存地址里面存的数值相减
- shell初级-----数据呈现方式
输入与输出 Linux系统将每个对象当作文件处理,这包括输入和输出进程.Linux用文件描述符来标识每个文件对象.文件描述符是一个非负整数,可以唯一标识会话中打开的文件.每个进程一次多可以有九个文件描 ...
- hdoj5832【模拟】
主要还是一个10001的倍数的问题: 队友的思路: 01 1个数*10001,最后四位是这个数的后四位 比如 521456 10001 521456 521456 9 5215081456 从后面fo ...
- hdoj5327【前缀和思想】
题意: 找给定区间的美丽数,美丽数的意思就是这个数每个位上的数都是唯一的. 思路: 前缀和的思想. 感想: 就是你当前位置代表某个特性的前面的所有和(瞎比比的,说了下感觉).前提是你必须找到这样的特性 ...