洛谷2971 [USACO10HOL]牛的政治Cow Politics
原题链接
假设只有一个政党,那么这题就退化成求树的直径的问题了,所以我们可以从此联想至\(k\)个政党的情况。
先处理出每个政党的最大深度,然后枚举每个政党的其它点,通过\(LCA\)计算长度取\(\max\)即可。
因为枚举只是枚举该政党的所有点,所以总的枚举复杂度依旧是\(O(n)\),总复杂度\(O(nlog_2n)\)。
#include<cstdio>
#include<cmath>
using namespace std;
const int N = 2e5 + 10;
const int M = N << 1;
const int K = 19;
int fi[N], di[M], ne[M], f[N][K], de[N], p[N], ma_p[N], ma_de[N], an[N], gn, l;
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
inline void add(int x, int y)
{
di[++l] = y;
ne[l] = fi[x];
fi[x] = l;
}
inline int maxn(int x, int y)
{
return x > y ? x : y;
}
inline void sw(int &x, int &y)
{
int z = x;
x = y;
y = z;
}
void dfs(int x)
{
int i, y;
if (ma_de[p[x]] < de[x])
{
ma_de[p[x]] = de[x];
ma_p[p[x]] = x;
}
for (i = 1; i <= gn; i++)
f[x][i] = f[f[x][i - 1]][i - 1];
for (i = fi[x]; i; i = ne[i])
if (!de[y = di[i]])
{
f[y][0] = x;
de[y] = de[x] + 1;
dfs(y);
}
}
int lca(int x, int y)
{
int i;
if (de[x] > de[y])
sw(x, y);
for (i = gn; ~i; i--)
if (de[f[y][i]] >= de[x])
y = f[y][i];
if (!(x ^ y))
return x;
for (i = gn; ~i; i--)
if (f[x][i] ^ f[y][i])
{
x = f[x][i];
y = f[y][i];
}
return f[x][0];
}
int main()
{
int i, n, m, x, ro;
n = re();
m = re();
gn = log2(n);
for (i = 1; i <= n; i++)
{
p[i] = re();
x = re();
if (!x)
{
ro = i;
continue;
}
add(i, x);
add(x, i);
}
de[ro] = 1;
dfs(ro);
for (i = 1; i <= n; i++)
an[p[i]] = maxn(an[p[i]], ma_de[p[i]] + de[i] - (de[lca(ma_p[p[i]], i)] << 1));
for (i = 1; i <= m; i++)
printf("%d\n", an[i]);
return 0;
}
洛谷2971 [USACO10HOL]牛的政治Cow Politics的更多相关文章
- LCA【洛谷P2971】 [USACO10HOL]牛的政治Cow Politics
P2971 [USACO10HOL]牛的政治Cow Politics 农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向 ...
- [USACO10HOL]牛的政治Cow Politics
农夫约翰的奶牛住在N ( <= N <= ,)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向道路,用各种各样的方法连接这些草地.而且从每片草地出发都可以抵达其他所有草地.也就是 ...
- 洛谷P3080 [USACO13MAR]牛跑The Cow Run
P3080 [USACO13MAR]牛跑The Cow Run 题目描述 Farmer John has forgotten to repair a hole in the fence on his ...
- 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- 洛谷P2971 牛的政治Cow Politics
题目描述 Farmer John's cows are living on \(N (2 \leq N \leq 200,000)\)different pastures conveniently n ...
- 洛谷P2853 [USACO06DEC]牛的野餐Cow Picnic
题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...
- 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup
https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...
- 洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths
题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...
随机推荐
- tomcat架构分析(valve机制)
关于tomcat的内部逻辑单元的存储空间已经在相关容器类的blog里阐述了.在每个容器对象里面都有一个pipeline及valve模块. 它们是容器类必须具有的模块.在容器对象生成时自动产生.Pipe ...
- kafka 清除topic数据脚本
原 kafka 清除topic数据脚本 2018年07月25日 16:57:13 pete1223 阅读数:1028 #!/bin/sh param=$1 echo " ...
- Python基础之-----------函数
---恢复内容开始--- 函数:在其他的语言中,我们也经常听到函数的概念,那么什么是函数呢?在Java中叫做method: 定义:函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函 ...
- js中函数的 this、arguments 、caller,call(),apply(),bind()
在函数内部有两个特殊的对象,arguments 和 this,还有一个函数对象的属性caller. arguments对象 arguments是一个类似数组的对象,包含着传入函数的所有参数. func ...
- 如何搭建python+selenium2+eclipse的环境
搭建python和selenium2的环境(windows) 1.下载并安装python(我用的是2.7的版本) 可以去python官网下载安装:http://www.python.org/getit ...
- spark快速开发之scala基础之1 数据类型与容器
写在前面 面向java开发者.实际上,具有java基础学习scala是很容易.java也可以开发spark,并不比scala开发的spark程序慢.但学习scala可有助于更快更好的理解spark.比 ...
- perl-我的第一个程序
1.问题描述: 总共90位长度的位流数据,其中只有5位的数据为1,其余位全部为0.统计好多组5位的简化数据(每一位之间空格隔开,每一组一行),将其扩展到90位. #!D:/EDA/Perl/bin $ ...
- 【centos】centos安装配置samba
因为我的centos为一台阿里云服务器,想和我本机(mac)进行文件共享.所以在我的阿里云上安装配置samba. 服务器环境:centos 7.3 本地环境:mac 第1步:首先查看是否已经安装过了s ...
- AngularJS——第8章 服务
第8章 服务 服务是一个对象或函数,对外提供特定的功能. 8.1 内建服务 1. $location是对原生Javascript中location对象属性和方法的封装. // $location内置服 ...
- Java构建指定大小文件
Java快速创建指定大小的文件,最多的解决办法就是循环向文件里面入固定大小的空字节,但是这种方式构建大文件性能比较低下,因此有这样两种方式可供参考: Java有一个类:FileChannel,查阅AP ...