www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當前位置:首頁 > 芯聞號 > 充電吧
[導(dǎo)讀]【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é)點的集合關(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;
}




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

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫毥谦F公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風險,如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應(yīng)對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅持高質(zhì)量發(fā)展策略,塑強核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