题意:http://codeforces.com/problemset/problem/456/E
思路:
并查集被坑到了,得用路径压缩。
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 ESP=1e-9; 49 const int mod=(int)1e9+7; 50 const int N=(int)1e6+10; 51 52 int fa[N]; 53 int find(int xa){ 54 int xb=xa; //把初始值赋给b 55 while(xa!=fa[xa]){ 56 xa=fa[xa]; //找到a的祖先节点 57 } 58 while(xb!=xa){ //直到b==a为止 59 int temp=fa[xb]; //设一个中间变量为b的父亲节点 60 fa[xb]=xa; //直接让b的父亲节点为a的祖先节点 61 xb=temp; //b等于b的父亲节点 62 } 63 return xa; 64 } 65 int len[N],lent[N]; 66 bool vis[N]; 67 68 struct EDGE 69 { 70 int to,next; 71 }edge[N]; 72 int Tot; 73 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 ",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 /**************************************************************************************/