题目描述

Farmer John's cows are living on \(N (2 \leq N \leq 200,000)\)different pastures conveniently numbered \(1..N\). Exactly \(N-1\) bidirectional cow paths (of unit length) connect these pastures in various ways, and each pasture is reachable from any other cow pasture by traversing one or more of these paths (thus, the pastures and paths form a graph called a tree).

The input for a particular set of pastures will specify the parent node \(P_i (0 \leq P_i \leq N)\) for each pasture. The root node will specify parent \(P_i == 0\), which means it has no parent.

The cows have organized K$ (1 \leq K \leq N/2)$ different political parties conveniently numbered \(1..K\). Each cow identifies with a single

political party; cow i identifies with political party \(A_i (1 \leq A_i \leq K)\). Each political party includes at least two cows.

The political parties are feuding and would like to know how much 'range' each party covers. The range of a party is the largest distance between any two cows within that party (over cow paths).

For example, suppose political party \(1\) consists of cows \(1, 3\), and \(6\), political party \(2\) consists of cows \(2, 4\), and \(5\), and the pastures are connected as follows (party 1 members are marked as -\(n\)-):

\(-3- | -1- / | \ 2 4 5\)

\(| -6-\) The greatest distance between any two pastures of political party \(1\) is \(3\) (between cows \(3\) and \(6\)), and the greatest distance for political party 2 is 2 (between cows \(2\) and \(4\), between cows \(4\) and \(5\), and between cows \(5\) and \(2\)).

Help the cows determine party ranges.

TIME LIMIT: \(2\) seconds

MEMORY LIMIT: \(64\)MB

农夫约翰的奶牛住在\(N (2 \leq N \leq 200,000)\)片不同的草地上,标号为\(1\)到\(N\)。恰好有\(N-1\)条单位长度的双向道路,用各种各样的方法连接这些草地。而且从每片草地出发都可以抵达其他所有草地。也就是说,这些草地和道路构成了一种叫做树的图。输入包含一个详细的草地的集合,详细说明了每个草地的父节点\(P_i (0 \leq P_i \leq N)\)。根节点的\(P_i == 0\), 表示它没有父节点。因为奶牛建立了\(1\)到\(K\)一共\(K (1 \leq K \leq N/2)\)个政党。每只奶牛都要加入某一个政党,其中, 第i只奶牛属于第\(A_i (1 \leq A_i \leq K)\)个政党。而且每个政党至少有两只奶牛。 这些政党互相吵闹争。每个政党都想知道自己的“范围”有多大。其中,定义一个政党的范围是这个政党离得最远的两只奶牛(沿着双向道路行走)的距离。

输入输出格式

输入格式:

  • Line \(1\): Two space-separated integers: \(N\) and \(K\)

  • Lines \(2..N+1\): Line \(i+1\) contains two space-separated integers: \(A_i\) and \(P_i\)

输出格式:

  • Lines \(1..K\): Line i contains a single integer that is the range of party \(i\).

输入输出样例

输入样例#1:

6 2
1 3
2 1
1 0
2 1
2 1
1 5

输出样例#1:

3
2

思路:题意是让你求在树上每个政党的两点间的最远距离,我们可以考虑先把每个政党的最大深度的点找出来,然后\(O(n)\)更新每个政党两点间的最大值,这里还是要通过\(LCA\)求两点间的树上距离。

代码:

#include<cstdio>
#include<algorithm>
#include<cctype>
#define maxn 200007
using namespace std;
int n,k,num,rt,head[maxn],f[maxn][22],d[maxn],dis[maxn>>1],a[maxn],b[maxn],c[maxn];
inline int qread() {
char c=getchar();int num=0,f=1;
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) num=num*10+c-'0';
return num*f;
}
struct Edge {
int v,nxt;
}e[maxn<<1];
inline void ct(int u, int v) {
e[++num].v=v;
e[num].nxt=head[u];
head[u]=num;
}
void dfs(int u, int fa) {
for(int i=head[u];i;i=e[i].nxt) {
int v=e[i].v;
if(v!=fa) {
f[v][0]=u;
d[v]=d[u]+1;
dfs(v,u);
}
}
}
inline int lca(int a,int b) {
if(d[a]>d[b]) swap(a,b);
for(int i=20;i>=0;--i)
if(d[a]<=d[b]-(1<<i)) b=f[b][i];
if(a==b) return a;
for(int i=20;i>=0;--i)
if(f[a][i]!=f[b][i]) a=f[a][i],b=f[b][i];
return f[a][0];
}
int main() {
n=qread(),k=qread();
for(int i=1,u;i<=n;++i) {
a[i]=qread(),u=qread();
if(!u) {rt=i;continue;}
ct(u,i);ct(i,u);
}
dfs(rt,0);
for(int i=1;i<=n;++i) {
if(d[i]>b[a[i]]) {
c[a[i]]=i;
b[a[i]]=d[i];
}
}
for(int j=1;j<=20;++j)
for(int i=1;i<=n;++i)
f[i][j]=f[f[i][j-1]][j-1];
for(int i=1;i<=n;++i)
dis[a[i]]=max(b[a[i]]+d[i]-2*d[lca(c[a[i]],i)],dis[a[i]]);
for(int i=1;i<=k;++i)
printf("%d\n",dis[i]);
return 0;
}

