[BZOJ1776][Usaco2010 Hol]cowpol 奶牛政坛
Description
政党1最大的两只奶牛的距离是3(也就是奶牛3和奶牛6的距离)。政党2最大的两只奶牛的距离是2(也就是奶牛2和4,4和5,还有5和2之间的距离)。 帮助奶牛们求出每个政党的范围。Input
Output
Sample Input
1 3
2 1
1 0
2 1
2 1
1 5
Sample Output
2
Solution
其实就是树的距离。就是那个两遍$bfs$的东西
对每个政党都做一遍树的距离就好了
找一个深度最大的点,然后与同政党的所有点分别求一下$lca$,取个$max$
这样复杂度是$O(nlogn)$的(每个节点只会访问一次,$lca$效率$O(logn)$)
至于$lca$求法就随意了,这里写的是树剖
#include <bits/stdc++.h> using namespace std ; #define N 400010 int n , k , head[ N ] , cnt , s ;
int a[ N ] , fa[ N ] ;
int dep[ N ] , siz[ N ] , top[ N ] ;
int mx[ N ] ;
struct node {
int to , nxt ;
} e[ N ] ;
vector < int > vt[ N ] ; void ins( int u , int v ) {
e[ ++ cnt ].to = v ;
e[ cnt ].nxt = head[ u ] ;
head[ u ] = cnt ;
} void dfs1( int u ) {
siz[ u ] = ;
for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
if( e[ i ].to == fa[ u ] ) continue ;
dep[ e[ i ].to ] = dep[ u ] + ;
dfs1( e[ i ].to ) ;
siz[ u ] += siz[ e[ i ].to ] ;
}
} void dfs2( int u , int topf ) {
top[ u ] = topf ;
int k = ;
for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
if( e[ i ].to == fa[ u ] ) continue ;
if( siz[ e[ i ].to ] > siz[ k ] ) k = e[ i ].to ;
}
if( !k ) return ;
dfs2( k , topf ) ;
for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
if( e[ i ].to == k || e[ i ].to == fa[ u ] ) continue ;
dfs2( e[ i ].to , e[ i ].to ) ;
}
} int lca( int x , int y ) {
while( top[ x ] != top[ y ] ) {
if( dep[ top[ x ] ] < dep[ top[ y ] ] ) swap( x , y ) ;
x = fa[ top[ x ] ] ;
}
if( dep[ x ] > dep[ y ] ) swap( x , y ) ;
return x ;
} bool cmp( int a , int b ) {
return dep[ a ] > dep[ b ] ;
} int main() {
scanf( "%d%d" , &n , &k ) ;
for( int i = ; i <= n ; i ++ ) {
int p ;
scanf( "%d%d" , &a[ i ] , &p ) ;
fa[ i ] = p ;
if( p ) ins( i , p ) , ins( p , i ) ;
vt[ a[ i ] ].push_back( i ) ;
if( p == ) s = i ;
}
dfs1( s ) ;
dfs2( s , s ) ;
for( int i = ; i <= k ; i ++ ) {
int ans = ;
sort( vt[ i ].begin() , vt[ i ].end() , cmp ) ;
for( int j = , len = vt[ i ].size() ; j < len ; j ++ ) {
int l = lca( vt[ i ][ ] , vt[ i ][ j ] ) ;
ans = max( ans , dep[ vt[ i ][ ] ] + dep[ vt[ i ][ j ] ] - * dep[ l ] ) ;
}
printf( "%d\n" , ans ) ;
}
return ;
}
[BZOJ1776][Usaco2010 Hol]cowpol 奶牛政坛的更多相关文章
- [bzoj1776][Usaco2010 Hol]cowpol 奶牛政坛_倍增lca
[Usaco2010 Hol]cowpol 奶牛政坛 题目大意: 数据范围:如题面. 题解: 第一想法是一个复杂度踩标程的算法..... 就是每种政党建一棵虚树,然后对于每棵虚树都暴力求直径就好了,复 ...
- 【BZOJ1776】[Usaco2010 Hol]cowpol 奶牛政坛 树的直径
[BZOJ1776][Usaco2010 Hol]cowpol 奶牛政坛 Description 农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N. ...
- COGS——T 803. [USACO Hol10] 政党 || 1776: [Usaco2010 Hol]cowpol 奶牛政坛
http://www.lydsy.com/JudgeOnline/problem.php?id=1776||http://cogs.pro/cogs/problem/problem.php?pid=8 ...
- bzoj:1776: [Usaco2010 Hol]cowpol 奶牛政坛
Description 农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向道路,用各种各样的方法连接这些草地.而且从每片 ...
- bzoj 1776: [Usaco2010 Hol]cowpol 奶牛政坛——树的直径
农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向道路,用各种各样的方法连接这些草地.而且从每片草地出发都可以抵达其他所 ...
- [Usaco2010 Hol]cowpol 奶牛政坛
题目描述: 农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向道路,用各种各样的方法连接这些草地.而且从每片草地出发都可 ...
- 【BZOJ】1776: [Usaco2010 Hol]cowpol 奶牛政坛
[题意]给定n个点的树,每个点属于一个分类,求每个分类中(至少有2个点)最远的两点距离.n<=200000 [算法]LCA [题解]结论:树上任意点集中最远的两点一定包含点集中深度最大的点(求树 ...
- bzoj [Usaco2010 Hol]cowpol 奶牛政坛【树链剖分】
意识流虚树 首先考虑只有一个党派,那么可以O(n)求树的直径,步骤是随便指定一个根然后找距离根最远点,然后再找距离这个最远点最远的点,那么最远点和距离这个最远点最远的点之间的距离就是直径 那么考虑多党 ...
- BZOJ 1776: [Usaco2010 Hol]cowpol 奶牛政坛 LCA + 树的直径
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...
随机推荐
- django源码笔记-【1】(转)
add by zhj:第二段代码有修改 原文:http://www.cnblogs.com/gaott/archive/2012/02/28/2371181.html 前言 Django是一个开放源代 ...
- 腾讯在线文档发布:实现QQ、微信多平台多人协作编辑
18日,腾讯宣布推出专注多人协作的在线文档产品—腾讯文档,据介绍,腾讯文档是一款支持随时随地创建.编辑的多人协作式在线文档工具,拥有一键翻译.实时股票函数和浏览权限安全可控等功能,以及打通QQ.微信等 ...
- 万恶之源 - Python基础数据类型一
整数 整数在Python中的关键字用int来表示; 整型在计算机中运于计算和比较 在32位机器上int的范围是: -2**31-2**31-1,即-2147483648-2147483647 在64 ...
- 前端js如何生成一个对象,并转化为json字符串
https://www.cnblogs.com/May-day/p/6841958.html 一,直接上代码 <script src="../../Content/jquery-2.0 ...
- (转)找回Git中丢失的Commit
总结:更新代码前一定要先将本地修改的文件存到本地git仓库.今天脑残直接更新了远程仓库代码导入今天写的代码...... @[git|commit|reflog] 在使用Git的过程中,有时候会因为一些 ...
- 记两个国外CTF的弱pwn
两道题都来自CSAW CTF 18.PWN学得不够多,如果哪里错了,欢迎留言交流. 第一个题 get_it checksec检查之后,发现栈保护没开,很可能是栈溢出.IDA打开F5看伪源码. int ...
- vmware tool安装
https://www.vmware.com/support/ws55/doc/ws_newguest_tools_linux.html VMware Workstation 5.5 Features ...
- 在MS SQL删除重复行的几种方法
1.如果有ID字段,就是具有唯一性的字段 delect table where id not in ( select max(id) ...
- windows上备份mysql数据库
方案一:采用mysql自带的工具mysqldump. 脚本文件backup.bat如下: set "YMD=%date:~,4%%date:~5,2%%date:~8,2%"cd ...
- Description Resource Path LocationType Java compiler level does not match the version of the instal
从别的地方导入进来的maven项目报: Description Resource Path Location TypeJava compiler level does not match the ve ...