Codeforces 455E Civilization【思维+树的直径+并查集】
来源:互联网 发布:金山数据恢复软件多大? 编辑:程序博客网 时间:2024/05/18 21:06
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.
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 aiand 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.
For each event of the first type print the answer on a separate line.
6 0 62 1 22 3 42 5 62 3 22 5 31 1
4
有N个点的一个城市,现在还有m条存在的边,有两种操作:
1.查询城市所在联通块,最远的两座城市的距离(我们要求的方案就是距离最小)。
2.合并两所城市所在的联通块。
思路:
①对于已知的边,我们将各个联通块求一个树上最长链(树的直径);
②然后对于每一对要合并的联通块,我们肯定找中间的点(树的直径上的中间的点)相连,那么构成的联通块,最远的两个点的距离一定是:
Max(Len【A】,(Len【B】+1)/2+(Len【A】+1)/2+1);这里需要保证Len【A】>Len【B】;
问题不难。
Ac代码:
#include<stdio.h>#include<string.h>#include<vector>using namespace std;vector<int>mp[350000];int f[350000];int vis[350000];int sum[350000];int root,maxn;int find(int a){ int r=a; while(f[r]!=r) r=f[r]; int i=a; int j; while(i!=r) { j=f[i]; f[i]=r; i=j; } return r;}void merge(int a,int b){ int A,B; A=find(a); B=find(b); if(A==B)return ; if(sum[A]>sum[B]) { f[B]=A; sum[A]=max(sum[A],(sum[A]+1)/2+(sum[B]+1)/2+1); } else { f[A]=B; sum[B]=max(sum[B],(sum[A]+1)/2+(sum[B]+1)/2+1); }}/******************************/void Dfs(int u,int from,int depth){ if(depth==0)root=u; if(depth>maxn) { maxn=depth; root=u; } vis[u]=1; for(int i=0;i<mp[u].size();i++) { int v=mp[u][i]; if(v==from)continue; else { Dfs(v,u,depth+1); } }}void dfs(int u,int from,int depth){ if(depth>maxn) { maxn=depth; } for(int i=0;i<mp[u].size();i++) { int v=mp[u][i]; if(v==from)continue; else { dfs(v,u,depth+1); } }}/******************************/int main(){ int n,m,q; while(~scanf("%d%d%d",&n,&m,&q)) { for(int i=1;i<=n;i++)mp[i].clear(); for(int i=1;i<=n;i++)f[i]=i,sum[i]=0; for(int i=1;i<=m;i++) { int x,y;scanf("%d%d",&x,&y); mp[x].push_back(y); mp[y].push_back(x); merge(x,y); } memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { if(vis[i]==0) { maxn=0; Dfs(i,-1,0); maxn=0; dfs(root,-1,0); sum[find(i)]=maxn; } } for(int i=1;i<=q;i++) { int op;scanf("%d",&op); if(op==1) { int x;scanf("%d",&x); printf("%d\n",sum[find(x)]); } else { int x,y;scanf("%d%d",&x,&y); if(find(x)==find(y))continue; else merge(x,y); } } }}
- Codeforces 455E Civilization【思维+树的直径+并查集】
- Codeforces 455C Civilization 树的直径+并查集
- 【树的直径+并查集】 codeforces 455C - Civilization
- CodeForces 455C Civilization(并查集+树直径)
- codeforces 455C C. Civilization(树形dp+树的直径+并查集)
- Codeforces 455C. Civilization (树的直径+并查集)
- codeforces round260 div1C&div2E Civilization 并查集+树的直径
- 【打CF,学算法——四星级】CodeForces 455C Civilization (【详解】并查集+树的直径)
- codeforces 455C Civilization 树的最长链+并查集
- Codeforces 455C Civilization(并查集+dfs)
- Codeforces 455C Civilization(并查集+dfs)
- Codeforces 455C Civilization(并查集+树形dp)
- 【CodeForces】445C Civilization 并查集
- codeforces 466C Civilization 并查集+dfs
- Codeforces 455C —— Civilization(并查集,树上最长链)
- coderforces 455C 并查集+树的直径
- Codeforces Round #260 (Div. 1)C题(并查集+树的直径)
- CodeForces445C Civilization 带权并查集
- 欢迎使用CSDN-markdown编辑器
- MyBatis之动态sql
- jquery获取select框当前选中的那一项
- Windows的cd命令
- 移动端UI自动化测试框架对比
- Codeforces 455E Civilization【思维+树的直径+并查集】
- 123
- 使用session实现网站N天免登陆(转载)
- 使用rabbitmq 实现延迟消费
- 网上商城项目总结
- tensorflow编程: Control Flow
- 加密的病历单
- uploadify文件上传实例
- MySQL-InnoDB表