洛谷P2971 牛的政治Cow Politics的更多相关文章

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

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

  2. 洛谷P1522 牛的旅行 Cow Tours

    ---恢复内容开始--- P1522 牛的旅行 Cow Tours189通过502提交题目提供者该用户不存在标签 图论 USACO难度 提高+/省选-提交该题 讨论 题解 记录 最新讨论 输出格式题目 ...

  3. 洛谷 P1522 牛的旅行 Cow Tours 题解

    P1522 牛的旅行 Cow Tours 题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不 ...

  4. 洛谷2971 [USACO10HOL]牛的政治Cow Politics

    原题链接 假设只有一个政党,那么这题就退化成求树的直径的问题了,所以我们可以从此联想至\(k\)个政党的情况. 先处理出每个政党的最大深度,然后枚举每个政党的其它点,通过\(LCA\)计算长度取\(\ ...

  5. 洛谷 - P3033 - 牛的障碍Cow Steeplechase - 二分图最大独立集

    https://www.luogu.org/fe/problem/P3033 二分图最大独立集 注意输入的时候控制x1,y1,x2,y2的相对大小. #include<bits/stdc++.h ...

  6. 洛谷 P1522 牛的旅行 Cow Tours

    题目链接:https://www.luogu.org/problem/P1522 思路:编号,然后跑floyd,这是很清楚的.然后记录每个点在这个联通块中的最远距离. 然后分连通块,枚举两个点(不属于 ...

  7. 洛谷 - P1522 - 牛的旅行 - Cow Tours - Floyd

    https://www.luogu.org/problem/P1522 好坑啊,居然还有直径不通过新边的数据,还好不是很多. 注意一定要等Floyd跑完之后再去找连通块的直径,不然一定是INF. #i ...

  8. [USACO10HOL]牛的政治Cow Politics

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

  9. 洛谷 P1522 牛的旅行 Cow Tours——暴力枚举+最短路

    先上一波题目  https://www.luogu.org/problem/P1522 这道题其实就是给你几个相互独立的连通图 问找一条新的路把其中的两个连通图连接起来后使得新的图中距离最远的两个点之 ...

随机推荐

  1. webpack笔记1

    1.设置多个入口起点 多用于提取公共类库 a.利用commonChunkPlugin const webpack= require('webpack'); const path = require(' ...

  2. Sprin Boot2.0之整合Mybatis整合分页插件

    pageHelper PageHelper 是一款好用的开源免费的 Mybatis 第三方物理分页插件 物理分页 支持常见的 12 种数据库.Oracle,MySql,MariaDB,SQLite,D ...

  3. 算法(Algorithms)第4版 练习 1.3.26

    方法实现: //1.3.26 /** * remove all of the nodes in the list that have key as its item field * * @param ...

  4. eclipse批量修改package、import中的包名

    问题:想把以前开发的包,用到新项目中,怎么操作呢? 解决方案: 把文件夹复制到新项目包中,同时需要更改很多package.import中的包名第一步:打开一个java文件,选中要替换的字段: 第二步: ...

  5. python- python内置模块 面向对象

    1.configparser模块 configparser用于处理特定格式的文件,其本质上是利用open来操作文件 # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2 ...

  6. VC++动态链接库(DLL)编程深入浅出:Q&A(原创)

    Q1:extern “C” 是做什么用的? A1:一种情况是多个文件中,变量声明或者函数声明,需要extern “C”,这种情况在这里不做讨论. 在dll工程中,被extern "C&quo ...

  7. POI2014

    ...一个shabi和一堆神题的故事 今天只写了两道 之后随缘更吧 啊 顺便 snake我是不会更的 bzoj3829 POI2014 Farmcraft mhy住在一棵有n个点的树的1号结点上,每个 ...

  8. Scrapy,终端startproject,显示错误TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

    F:\python_project\test>scrapy startproject spz Traceback (most recent call last): File "d:\p ...

  9. 四维偏序 CDQ套CDQ

    对CDQ深一步的理解 昨天做了一道CDQ,看了一堆CDQ可做的题,今天又做了一道四维偏序 感觉对CDQ的理解又深了一点,故来写一写现在自己对于CDQ的理解 CDQ其实就是实现了这样的一个问题的转化: ...

  10. CentOS 6.6 搭建Zabbix 3.0.3 过程

    分享CentOS 6.6下搭建Zabbix 3.0.3 的过程,希望都大家有所帮助. 环境安装 系统环境: # cat /etc/RedHat-release CentOS release 6.6 ( ...