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个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...
随机推荐
- Node.Js and Mongoose
Mongoose官方API,我做完之后整理出来的心得. ONE· Getting Started First be sure you have MongoDB and Node.js installe ...
- Python的交互模式和命令行模式
Pyhton的交互模式 在终端输入Python3命令就会进入家Python的交互模式,在交互模式下,输入一行代码,回车,就会执行这行代码. Python的命令行模式 在终端输入Python3 1.py ...
- git之clone
git clone 命令参数: usage: git clone [options] [--] <repo> [<dir>] -v, --verbose be more ver ...
- Poj 2488 A Knight's Journey(搜索)
Background The knight is getting bored of seeing the same black and white squares again and again an ...
- 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历
二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则 ...
- VxVM如何扩展和收缩卷及文件系统
1. 同时扩展卷和文件系统 先用vxassist命令检查DG可用空间 [root@rhelnode1 ~]# vxassist -g testdg maxsize Maximum volume siz ...
- enable or disable Oracle block change tracking
Oracle的block change tracking用于记录上次备份以来改变过的block信息,因此打开block change tracking可以大大加快增量备份的速度. 1. Enable ...
- spring bean属性及子元素使用总结
spring bean属性及子元素使用总结 2016-08-03 00:00 97人阅读 评论(0) 收藏 举报 分类: Spring&SpringMVC(17) 版权声明:本文为博主原创 ...
- 下载Django
Django下载教程以及学习教程https://code.ziqiangxuetang.com/django/django-queryset-api.html 或者直接搜索自强学堂
- 一些API的用法
//1.init初始化 NSString * str1 = [[NSString alloc] init]; NSLog(@"str1 = %@",str1); //2.initW ...