[BZOJ1776][Usaco2010 Hol]cowpol 奶牛政坛
Description

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) ...
随机推荐
- IO流(10)复制多级文件夹
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import ja ...
- JS闭包中的循环绑定处理程序
前几天工作中写前端js代码时,遇到了遍历元素给它添加单击事件.就是这个问题让我整整调了一个下午.最后还是下班回家,上网查资料才知道怎么解决的. (PS:之前也在<jQuery基础教程>第四 ...
- 十天精通CSS3(7)
:enabled选择器 在Web的表单中,有些表单元素有可用(“:enabled”)和不可用(“:disabled”)状态,比如输入框,密码框,复选框等.在默认情况之下,这些表单元素都处在可用状态.那 ...
- Spark ClassNotFoundException $$anonfun$2
Spark ClassNotFoundException $$anonfun$2 1. 软件环境: 软件 版本 Spark 原生1.6.0 Hadoop 原生2.6.5 2. 应用场景&问题描 ...
- ID和Name
ID和Name都可以用来标识一个标记,Javascript分别有两个方法getElementById和getElementByName来定位Dom节点. 区别如下: 1.我们知道在网页做Post提交时 ...
- 用python写http接口自动化测试框架
本文是转载张元礼的博客 http://blog.csdn.Net/vincetest 一.测试需求描述 对服务后台一系列的http接口功能测试. 输入:根据接口描述构造不同的参数输入值 输出:XML文 ...
- matlab 以excel格式将字符串数组写入TXT文件
[m, n] = size(FFoutpu);fp = fopen('FFoutpu.txt','wt');fprintf(fp, 'name CODE ROTC EBIT_EV SHIZHI ROT ...
- SP Flash Tool New Version v5.1352.01
Friends, Sp Tool updated to new version with whole new revamped interface New SP Flash Tool 3.1352.0 ...
- cordova+Android Studio 1.0+ionic+win7(转)
转自http://blog.csdn.net/fuyunww/article/details/42216125 目录(?)[-] 在项目目录下执行 a创建工程 b添加平台支持 c添加插件在Androi ...
- 禁止F12与右键
实践项目的代码哦,给大家分享下,如何屏蔽右键与F12. 应用网站 www.empiretreasure.vip 与 www.MineBook.vip.可以去逛逛哦. 不多说了,上代码 ...