CodeForces 566D Restructuring Company (并查集+链表)
题意:给定 3 种操作,
第一种 1 u v 把 u 和 v 合并
第二种 2 l r 把 l - r 这一段区间合并
第三种 3 u v 判断 u 和 v 是不是在同一集合中。
析:很容易知道是用并查集来做,但是如果单纯的用并查集,肯定是要超时的,所以要用链表,如果合并了,就把链表指向,
这样就搞定了这个题。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int p[maxn], nxt[maxn]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } int main(){
scanf("%d %d", &n, &m);
for(int i = 0; i <= n; ++i) p[i] = i, nxt[i] = i + 1;
while(m--){
int op, u, v;
scanf("%d %d %d", &op, &u, &v);
if(op == 1){
int x = Find(u);
int y = Find(v);
if(x != y) p[y] = x;
}
else if(op == 2){
int x = Find(v);
while(true){
if(u > v) break;
int y = Find(u);
if(x != y) p[y] = x;
int t = u;
u = nxt[u];
nxt[t] = v+1;
}
}
else printf("%s\n", Find(u) == Find(v) ? "YES" : "NO");
}
return 0;
}
CodeForces 566D Restructuring Company (并查集+链表)的更多相关文章
- CodeForces - 566D Restructuring Company 并查集的区间合并
Restructuring Company Even the most successful company can go through a crisis period when you have ...
- codeforces 566D D. Restructuring Company(并查集)
题目链接: D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集
D. Restructuring Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- D. Restructuring Company 并查集 + 维护一个区间技巧
http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces 556D Restructuring Company
传送门 D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Gym 100463E Spies 并查集
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...
- 【CF878C】Tournament set+并查集+链表
[CF878C]Tournament 题意:有k个项目,n个运动员,第i个运动员的第j个项目的能力值为aij.一场比赛可以通过如下方式进行: 每次选出2个人和一个项目,该项目能力值高者获胜,败者被淘汰 ...
- Codeforces 859E Desk Disorder 并查集找环,乘法原理
题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...
随机推荐
- PKUSC2018 Slay The Spire
有攻击牌和强化牌各 $n$ 张,强化牌可以让之后所有攻击牌攻击力乘一个大于 $1$ 的系数,攻击牌可以造成伤害 求所有“抽出 $m$ 张然后打 $k$ 张”能造成的伤害之和 $k,m,2n \leq ...
- bzoj 4710 分特产
有 $n$ 个人,$m$ 种物品,每种物品有 $a_i$ 个,求每个人至少分到一个的方案数 $n,m,a_i \leq 2000$ sol: 比上一个题简单一点 还是考虑容斥 每个人至少分到一个 = ...
- java-07接口与继承
1.动手实验:继承条件下的构造方法调用 代码: package demo; class Grandparent{ public Grandparent(){ System.out.println(&q ...
- 大整数乘法(Comba 乘法 (Comba Multiplication)原理)
Comba 乘法以(在密码学方面)不太出名的 Paul G. Comba 得名.上面的笔算乘法,虽然比较简单, 但是有个很大的问题:在 O(n^2) 的复杂度上进行计算和向上传递进位,看看前面的那个竖 ...
- shell 实现闰年的判断
#!/bin/shecho "please input the year"read year let "n1=$year % 4"let "n2=$y ...
- 基于spring及zookeeper的dubbo工程搭建
一.生产者搭建 新建一个maven工程,勾选Create a simple project Packaging方式选择jar包的方式. 修改pom.xml文件: <project xmlns=& ...
- BZOJ3489:A simple rmq problem
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...
- unix下网络编程之I/O复用(五)
前言 本章节是用基本的Linux/Unix基本函数加上select调用编写一个完整的服务器和客户端例子,可在Linux(ubuntu)和Unix(freebsd)上运行,客户端和服务端的功能如下: 客 ...
- 有关UCOS_II在LPC1768上的应用
https://www.cnblogs.com/chungshu/archive/2012/12/14/2818380.html
- Java-API:java.util.UUID
ylbtech-Java-API:java.util.UUID 1.返回顶部 2.返回顶部 3.返回顶部 4. 百科返回顶部 5.返回顶部 0. https://docs.oracle ...