日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces Round #260 (Div. 1) C. Civilization(dfs+并查集)

發布時間:2024/4/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces Round #260 (Div. 1) C. Civilization(dfs+并查集) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 cities v1,?v2,?…,?vk, that there is a road between any contiguous cities vi and vi?+?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 city x lies.
Andrew asks Dima to merge the region where city x lies with the region where city y 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 and bi (ai?≠?bi; 1?≤?ai,?bi?≤?n). These numbers represent the road between cities ai and bi. 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 city xi (1?≤?xi?≤?n).
2 xi yi. It is the request Andrew gives to Dima to merge the region that contains city xi and the region that contains city yi (1?≤?xi,?yi?≤?n). Note, that xi can be equal to yi.
Output
For each event of the first type print the answer on a separate line.

Examples
inputCopy
6 0 6
2 1 2
2 3 4
2 5 6
2 3 2
2 5 3
1 1
outputCopy
4

題目大意:給定N,M和Q,N表示有N個城市,M條已經修好的路,修好的路是不能改變的,然后是Q次操作,操作分為兩種,一種是查詢城市x所在的聯通集合中,最長的路為多長。二是連接兩個聯通集合,采用聯通之后最長路最短的方案。
解析:
對于建好的圖
進行兩次dfs,第一次dfs得到最大邊的一個端點,
然后以這個端點為根節點,進行第二次dfs,將這個連通塊中的所有點的pre設為這個點.并更新這個連通塊最長路的值
Q次操作,用并查集維護(在這里請注意并查集的寫法,比最基本的寫法要效率要高),聯通的時候要采用最長路徑最短的方案,所以s的轉移方程變為s = max(s, (s+1)/2 + (s0+1)/2 + 1),這個公式很好理解就不多講了.

#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define inf 2099999999 #define mod 1000000007 #define rep(i,a,b) for(int i=a;i<=b;i++) #define rep1(i,a,b) for(int i=a;i>=b;i--) const int N=3e5+100; int arr[N]; int pre[N]; int dis[N]; vector<int>G[N]; int ans,rec; int fi(int x) {return x==pre[x]?x:pre[x]=fi(pre[x]); } void dfs(int now ,int last,int root,int d) {pre[now]=root;if(d>ans) rec=now,ans=d;for(int i=0;i<G[now].size();i++){int y =G[now][i];if(y!=last)dfs(y,now ,root,d+1);} } int main() {#ifdef LOCAL_DEFINEfreopen("D://rush.txt", "r", stdin);#endif//ios::sync_with_stdio(false),cin.tie(0);int n,m,q,a,b,x,y;scanf("%d%d%d",&n,&m,&q);rep(i,1,n) pre[i]=i;for(int i=0;i<m;i++){scanf("%d%d",&a,&b);G[a].pb(b);G[b].pb(a);}for(int i=1;i<=n;i++){if(pre[i]==i){ans=0;dfs(i,0,i,0);dfs(rec,0,rec,0);dis[rec]=ans;}}while(q--){scanf("%d",&a);if(a==1){scanf("%d",&x);printf("%d\n", dis[fi(x)]);}else{scanf("%d%d",&x,&y);x=fi(x),y=fi(y);if(x!=y){pre[y]=x;if(dis[x]<dis[y]) swap(dis[x],dis[y]);dis[x]=max(dis[x],(dis[x]+1)/2+(dis[y]+1)/2+1);}}}return 0; }

轉載于:https://www.cnblogs.com/ffgcc/p/10546379.html

總結

以上是生活随笔為你收集整理的Codeforces Round #260 (Div. 1) C. Civilization(dfs+并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。