原题链接

假设只有一个政党,那么这题就退化成求树的直径的问题了,所以我们可以从此联想至\(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的更多相关文章

  1. LCA【洛谷P2971】 [USACO10HOL]牛的政治Cow Politics

    P2971 [USACO10HOL]牛的政治Cow Politics 农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向 ...

  2. [USACO10HOL]牛的政治Cow Politics

    农夫约翰的奶牛住在N ( <= N <= ,)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向道路,用各种各样的方法连接这些草地.而且从每片草地出发都可以抵达其他所有草地.也就是 ...

  3. 洛谷P3080 [USACO13MAR]牛跑The Cow Run

    P3080 [USACO13MAR]牛跑The Cow Run 题目描述 Farmer John has forgotten to repair a hole in the fence on his ...

  4. 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  5. 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  6. 洛谷P2971 牛的政治Cow Politics

    题目描述 Farmer John's cows are living on \(N (2 \leq N \leq 200,000)\)different pastures conveniently n ...

  7. 洛谷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 ...

  8. 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup

    https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...

  9. 洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths

    题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...

随机推荐

  1. nmap使用

    Nmap使用 Nmap是主机扫描工具,他的图形化界面是Zenmap,分布式框架为Dnamp. Nmap可以完成以下任务: 主机探测 端口扫描 版本检测 系统检测 支持探测脚本的编写 Nmap在实际中应 ...

  2. js中的arguments对象

    在Javascript中没有函数重载,而arguments对象弥补了这点不足. js函数的参数是一个数组,在参数个数不固定的情况下,只需要给方法传递不同元素个数的数组即可.即使声明函数时没有形式参数, ...

  3. AS不能真机调试 (转)

    经常遇到这种问题 现总结一下原因 打开手机的开发人员选项,允许USB调试 AS--->tools--->android-->Enable ADB Interation选项 勾上. A ...

  4. PropertyGrid控件动态生成属性及下拉菜单 (转)

    http://blog.sina.com.cn/s/blog_6f14b7010101b91b.html https://msdn.microsoft.com/zh-cn/library/ms1718 ...

  5. CentOS 7安装Zabbix 3.4

    01.最小化安装操作系统 02.升级系统组件到最新版本 yum -y update 03.关闭 SELinux sed -i “s/SELINUX=enforcing/SELINUX=disabled ...

  6. Zabbix配置优化

    1.zabbix开启中文语言zabbix是一个多语言监控系统,默认使用英文并且也支持中文语言,详见<zabbix汉化方法>,但是安装zabbix里面看不到中文语言.请往下看: ![](ht ...

  7. vue初学:基础概念

    一.vue使用步骤: 1.引包vue.js 2.html中写要操作的DOM节点 3.创建vue对象:new Vue({options}); 4.配置options:el:(要操作的对象,用选择器,同j ...

  8. python:django

    ====启动django==== python manager.py runserver --host 0.0.0.0 --port 9008 python manager.py runserver ...

  9. 项目总结09:select标签下封装option标签

    项目中经常用到Select标签,用封装好的方法获取option,可以避免冗赘的代码: 1.JSP--标签 <select class="width_md" name=&quo ...

  10. Union and Intersection of two sorted lists 并集和交集

    跟面试官确认是arrayList还是singly-linked list /*  Union 并集:两个升序的list a, b, 返回其并集(升序排序)*/ public class UnionTw ...