【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

给一个森林。
就是由很多棵树组成。。
然后会询问你其中一棵树的最长链。

初始状态的最长链可以用两遍dfs分别找最长路得到。

然后要求你支持合并。

且合并过后。

新的树的最长链尽可能短。

这个合并的过程可以用并查集来表示。

给并查集添加一个变量value

表示这个并查集形成的树的最长链。

两个树合并。

最优的方法就是分别取最长链上的中点。

这样得到的新的树的最长链才可能最短。

新的最长链的长度在value[x],value[y],(value[x]+1)/2+(value[y]+1)/2+1中取最大值就好

(即每个最长链的一半 加上连上它们的那条新的边

(或者原来两个最长链更长。

【代码】

#include <bits/stdc++.h>
using namespace std; const int N = 3e5; int n, m, q, f[N + 10],now,ma,cur,dis[N+10],value[N+10];
vector<int> g[N + 10];
bool flag[N + 10]; void dfs(int x, int pre,bool temp) {
f[x] = now;
if (dis[x] > ma) {
ma = dis[x];
cur = x;
}
for (int y : g[x]) {
if (y == pre) continue;
if (flag[y]==temp) continue;
flag[y] = temp;
dis[y] = dis[x] + 1;
dfs(y, x,temp);
}
} int ff(int x) {
if (x == f[x])
return x;
else
return f[x] = ff(f[x]);
} int main() {
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
memset(dis,255,sizeof dis);
cin >> n >> m >> q;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y); g[y].push_back(x);
}
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= n; i++)
if (dis[i]==-1){
now = i;
ma = 0; cur = i;
dis[i] = 0;
flag[i] = true;
dfs(i, -1,true);
dis[cur] = 0; ma = 0;
flag[cur] = false;
dfs(cur, -1,false);
value[i] = ma;
}
for (int i = 1; i <= q; i++) {
int ope,x,y;
cin >> ope;
if (ope == 1) {
cin >> x;
x = ff(x);
cout << value[x] << endl;
}
else {
cin >> x >> y;
int r1 = ff(x), r2 = ff(y);
if (r1 == r2) continue;
int temp1 = max(value[r1],value[r2]);
int temp2 = (value[r1] + 1) / 2 + (value[r2]+1)/2 + 1;
value[r1] = max(temp1,temp2);
f[r2] = r1;
}
}
return 0;
}

【【henuacm2016级暑期训练】动态规划专题 L】Civilization的更多相关文章

  1. 【henuacm2016级暑期训练-动态规划专题 C】Little Girl and Maximum XOR

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑r最后的二进制形式为 1xxxxx 那么我们肯定想让第一个最高位的1保留. 因此我们选取的另外一个数字 一定是 0xxxxx的形 ...

  2. 【 henuacm2016级暑期训练-动态规划专题 A 】Cards

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 很显然只要维护B,R,G的数量就好了. 可以很容易想到一个dfs(int a,int b,int c) 然后如果a+b+c==1,那 ...

  3. 【henuacm2016级暑期训练-动态规划专题 B】Coloring Trees

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] f[i][j][k]前i个位置,第i个位置放j这个颜色,然后形成了k个联通块的最小花费 分第i个位置有没有已经放颜色两种情况考虑. ...

  4. 【 【henuacm2016级暑期训练】动态规划专题 P】Animals

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 第i只动物如果饲养它的话. 代价是固定的就是(n-i+1)a[i] 所以相当于给你n个物品,每个物品的重量为(n-i+1)a[i], ...

  5. 【【henuacm2016级暑期训练】动态规划专题 O】Robot Rapping Results Report

    [链接] 我是链接,点我呀:) [题意] 让你确定一个最小的k 使得1..k这些比赛的结果能够推导出所有人之间的实力大小 [题解] 如果关系越多.那么就越能确定所有人之间的大小关系. (多一点也能唯一 ...

  6. 【【henuacm2016级暑期训练】动态规划专题 N】Valid Sets

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 给你一棵树. 让你统计其中子树T的数量. 这个子树T要满足最大值和最小值之差小于等于d 树形DP 可以枚举点root为子树的根. 统 ...

  7. 【【henuacm2016级暑期训练】动态规划专题 M】Little Pony and Harmony Chest

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每一位显然只要取1..60这些数字. 然后需要保证每个这些数字里面,每个数字所用到的质因子都它所唯一拥有的.别人不能用 因为如果别人 ...

  8. 【 【henuacm2016级暑期训练】动态规划专题 K】 Really Big Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现如果x是reallynumber那么x+1也会是reallynumber.... (个位数+1,各位数的和+1了但是整个数也+ ...

  9. 【【henuacm2016级暑期训练】动态规划专题 J】Red-Green Towers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然最多1000行的样子. 从上到小做dp 设f[i][j]为前i行,使用了j个红色方块的方案数. f[1][r] = 1;如果r& ...

随机推荐

  1. Fibbonacci Number(杭电2070)

    /*Fibbonacci Number Problem Description Your objective for this question is to develop a program whi ...

  2. comp.lang.javascript FAQ [zz]

    comp.lang.javascript FAQ Version 32.2, Updated 2010-10-08, by Garrett Smith FAQ Notes 1 Meta-FAQ met ...

  3. android TextView加边框

    为TextView加边框.须要在drawable建xml文件,里面设置shape来设置文本框的特殊效果. <?xml version="1.0" encoding=" ...

  4. Linux 程序设计学习笔记----Linux下文件类型和属性管理

    转载请注明出处:http://blog.csdn.net/suool/article/details/38318225 部分内容整理自网络,在此感谢各位大神. Linux文件类型和权限 数据表示 文件 ...

  5. Cocos Code IDE

    https://www.cnblogs.com/luorende/p/6464181.html http://www.cocoachina.com/bbs/read.php?tid-464164.ht ...

  6. DNS隧道工具汇总——补充,还有IP over DNS的工具NSTX、Iodine、DNSCat

    github上有一堆的工具:https://github.com/search?utf8=%E2%9C%93&q=DNS+tunnel+&type= DNS隧道大检阅 研究了一天的DN ...

  7. oracle得到建表语句

    第一种方法是使用工具,如:pl/sql developer,在[工具]--[导出用户对象]出现就可以得到建表脚本. 第二种方法是,sql语句. DBMS_METADATA.GET_DDL包可以得到数据 ...

  8. 【BZOJ 2038】小Z的袜子

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2038 [算法] 莫队算法 [代码] #include<bits/stdc++. ...

  9. [MySQL] 统计函数记录

    时间段统计========== 按年汇总,统计:select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by ...

  10. Reusability1

    Reusability 1. 复用的层面 1.1 代码级别复用 顾名思义,代码复用就是把代码都搬过来,这是最主要的复用 1.1.1 白盒复用 白盒的意思就是里面的东西我们都知道是什么,具体实现的方式也 ...