【CF566#D】 Restructuring Company (并查集---合并区间操作)
題干:
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:
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. If?type?=?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.
Examples
Input
8 6 3 2 5 1 2 5 3 2 5 2 4 7 2 1 2 3 1 7Output
NO YES YES題目大意:
給出n個點q個操作。“1” 代表合并u v兩個點,“2” 代表合并u,v及其中間的所有點,“3”代表查詢這兩個點是否在一個區間內。
解題報告:
并查集的區間合并操作。
錯誤代碼:()
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int MAX = 200000 + 5; int n,m; int f[MAX];//關系集合 int uu[MAX];//uu[i]記錄的是下一個不在當前集合區間內的點 int getf(int v) {return f[v] == v? v:f[v]=getf(f[v]); } void merge(int u,int v) {int t1,t2;t1=getf(u);t2=getf(v);if(t1!=t2){f[t2]=t1;} } int join(int u,int v) {int t1,t2;t1=getf(u);t2=getf(v);if(t1==t2)return 0;else return 1; } void init() {for(int i = 1; i<=n; i++) {f[i]=i;uu[i]=i+1;//u[i]記錄的是下一個不在當前集合區間內的點 } } int main() {int op,u,v,tmp; while(~scanf("%d%d",&n,&m) ) {int sum=0;init();while(m--) {scanf("%d %d %d",&op,&u,&v); if(op == 1) {merge(u,v);}else if(op == 2) {for(int i = u; i<v; i=tmp) {tmp = uu[i];merge(i,i+1);uu[i]=uu[v];}}else {if(getf(u) == getf(v) ) {printf("YES\n");}else printf("NO\n");}}} return 0; }ac代碼:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int MAX = 200000 + 5; int n,m; int f[MAX];//關系集合 int uu[MAX];//uu[i]記錄的是下一個不在當前集合區間內的點 int getf(int v) {if(f[v]!=v)f[v]=getf(f[v]);return f[v]; } void merge(int u,int v) {int t1,t2;t1=getf(u);t2=getf(v);if(t1!=t2){f[t2]=t1;} } int join(int u,int v) {int t1,t2;t1=getf(u);t2=getf(v);if(t1==t2)return 0;else return 1; } void init() {for(int i = 1; i<=n; i++) {f[i]=i;uu[i]=i+1;//u[i]記錄的是下一個不在當前集合區間內的點 } } int main() {int op,u,v,tmp; while(~scanf("%d%d",&n,&m) ) {int sum=0;init();while(m--) {scanf("%d %d %d",&op,&u,&v); if(op == 1) {merge(u,v);}else if(op == 2) {for(int i = u + 1; i<=v; i=tmp) {tmp = uu[i];merge(i-1,i);uu[i]=uu[v];}}else {if(getf(u) == getf(v) ) {printf("YES\n");}else printf("NO\n");}}} return 0; }總結:
? ? ?1.看清輸出YES 還是Yes ? !
?
總結
以上是生活随笔為你收集整理的【CF566#D】 Restructuring Company (并查集---合并区间操作)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国汽车工业“活化石” 新一代北汽BJ2
- 下一篇: 【51nod - 前缀异或】 对前缀和的