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--2546--背包)
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- js获取验证码 秒表效果(原创)
<script src="http://code.jquery.com/jquery-latest.js"></script> <input type ...
- [Java] 总结1.5/1.6/1.7版本的特性
开发过程中接触到了从jdk1.5---jdk1.7的使用,在不同的阶段,都使用过了jdk的一些新特性,操作起来更加方面啦!特此总结了下,以下是测试代码: JDK1.5新特性: 1.自动装箱与拆箱: I ...
- JDBC基础01
今日知识 1. JDBC基本概念2. 快速入门3. 对JDBC中各个接口和类详解 JDBC: 1. 概念:Java DataBase Connectivity Java 数据库连接, Java语言操作 ...
- Keras简单使用
Keras简单使用在keras中建立模型测试自己的图片一些有用的函数(持续更新) Keras简单使用 在keras中建立模型 相对于自己写机器学习相关的函数,keras更能快速搭建模型,流程如下: 通 ...
- Xcode7.3 使用NSURLSession发送HTTP请求报错
控制台打印:Application Transport Security has blocked a cleartext HTTP (http://) resource load since it i ...
- Android之Action Bar
Action Bar在实际应用中,很好地为用户提供了导航,窗口位置标识,操作点击等功能.它出现于Android3.0(API 11)之后的版本中,在2.1之后的版本中也可以使用. 添加与隐藏Actio ...
- React+webpack
webPack + React 步骤: 1. 创建文件夹 src 源代码目录 main.js 打包的入口文件 App.js 项目的根组件 import React,{Component} from ' ...
- Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息
本文主要介绍了Android项目集成百度地图API,使用AlarmManager定时调用Service,在Service中请求坐标更新,并通过坐标得到省.市和县三级地理位置信息的方法. 程序结构很简单 ...
- ProgressDialog的关键几个函数
进度条对话框在开发是常见的一种工具,只要注意以下几点,就可以轻松使用. ProgressDialog.setMax(MAX_PROGRESS); //设置最大值,可以如下定义一个常值 //priva ...