codeforces 566D D. Restructuring Company(并查集)
2016-04-03 20:21
549 查看
题目链接:D. Restructuring Companytime limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputEven the most successful company can go through a crisis period when you have to make a hard decision — to restructure, discard and merge departments, fire employees and do other unpleasant stuff. Let's consider the following model of a company.There are n people working for the Large Software Company. Each person belongs to some department. Initially, each person works on his own project in his own department (thus, each company initially consists of n departments, one person in each).However, harsh times have come to the company and the management had to hire a crisis manager who would rebuild the working process in order to boost efficiency. Let's use team(person) to represent a team where person person works. A crisis manager can make decisions of two types:Merge departments team(x) and team(y) into one large department containing all the employees of team(x) and team(y), where xand y (1 ≤ x, y ≤ n) — are numbers of two of some company employees. If team(x) matches team(y), then nothing happens.Merge departments team(x), team(x + 1), ..., team(y), where x and y (1 ≤ x ≤ y ≤ n) — the numbers of some two employees of the company.At that the crisis manager can sometimes wonder whether employees x and y (1 ≤ x, y ≤ n) work at the same department.Help the crisis manager and answer all of his queries.Input
The first line of the input contains two integers n and q (1 ≤ n ≤ 200 000, 1 ≤ q ≤ 500 000) — the number of the employees of the company and the number of queries the crisis manager has.Next q lines contain the queries of the crisis manager. Each query looks like type x y, where
. If type = 1 or type = 2, then the query represents the decision of a crisis manager about merging departments of the first and second types respectively. Iftype = 3, then your task is to determine whether employees x and y work at the same department. Note that x can be equal to y in the query of any type.Output
For each question of type 3 print "YES" or "NO" (without the quotes), depending on whether the corresponding people work in the same department.Examplesinput
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputEven the most successful company can go through a crisis period when you have to make a hard decision — to restructure, discard and merge departments, fire employees and do other unpleasant stuff. Let's consider the following model of a company.There are n people working for the Large Software Company. Each person belongs to some department. Initially, each person works on his own project in his own department (thus, each company initially consists of n departments, one person in each).However, harsh times have come to the company and the management had to hire a crisis manager who would rebuild the working process in order to boost efficiency. Let's use team(person) to represent a team where person person works. A crisis manager can make decisions of two types:Merge departments team(x) and team(y) into one large department containing all the employees of team(x) and team(y), where xand y (1 ≤ x, y ≤ n) — are numbers of two of some company employees. If team(x) matches team(y), then nothing happens.Merge departments team(x), team(x + 1), ..., team(y), where x and y (1 ≤ x ≤ y ≤ n) — the numbers of some two employees of the company.At that the crisis manager can sometimes wonder whether employees x and y (1 ≤ x, y ≤ n) work at the same department.Help the crisis manager and answer all of his queries.Input
The first line of the input contains two integers n and q (1 ≤ n ≤ 200 000, 1 ≤ q ≤ 500 000) — the number of the employees of the company and the number of queries the crisis manager has.Next q lines contain the queries of the crisis manager. Each query looks like type x y, where
. If type = 1 or type = 2, then the query represents the decision of a crisis manager about merging departments of the first and second types respectively. Iftype = 3, then your task is to determine whether employees x and y work at the same department. Note that x can be equal to y in the query of any type.Output
For each question of type 3 print "YES" or "NO" (without the quotes), depending on whether the corresponding people work in the same department.Examplesinput
8 6 3 2 5 1 2 5 3 2 5 2 4 7 2 1 2 3 1 7output
NO YES YES 题意: 三种操作,type==1,把x,y合并到一个集合里面;type==2,把x,x+1,x+2...y合并到一个集合里;type==3,query x和y是否在一个集合里; 思路: 很显然是并查集,只是type==2是对应的是区间操作,但发现如果是在一个集合里了还要操作就是浪费,可以把已经在一个区间的压缩,用一个pre数组,pre[i]表示数i的前面和i不是一个集合的与i最近的数;这样可以一次合并后下一次直接跳过中间多余的; AC代码:
/*
2014300227 | 566D - 25 | GNU C++11 | Accepted | 249 ms | 3732 KB |
*/ #include <bits/stdc++.h> using namespace std; const int N=2e5+4; typedef long long ll; int n,p ,pre ,q,type,x,y; int findset(int x) { if(x == p[x])return x; return p[x] = findset(p[x]); } int main() { scanf("%d%d",&n,&q); for(int i = 1;i <= n;i++) { p[i] = i; pre[i] = i-1; } while(q--) { scanf("%d%d%d",&type,&x,&y); if(type == 3) { if(findset(x) == findset(y))printf("YES\n"); else printf("NO\n"); } else if(type == 1) { int fx = findset(x),fy = findset(y); p[fx] = fy; } else { int r; for(int i = y;i >= x;i = r) { r = pre[i]; if(r<x)break; p[findset(r)] = p[findset(i)]; pre[i] = pre[r]; } } } return 0; }
相关文章推荐
- First Preview of Android N: Developer APIs & Tools
- TimesTen中如何标识客户端连接
- 第五周学习进度
- 剑指offer-面试题27:二叉搜索树与双向链
- 大话设计模式—命令模式
- Java对象的四种引用类型
- 20145218 《Java程序设计》第五周学习总结
- CentOS系统使用配置文件修改IP地址详细教程
- Percona Toolkit 学习(四)(heartbeat, index-usage,ioprofile,killmextmysql-summary)
- hdu1181 变形课 BFS 判断连通性
- 自定义View(一)
- 第五周作业
- 引导页的使用
- 研究生小论文的投稿技巧
- 【Unity Shaders】使用CgInclude让你的Shader模块化——创建CgInclude文件存储光照模型
- 利用NLTK在Python下进行自然语言处理
- 静态工厂方法和实例工厂方法区别
- Multiple APK Support in Android Market
- 常用控件
- 包装类