洛谷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 ...
随机推荐
- python闭包的代码
- python 删除模块
import systry: import librabbitmqexcept Exception: passelse: version = getattr(librabbitmq, ...
- js判断是否为undefined
typeof(isadmin)=="undefined"需要使用typeof才能判断
- php json中文被转义
php 5.4 json_encode($str, JSON_UNESCAPED_UNICODE); 5.4版本以下 方法一function encode_json($str){ $code = js ...
- clone()与image和 cloneTo()
Mat image = imread("1.png" ) ; Mat image1 ; Mat image1(image) ;//仅是创建了Mat的头部分,image1与image ...
- Error in building opencv with ffmpeg
I installed ffmpeg according to this article. ffmpeg installation was ok. Now I build opencv with ff ...
- 树形DP(记忆化搜索) HYSBZ - 1509
题目链接:https://vjudge.net/problem/HYSBZ-1509 我参考的证明的论文:8.陈瑜希<多角度思考 创造性思维>_百度文库 https://wenku.ba ...
- APP内的H5页面测试方法, 移动端的浏览器(例如UC浏览器)测试方法
前言: 用appium做UI自动化,测试APP里面的H5和测试手机浏览器打开的H5的操作流程上是有所区别的.比如要测试APP内嵌的H5需要先操作appium启动APP,然后通过context切到web ...
- Class语法糖
TypeScript源码 class A { hello() { } } class B extends A{ welcome() { } } TypeScript编译 var __extends = ...
- Mac下环境变量设置错误,导致命令行命令不能使用后的解决办法
1 在命令行中,临时设置环境变量 export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin 2 各种命令就可以使用了.然后修复错误的环境变量配置 ...