题目链接

  • 题意:

    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. table合并单元格 colspan(跨列)和rowspan(跨行)

    colspan和rowspan这两个属性用于创建特殊的表格. colspan是“column span(跨列)”的缩写.colspan属性用在td标签中,用来指定单元格横向跨越的列数: 在浏览器中将显 ...

  2. 常用的CSS命名

    头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体佈局宽度:wrapper 左右中:left rig ...

  3. size_t与size_type的使用

    size_t 是为了方便系统之间的移植而定义的 在32位系统上 定义为 unsigned int在64位系统上 定义为 unsigned long 更准确地说法是 在 32位系统上是32位无符号整形在 ...

  4. IDEA导入个性化主题的方法

    IDEA的主题可以自定义,也可从网上下载 http://www.riaway.com/theme.php 喜欢的主题,保存到本地. 主题是一个jar的包.导入到idea的方法如下: file –> ...

  5. concurrently - npm 同时运行前端和后台服务

    项目基于vue(前端)+node(后台),需要启动两个服务 0.文件夹结构及package.json内容: 1.客户端 npm run dev 2.服务器 cd server npm run serv ...

  6. 编写模块时的声明(含MODULE_LICENSE等)(转)

    编写模块必须先声明下面两句: #include <linux/module.h>               //这个头文件包含了许多符号与函数的定义,这些符号与函数多与加载模块有关 #i ...

  7. Period UVA - 1328_结论题

    Code: #include<cstdio> #include<cstring> using namespace std; const int maxn=1000000+5; ...

  8. mDNS 原理的简单理解

    转自:http://www.binkery.com/post/318.html mDNS 原理的简单理解 mDNS multicast DNS , 使用5353端口. 在局域网内,你要通过一台主机和其 ...

  9. 自动化测试之firebug、firepath、IDE的使用

    1firebug安装-firefox添加组件-firebug 如图 firepath依赖于firebug 展示路径用,安装和firebug一样

  10. Codeforces 787B Not Afraid( 水 )

    链接:传送门 题意:判断 m 组数,如果某一组中出现负数就判断这一组中是否存在与之相反的数,如果每一组中都满足要求则输出 "NO" 反之输出 "YES" 思路: ...