Containing n points are given, without edges to FIG. M, the node numbered 1..n. There are four types of operations:
. 1, Link UV: between the node u and v is connected to a free edge.
2, Query uv: Discover u and v belong to the same connected component.
3, Count the number of the communication component of the current query graph.
4, Max asked, along with the current largest component.
Entry
First row: three integers n, meaning of m, n, m as described in the subject. Next m lines: two per row numbers u, v (1 <= u, v <= n), expressed article undirected edges between nodes u and v. The next line is an integer q, operands. The next line q: Each line is one of the four operations described in the title.
Export
The output for each query, the query for Query uv, if u and v are in the same together with component output yEs, otherwise the output nO.
Sample input 1
8 4 8 2 3 7 1 5 6 3 14 Count Max Link 3 2 Count Max Query 1 2 Link 6 8 Query 2 6 Count Max Link 1 3 Query 5 7 Count Max
Sample Output 1
4 3 3 5 nO yEs 3 5 yEs 2 7
prompt
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 const int maxn=0x3f3f3f; 5 int pa[maxn]; 6 int sz[maxn]; 7 int Max=0,cnt=0; 8 void makeset() 9 { 10 for(int i=1;i<=n;i++) 11 { 12 pa[i]=i; 13 sz[i]=1; 14 } 15 cnt=n; 16 Max=1; 17 } 18 int Find(int x) 19 { 20 int r=x; 21 while(pa[r]!=r) 22 r=pa[r]; 23 24 while(x!=r) 25 { 26 int y=pa[x]; 27 pa[x]=r; 28 x=y; 29 } 30 return r; 31 } 32 void Link(int x,int y) 33 { 34 int a=Find(x),b=Find(y); 35 if(a!=b) 36 { 37 pa[a]=b; 38 sz[b]+=sz[a]; 39 Max=max(Max,sz[b]); 40 cnt--; 41 } 42 } 43 bool Query(int x,int y) 44 { 45 return Find(x)==Find(y); 46 } 47 int main() 48 { 49 50 scanf("%d%d",&n,&m); 51 52 makeset(); 53 string s; 54 for(int i=1;i<=m;i++) 55 { 56 int x,y; 57 cin>>x>>y; 58 Link(x,y); 59 } 60 int t; 61 cin>>t; 62 while(t--) 63 { 64 cin>>s; 65 if(s=="Link") 66 { 67 int x,y; 68 scanf("%d%d",&x,&y); 69 Link(x,y); 70 } 71 if(s=="Query") 72 { 73 int x,y; 74 scanf("%d%d",&x,&y); 75 if(Query(x,y)==1) 76 printf("yEs\n"); 77 else 78 printf("nO\n"); 79 } 80 if(s=="Count") 81 { 82 printf("%d\n",cnt); 83 } 84 if(s=="Max") 85 { 86 printf("%d\n",Max); 87 } 88 } 89 return 0; 90 }