题目链接

  • 题意:

    n个点,m条边的森林。q次操作。

    每次操作:1、询问x所在树的直径 2、合并x和y所在的树,使得合并后的直径最小

    (1 ≤ n ≤ 3·105; 0 ≤ m < n; 1 ≤ q ≤ 3·105)

  • 分析:

    没有读到图是森林。。

    。做的好纠结

    最開始将每一个树都求一下直径,之后使用并查集处理。每次合并直径:至少是两个树的直径,或者将两个直径的最中间的部分连接求直径

const int MAXN = 310000;

int rt[MAXN], ans[MAXN];
VI G[MAXN];
bool vis[MAXN]; void init(int n)
{
REP(i, n)
{
vis[i] = false;
ans[i] = 0;
G[i].clear();
rt[i] = i;
}
} int find(int n)
{
return n == rt[n] ? n : rt[n] = find(rt[n]);
} void merge(int a, int b)
{
int fa = find(a), fb = find(b);
if (fa != fb)
{
rt[fa] = fb;
ans[fb] = max(ans[fb], (ans[fb] + 1) / 2 + (ans[fa] + 1) / 2 + 1);
ans[fb] = max(ans[fa], ans[fb]);
}
} int Max, id;
void dfs(int u, int fa, int dep)
{
vis[u] = true;
REP(i, G[u].size())
{
int v = G[u][i];
if (v != fa)
dfs(v, u, dep + 1);
}
if (dep > Max)
{
Max = dep;
id = u;
}
} int main()
{
int n, m, q;
while (~RIII(n, m, q))
{
init(n + 1);
REP(i, m)
{
int a, b;
RII(a, b);
G[a].push_back(b);
G[b].push_back(a);
int fa = find(a), fb = find(b);
rt[fa] = fb;
}
FE(i, 1, n)
{
if (!vis[i])
{
Max = -1;
dfs(i, -1, 0);
Max = -1;
dfs(id, -1, 0);
ans[find(i)] = Max;
}
}
REP(i, q)
{
int op;
RI(op);
if (op == 2)
{
int a, b;
RII(a, b);
merge(a, b);
}
else
{
int a;
RI(a);
WI(ans[find(a)]);
}
}
}
return 0;
}

Codeforces Round #260 (Div. 1)——Civilization的更多相关文章

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

    题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...

  2. 递推DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...

  3. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  4. Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集

    题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...

  5. Codeforces Round #260 (Div. 2)AB

    http://codeforces.com/contest/456/problem/A A. Laptops time limit per test 1 second memory limit per ...

  6. Codeforces Round #260 (Div. 1) D. Serega and Fun 分块

    D. Serega and Fun Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/pro ...

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

    A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...

  8. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  9. Codeforces Round #260 (Div. 1) 455 A. Boredom (DP)

    题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory l ...

随机推荐

  1. 安装MySQL最后一步出现错误Error Nr.1045解决方法

    转自:https://blog.csdn.net/gsls200808/article/details/46846019 安装MySQL最后一步出现错误Error Nr.1045 Connection ...

  2. Springboot 之 引入Thymeleaf

    转自:https://segmentfault.com/a/1190000011149325 前言 Spring-boot-starter-web集成了Tomcat以及Spring MVC,会自动配置 ...

  3. golang 初体验

    1.下载golang https://code.google.com/p/go/downloads/list 在windows下安装,下载windows32版本 2.安装 安装完毕,默认在C:\Go ...

  4. 给统计人讲Python(1)_科学计算库-Numpy

    本地代码是.ipynb格式的转换到博客上很麻烦,这里展示部分代码,了解更多可以查看我的git-hub:https://github.com/Yangami/Python-for-Statisticia ...

  5. IO流读取文件内容时,出现空格的问题(未找到原因)

    import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOExceptio ...

  6. map使用

    // map使用 1 #include <iostream> #include "insertVal.h" #include "sort.h" us ...

  7. 洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)

    题目描述 Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always p ...

  8. javascript中对象属性搜索原则

    为什么通过对象就能访问到原型中的属性或者方法? 属性搜索原则: 1 首先会在对象本身查找有没有该属性,如果有直接返回 2 如果没有,此时就会在构造函数中查找通过this给对象添加的成员中有没有,如果有 ...

  9. 利用Axis2默认口令安全漏洞入侵WebService网站

    近期,在黑吧安全网上关注了几则利用Axis2默认口令进行渗透测试的案例,大家的渗透思路基本一致,利用的技术工具也大致相同,我在总结这几则案例的基础之上进行了技术思路的拓展.黑吧安全网Axis2默认口令 ...

  10. 【SQL】分析函数功能-排序

    1:排名,不考虑并列问题 row_number() 2:排名,有并列,并列后的排名不连续 rank() 3:排名,有并列,并列后的排名连续 dense_rank() 测试: SQL> creat ...