VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 stl应用
来源:互联网 发布:亚马逊 阿里云 offer 编辑:程序博客网 时间:2024/06/11 19:51
Even 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), wherex and 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.
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.
For each question of type 3 print "YES" or "NO" (without the quotes), depending on whether the corresponding people work in the same department.
8 63 2 51 2 53 2 52 4 72 1 23 1 7
NOYESYES
题意是,要求合并两个点到一个集合,成段合并到一个集合,由于,集合只会合并,不会分开,所以用并查集,就可以高效实现,主要是第二个操作,要求成段的合并,这里
我们可以用一个set map等等实现,只需要成段的合并就可以了。因为,每个点,只会进一次,最多也只出一次,总的复杂度为o(n * log(n))
#define N 200005int n,q,t,x,y,father[N];set<int> myset;set<int>::iterator it,it2;int findFa(int f){ if(father[f] == f) return f; else return father[f] = findFa(father[f]);}void Union(int a,int b){ int fa = findFa(a),fb = findFa(b); if(fa != fb){ father[fa] = fb; }}int main(){ while(S2(n,q)!=EOF) { for(int i = 1;i<=n;i++){ father[i] = i;myset.insert(i); } FI(q){ S(t);S2(x,y); if(t == 1){ Union(x,y); } else if(t == 2){ it = myset.upper_bound(x); while(it != myset.end() && *it <= y){ Union(x,*it); it2 = it++; myset.erase(it2); } } else if(t == 3){ if(findFa(x) == findFa(y)){ printf("YES\n"); } else { printf("NO\n"); } } } } return 0;}
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 stl应用
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 set 二分
- VK Cup 2015 - Finals, online mirror
- VK Cup 2015 - Finals, online mirror F. Clique in the Divisibility Graph 数论
- VK Cup 2015 - Finals, online mirror F - Clique in the Divisibility Graph dp
- Matching Names(Trie树)-(VK Cup 2015 - Finals, online mirror)
- CodeForces 566 D.Restructuring Company(并查集)
- CodeForces 566 D.Restructuring Company(并查集)
- 【并查集】Codeforces 566D Restructuring Company
- Bubble Cup 8 - Finals [Online Mirror]
- Bubble Cup 9 - Finals [Online Mirror] 题解
- Codeforces Problemset 212D(VK Cup 2012 Finals (unofficial online-version))
- Codeforces Bubble Cup 8 - Finals [Online Mirror] 解题报告
- Bubble Cup 8 - Finals [Online Mirror]C. Party
- VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) B. Work Group 树形dp
- VK Cup 2012 Finals E
- Codeforces 566D Restructuring Company
- CodeForces 566D Restructuring Company
- Python 实用命令
- 开发框架 dhroid源码
- MyBatis开发实战
- C# 装箱与拆箱
- POJ 1236 Network of Schools(强联通分量)
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 stl应用
- UI设计:如何设计移动应用的导航
- Highways(POJ_2485)
- 校园二手交易系统应用带服务端
- spark机制note
- linux初学(十七)之linux管道及重定向
- JDBC连接与增删查改的封装
- HDU 1232 畅通工程 __ 基础并查集
- C#自定义log机制