【打CF,學算法——四星級】CodeForces 455C Civilization (【詳解】并查集+樹的直徑)
【CF簡介】
提交鏈接:CF 455C
題面:
C. Civilization time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output
Andrew plays a game called "Civilization". Dima helps him.
The game has n cities and m bidirectional roads. The cities are numbered from 1 to n. Between any pair of cities there either is a single (unique) path, or there is no path at all. A path is such a sequence of distinct citiesv1,?v2,?...,?vk, that there is a road between any contiguous citiesvi andvi?+?1 (1?≤?i?<?k). The length of the described path equals to(k?-?1). We assume that two cities lie in the same region if and only if, there is a path connecting these two cities.
During the game events of two types take place:
Andrew asks Dima about the length of the longest path in the region where cityx lies. Andrew asks Dima to merge the region where city x lies with the region where cityy lies. If the cities lie in the same region, then no merging is needed. Otherwise, you need to merge the regions as follows: choose a city from the first region, a city from the second region and connect them by a road so as to minimize the length of the longest path in the resulting region. If there are multiple ways to do so, you are allowed to choose any of them.
Dima finds it hard to execute Andrew's queries, so he asks you to help him. Help Dima.
Input
The first line contains three integers n,m,q (1?≤?n?≤?3·105;0?≤?m?<?n; 1?≤?q?≤?3·105) — the number of cities, the number of the roads we already have and the number of queries, correspondingly.
Each of the following m lines contains two integers,ai andbi (ai?≠?bi;1?≤?ai,?bi?≤?n). These numbers represent the road between citiesai andbi. There can be at most one road between two cities.
Each of the following q lines contains one of the two events in the following format:
1 xi. It is the request Andrew gives to Dima to find the length of the maximum path in the region that contains cityxi (1?≤?xi?≤?n).2 xiyi. It is the request Andrew gives to Dima to merge the region that contains cityxi and the region that contains cityyi (1?≤?xi,?yi?≤?n). Note, thatxi can be equal toyi. Output
For each event of the first type print the answer on a separate line.
Examples Input
6?0?6 2?1?2 2?3?4 2?5?6 2?3?2 2?5?3 1?1
Output
4
題意:
??? 題目給出一張圖,圖中原有n個點,m條邊,且m<n,且兩個點之間沒有重邊,且兩點之間最多只有一條路徑。后續(xù)有兩種操作,操作一,詢問某節(jié)點所在的聯(lián)通塊上最長的鏈的長度。操作二,將兩個聯(lián)通塊相連,要求相連后形成的聯(lián)通塊的最大長度盡量小。(如果兩點原就在一個聯(lián)通塊,就不需要連接了)
解題:
??? 第一次寫求樹的直徑,其實也不難,首先在一個聯(lián)通塊上隨意找一個出發(fā),求得該點可以到的最遠點的位置,并從最遠點反向再進行dfs一遍,此時的最大值就為樹的直徑(樹的直徑指的是樹上最長的鏈的長度)。因為,任意從樹上某一點出發(fā),能到的最遠點一定在樹的直徑上,故可以反向dfs,求得最優(yōu)值。
??? 可以用并查集的方式來維護節(jié)點的集合關(guān)系。當合并兩個集合時,為了使合并后得到的樹的直徑最小,會采取在兩顆樹的直徑的中間點位置相連,這樣就最小化了最長鏈的優(yōu)勢,從而滿足了題意,將新得到的聯(lián)通塊合并(更新fa數(shù)組),同時更新代表節(jié)點對應(yīng)的val值。
相關(guān)知識點:
??????
并查集入門
代碼:
#include#include#include#include#include#include#include#define?maxn?300005 using?namespace?std; //存儲邊 struct?edge { int?u,v,nxt; }store[maxn*2]; //cur為當前dfs的起點,max_len最大長度,p最遠點位置 int?head[maxn],cnt=0,fa[maxn],val[maxn],cur,max_len,p; //訪問標記數(shù)組 bool?vis[maxn]; //添加雙向邊 void?addedge(int?u,int?v) { store[cnt].nxt=head[u]; head[u]=cnt; store[cnt].u=u; store[cnt++].v=v; ????store[cnt].nxt=head[v]; head[v]=cnt; store[cnt].v=u; store[cnt++].u=v; } //dfs求樹的直徑,pre是樹的前驅(qū)節(jié)點,避免回去 //flag在第一次時不標記,第二次反向時標記 void?dfs(int?x,int?pre,int?step,int?flag) { vis[x]=flag; fa[x]=cur; ????val[x]=step; if(val[x]>max_len) { ???????max_len=val[x]; ???p=x; } for(int?i=head[x];~i;i=store[i].nxt) { if(store[i].v!=pre&&!vis[store[i].v]) dfs(store[i].v,store[i].u,step+1,flag); } } //找出集合代表元素,同時進行路徑壓縮 int?Find(int?x) { ??return?fa[x]!=x?fa[x]=Find(fa[x]):x;? } //合并兩個集合,并更新最長鏈長度 void?Union(int?a,int?b) { ???int?tmp,ta,tb; ???ta=Find(a); ???tb=Find(b); ???tmp=max((val[ta]+1)/2+(val[tb]+1)/2+1,max(val[ta],val[tb])); ???fa[ta]=tb; ???val[tb]=tmp; } int?main() { ????int?n,m,q,a,b,tmp,op,ta,tb; memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); cnt=0; scanf("%d%d%d",&n,&m,&q); //構(gòu)圖 for(int?i=0;i<m;i++) { scanf("%d%d",&a,&b); addedge(a,b); } //初始化 for(int?i=1;i<=n;i++) fa[i]=i; for(int?i=1;i<=n;i++) { if(!vis[i]) { cur=i; max_len=0; p=0; dfs(i,-1,0,0); //可能該點沒有邊,需注意 if(p!=0) ????????????dfs(p,-1,0,1); val[fa[p]]=max_len; } } //操作 for(int?i=0;i<q;i++) { ???????scanf("%d",&op); ???if(op==1) ???{ ???scanf("%d",&a); ???ta=Find(a); ???printf("%dn",val[ta]); ???} ???else ???{ ???scanf("%d%d",&a,&b); ???ta=Find(a); ???tb=Find(b); ???if(ta==tb) ???continue; ???else ???Union(a,b); ???} } return?0; }