Meaning of the questions: http://codeforces.com/problemset/problem/456/E
Ideas:
Disjoint-set is to pit, starting a path compression.
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr strcat 13 #include <string> 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 #include <cassert> 21 #include <iomanip> 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 25 //****************** 26 clock_t __START,__END; 27 double __TOTALTIME; 28 void _MS(){__START=clock();} 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;} 30 //*********************** 31 #define rint register int 32 #define fo(a,b,c) for(rint a=b;a<=c;++a) 33 #define fr(a,b,c) for(rint a=b;a>=c;--a) 34 #define mem(a,b) memset(a,b,sizeof(a)) 35 #define pr printf 36 #define sc scanf 37 #define ls rt<<1 38 #define rs rt<<1|1 39 typedef pair<int,int> PII; 40 typedef vector<int> VI; 41 typedef unsigned long long ull; 42 typedef long long ll; 43 typedef double db; 44 const db E=2.718281828; 45 const db PI=acos(-1.0); 46 const ll INF=(1LL<<60); 47 const int inf=(1<<30); 48 const DB the ESP = 1E9 ; 49 const int MOD = ( int ) 1E9 + . 7 ; 50 const int N = ( int ) 1e6 + 10 ; 51 is 52 is int FA [N]; 53 is int Find ( int XA) { 54 is int XB = XA; // initial value assigned to B 55 the while (XA =! FA [XA]) { 56 is XA = FA [XA]; // find a ancestor node 57 is } 58 the while(! XB = XA) { // until A == b 59 int TEMP = FA [XB]; // provided as intermediate variable b is a parent node 60 FA [XB] = XA; // directly from the father b ancestor node is a node 61 is XB = TEMP; // b = b parent node 62 is } 63 is return XA; 64 } 65 int len [N], Lent [N]; 66 BOOL VIS [N]; 67 68 struct EDGE 69 { 70 int to, Next; 71 is } Edge [N]; 72 int Tot; 73 is int head[N]; 74 void Init(int n) 75 { 76 Tot=0; 77 for(int i=0;i<=n;++i) 78 head[i]=0; 79 } 80 void add(int from,int to) 81 { 82 ++Tot; 83 edge[Tot].to=to; 84 edge[Tot].next=head[from]; 85 head[from]=Tot; 86 }//for(int i=head[x];i;i=edge[i].next) 87 int TempNode,tot,TempC; 88 void dfs(int u,int fu,int c,int nn) 89 { 90 fa[u]=nn; 91 for(int i=head[u];i;i=edge[i].next) 92 { 93 int to=edge[i].to; 94 if(to!=fu) 95 dfs(to,u,c+1,nn); 96 } 97 if(c>TempC) 98 TempC=c,TempNode=u; 99 } 100 101 int main() 102 { 103 int n,m,q; 104 sc("%d%d%d",&n,&m,&q); 105 for(int i=1;i<=n;++i)fa[i]=i; 106 for(int i=1;i<=m;++i) 107 { 108 int u,v; 109 sc("%d%d",&u,&v); 110 add(u,v); 111 add(v,u); 112 } 113 for(int i=1;i<=n;++i) 114 { 115 if(fa[i]!=i)continue; 116 TempNode=TempC=tot=0; 117 dfs(i,-1,1,i); 118 TempC=0; 119 dfs(TempNode,-1,1,TempNode); 120 len[TempNode]=TempC-1; 121 } 122 for(int i=1;i<=q;++i) 123 { 124 int op,x,y; 125 sc("%d",&op); 126 if(op==1) 127 { 128 sc("%d",&x); 129 pr("%d\n",len[find(x)]); 130 } 131 else 132 { 133 sc("%d%d",&x,&y); 134 int fx=find(x); 135 int fy=find(y); 136 // if(fx>fy)swap(fx,fy); 137 if(fx==fy)continue; 138 len[fx]=max(max(len[fx],len[fy]),(len[fx]+1)/2+(len[fy]+1)/2+1); 139 fa[fy]=fx; 140 } 141 } 142 return 0; 143 } 144 145 /**************************************************************************************/