Codeforces Round #260 (Div. 1)——Civilization
- 题意:
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的更多相关文章
- 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) ...
- 递推DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
- Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集
题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...
- Codeforces Round #260 (Div. 2)AB
http://codeforces.com/contest/456/problem/A A. Laptops time limit per test 1 second memory limit per ...
- 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 ...
- 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 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- 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 ...
随机推荐
- hdoj--5621--KK's Point(简单数学)
KK's Point Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- cxf调用WebService
一.用CXF调用WebService的几种方式,参考: http://cxf.apache.org/docs/how-do-i-develop-a-client.html 二.JaxWsProxyFa ...
- SQL Server 2005外围应用配置器
在SQL Server Configuration Manager中,重启“SQL Server(SQL2005)”服务.
- 前端分页功能实现(PC)
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>加 ...
- Windows下配置SVN服务器
2013-09-03 21:40:34 1. 下载安装svn软件 1.1 服务端svn 下载地址:http://subversion.apache.org/packages.html 默认安装路径:D ...
- guice基本使用,配置模块的两种方式(三)
guice是使用module进行绑定的,它提供了两种方式进行操作. 第一种是继承AbstractModule抽象类. package com.ming.user.test; import com.go ...
- 读《我是一只 IT 小小鸟》
读<我是一只 IT 小小鸟> 作为一个一向看重节操的体面人,即使面临许多 DDL 包括期中考试,在忙乱不堪的时候我也断不断告诫自己,不能迫于课程要求仅为了写出一篇笔记而去读书,以后更是如此 ...
- Bootstrap3 学习笔记
图片新窗口打开浏览
- sql多表关联
inner join(等值连接) 只返回两个表中联结字段相等的行 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有 ...
- Session和Cookie对比详解
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...