日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當前位置:首頁 > > 充電吧
[導讀]【CF簡介】提交鏈接:CF 455C題面:C. Civilization time limit per test 1 second memory limit per test 256 megaby

【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é)點的集合關系。當合并兩個集合時,為了使合并后得到的樹的直徑最小,會采取在兩顆樹的直徑的中間點位置相連,這樣就最小化了最長鏈的優(yōu)勢,從而滿足了題意,將新得到的聯(lián)通塊合并(更新fa數(shù)組),同時更新代表節(jié)點對應的val值。

相關知識點:

?????? 并查集入門

代碼:


#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);
	//構圖
	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;
}




本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關鍵字: 驅(qū)動電源

在工業(yè)自動化蓬勃發(fā)展的當下,工業(yè)電機作為核心動力設備,其驅(qū)動電源的性能直接關系到整個系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動勢抑制與過流保護是驅(qū)動電源設計中至關重要的兩個環(huán)節(jié),集成化方案的設計成為提升電機驅(qū)動性能的關鍵。

關鍵字: 工業(yè)電機 驅(qū)動電源

LED 驅(qū)動電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個照明設備的使用壽命。然而,在實際應用中,LED 驅(qū)動電源易損壞的問題卻十分常見,不僅增加了維護成本,還影響了用戶體驗。要解決這一問題,需從設計、生...

關鍵字: 驅(qū)動電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動電源的公式,電感內(nèi)電流波動大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關鍵字: LED 設計 驅(qū)動電源

電動汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動汽車的核心技術之一是電機驅(qū)動控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機驅(qū)動系統(tǒng)中的關鍵元件,其性能直接影響到電動汽車的動力性能和...

關鍵字: 電動汽車 新能源 驅(qū)動電源

在現(xiàn)代城市建設中,街道及停車場照明作為基礎設施的重要組成部分,其質(zhì)量和效率直接關系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進步,高亮度白光發(fā)光二極管(LED)因其獨特的優(yōu)勢逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關鍵字: 發(fā)光二極管 驅(qū)動電源 LED

LED通用照明設計工程師會遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關鍵字: LED 驅(qū)動電源 功率因數(shù)校正

在LED照明技術日益普及的今天,LED驅(qū)動電源的電磁干擾(EMI)問題成為了一個不可忽視的挑戰(zhàn)。電磁干擾不僅會影響LED燈具的正常工作,還可能對周圍電子設備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來解決L...

關鍵字: LED照明技術 電磁干擾 驅(qū)動電源

開關電源具有效率高的特性,而且開關電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機重量也有所下降,所以,現(xiàn)在的LED驅(qū)動電源

關鍵字: LED 驅(qū)動電源 開關電源

LED驅(qū)動電源是把電源供應轉(zhuǎn)換為特定的電壓電流以驅(qū)動LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關鍵字: LED 隧道燈 驅(qū)動電源
關閉