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+并查集)相关推荐

  1. Codeforces Round #548 (Div. 2)C. Edgy Trees 并查集

    codeforces 1139C 题目链接:http://codeforces.com/contest/1139/problem/C 题意: 给你一个n个结点n-1条边的无向连通图,由红边和黑边组成. ...

  2. Codeforces Round #260 (Div. 2)

    Codeforces Round #260 (Div. 2) 题目链接 A:水题,事实上仅仅要推断有没有一个ai != bi就可以,由于都保证是1 - n的不相等数字 B:找到2 3 4的循环节,发现 ...

  3. 图论 ---- Codeforces Round #649 (Div. 2)D题[dfs求环+深度分层求图中独立集]

    D. Ehab's Last Corollary 题目大意: 就是给你一个联通图,你有两种选择 1.你可以输出包含⌈k2⌉\lceil{k\over2}\rceil⌈2k​⌉个顶点得独立点集,什么是独 ...

  4. 570D Codeforces Round #316 (Div. 2) D(dfs序,时间戳,二分

    题目:一棵树上每个节点有个字符值,询问每个节点的深度为h的子节点的字符是否能组成一个回文串. 思路:首先是奇妙的dfs序和时间戳,通过记录每个节点的dfs进出时间,可以发现某个节点的子节点的进出时间均 ...

  5. E. The Humanoid Codeforces Round #834 (Div. 3)(暴力dfs?)

    题意:有一个怪兽和n个宇航员(怪兽有一个绿色药丸和一个蓝色药丸),怪兽和宇航员都有自己的一个力量值,怪兽在面对力量值小于他的宇航员时可以吃了他并且获得他的的力量(向下取整),怪兽可以在任意时刻吃他的小 ...

  6. Codeforces Round #260 (Div. 1) A - Boredom DP

    点击打开链接 题意: 给你n个数,你每次可以选择删除去一个数x,但是等于x+1和等于x-1的数都得删去 你每一次操作可以得x分 思路一: dp[i]表示到i后能够得到的最大分数 dp[i]=max(d ...

  7. DFS Codeforces Round #299 (Div. 2) B. Tavas and SaDDas

    题目传送门 1 /* 2 DFS:按照长度来DFS,最后排序 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #includ ...

  8. Codeforces Round #628 (Div. 2) F. Ehab‘s Last Theorem dfs树

    传送门 文章目录 题意: 思路: 题意: 给你个nnn个点mmm条边的图,可以选择完成以下两个任务中的一个: (1)(1)(1)找出大小恰好为n\sqrt nn​的一个独立集. (2)(2)(2)找出 ...

  9. Codeforces Round #731 (Div. 3) G. How Many Paths? dfs + 拓扑 + 思维

    传送门 题意: 给你一张nnn个点mmm条边的图,让你对每个点确定一个编号,规则如下: (1)(1)(1) 对于不能到的点编号为000. (2)(2)(2) 对于只有一条路径能到这个点的点编号为111 ...

最新文章

  1. AtomicLong可以被原子地读取和写入的底层long值的操作
  2. 10本计算机视觉必读经典图书,入门篇 + 提升篇
  3. 第九周项目实践3 利用二叉树遍历思想解决问题
  4. javascript感叹号1_「翻译」JavaScript的可视化学习之三:作用域(链)
  5. 爬小说遇到编码问题 笔趣阁
  6. 深入浅出Node.js (2) - 模块机制
  7. 如何在PowerPoint中自动调整图片大小
  8. linux shell之cut命令
  9. 从0到1,手把手教你如何使用哈工大NLP工具——PyLTP
  10. redistemplate hash 过期时间_redisTemplate的使用以及和stringRedisTemplate的区别
  11. 解决ios下的微信打开的页面背景音乐无法自动播放(转载)
  12. VideoView播放视频会引起其它音乐播放器暂停问题解决
  13. python中的fun_Python fun中*args,**kwargs参数的含义和用法(*args,**kwargs),Pythonfunargskwargs,及...
  14. 遗传算法的应用之函数优化和组合优化
  15. 28岁以后,我再也没为工作拼过命
  16. VB打造QQ批量登陆器
  17. 2018安徽省考c语言笔试答案,2018年安徽公务员考试行测真题解读
  18. Axure计算器原型
  19. 机器算法基础——回归分析
  20. python的界面文字翻译_教你用Python实现微信翻译机器人

热门文章

  1. OpenCV cvReleaseImage把图像怎么样了?
  2. struts2 spring hibernate 原理
  3. SharePoint 2010 同步用户Services 一直Starting 终极解决方案
  4. Windows Vista Upgrade Advisor 1.0
  5. android studio 以源码形式导入volley作为library,SDK的目录结构
  6. 【Android】init.rc
  7. 实现类似Android联系人搜索功能
  8. Android 蓝牙操作--读取远程已配对的蓝牙设备
  9. 006-筛选分类排序搜索查找Filter-Classificatio-Sort-Search-Find-Seek-Locate
  10. RecyclerView万能分割线