【libreOJ模板】并查集(输入挂,取模与find优化)
1.了解了各种输入挂性orz,找到了一个合适的
2.find用while写能快一倍,并且能被数据卡掉
3.取模只能快十几毫秒,但也能被数据卡掉
取模find双优化是1997mm过的
再加一个性价比较高的输入挂是438mm 23333
#include <cstdio>
#include <cmath>
#include <complex>
#include <algorithm>
#include <iostream>
#include<string.h>
#include<vector>
#include<ctime>
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mmm(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
#define N 2333333
const ll M= ;
const int maxn = 4e6 + ;
int f[maxn];
using namespace std; template<typename T>inline void add_(T &A, int B, ll MOD = M) { A += B; (A >= MOD) && (A -= MOD); }
template<typename T>inline void mul_(T &A, ll B, ll MOD = M) { A = (A*B) % MOD; }
namespace IO
{
const int MAXL = << ;
char buf[MAXL], *S, *T, ch; inline char Getch()
{
if (S == T) T = (S = buf) + fread(buf, , MAXL, stdin);
return S == T ? EOF : *S++;
} inline void Read(int &x)
{
x = ;
while (!isdigit(ch = Getch()));
do { x = x * + (ch ^ ''); } while (isdigit(ch = Getch()));
}
}
using namespace IO; int find(int x) {
while (f[x] ^ x)x = f[x] = f[f[x]]; return x;
}
void un(int x, int y) {
int xx = find(x), yy = find(y);
if(xx^yy)f[xx] = yy;
}
int main() {
int n, m;
cin >> n >> m;
rep(i, , n)f[i] = i;
int op, a, b;
ll ans = ;
rep(i, , m) {
Read(op); Read(a); Read(b);
if(!op)un(a, b);
else {
add_(ans, ans + (find(a) == find(b)));
}
}
cout << ans << endl;
cin >> n;
}
【libreOJ模板】并查集(输入挂,取模与find优化)的更多相关文章
- HDU 3306 Another kind of Fibonacci(矩阵+ll超时必须用int&输入必须取模&M必须是int类型)
Another kind of Fibonacci [题目链接]Another kind of Fibonacci [题目类型]矩阵+ll超时必须用int&输入必须取模&M必须是int ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- HDU 1232 畅通工程(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1232 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出 ...
- luoguP3367 [模板]并查集
题目链接:https://www.luogu.org/problemnew/show/P3367 思路: 今天学了新算法——并查集,本题是简单的并查集题的模板. 核心思想是“递归+压缩路径”. 并查集 ...
- Luogu [P3367] 模板 并查集
[模板]并查集 题目详见:[[P3367][模板]并查集] (https://www.luogu.org/problemnew/show/P3367) 这是一道裸的并查集题目(要不然叫模板呢) 废话不 ...
- LibreOJ #109. 并查集
题目描述 这是一道模板题. 维护一个 nnn 点的无向图,支持: 加入一条连接 uuu 和 vvv 的无向边 查询 uuu 和 vvv 的连通性 由于本题数据较大,因此输出的时候采用特殊的输出方式:用 ...
- 洛谷P4723 【模板】线性递推(多项式取模 线性代数)
题意 题目链接 Sol Orz shadowice 注意,下面的代码自带O(随时TLE)倍大常数.. #include<bits/stdc++.h> #define Pair pair&l ...
- 【畅通工程 HDU - 1232 】【并查集模板】
并查集讲解和模板 有一个博文对此分析的很透彻,附链接 为避免原链接失效,现摘录如下: 为了解释并查集的原理,我将举一个更有爱的例子. 话说江湖上散落着各式各样的大侠,有上千个之多.他们没有什么正当职业 ...
- 【bzoj1370】[Baltic2003]Gang团伙 并查集
题目描述 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: 所有是朋友的人组成一个团伙.告诉你关于这n个人的m条信息, ...
随机推荐
- R绘制3D散点图
目前主要使用ggplot2做一些数据可视化的分析,但是ggplot2有个很大的缺陷是不支持3D作图,所以需要查找其他替代方案. 下面找到的两个替代方案不错,亲测可行,记录于此. 交互3D librar ...
- Python访问MongoDB,并且转换成Dataframe
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/7/13 11:10 # @Author : baoshan # @Site ...
- windowsclient开发--为你的client进行国际化
之前博客讲过函数: GetUserDefaultUILanguage Returns the language identifier for the user UI language for the ...
- hdoj:2022
#include <iostream> #include <string> using namespace std; int main() { int m, n; int x, ...
- Ubuntu 中的VI和vim
转载出处:http://blog.csdn.net/xiajun07061225/article/details/7039413 或功能键[Home]:移动到这一行的最前面字符处. $或功能键[End ...
- jQuery移除或禁用html元素点击事件常用方法小结
移除或禁用html元素的点击事件可以通过css实现也可以通过js或jQuery实现. 一.CSS方法 .disabled { pointer-events: none; } 二.jQuery方法 方法 ...
- Spark学习笔记——安装和WordCount
1.去清华的镜像站点下载文件spark-2.1.0-bin-without-hadoop.tgz,不要下spark-2.1.0-bin-hadoop2.7.tgz 2.把文件解压到/usr/local ...
- CentOS7 yum方式安装MariaDB 10.2.13-1
注:以下步骤都是以root身份运行. 一.建立mariadb.repo 1,编辑新文件,命令:vim /etc/yum.repos.d/mariadb.repo 2,输入如下内容,保存退出 [mar ...
- Git 删除提交记录
.Checkout git checkout --orphan latest_branch 2. Add all the files git add -A 3. Commit the changes ...
- 解决java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone
使用spring boot整合MySQL时一直报 java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecog ...