[POI2011]Inspekcja

题目大意:

给你一棵\(n(n\le10^6)\)个点的树,\(s\)为起点。每次选择一个点作为目标点\(t_i\),沿最短路走到\(t_i\)再走回\(s\)(最后一次除外)。相邻两次行动不能经过相同的边。问将每一个点作为\(s\),是否存在一种方案使得除\(s\)外的所有结点都作为目标点被恰好访问一次,如果是,求最小路径和。

思路:

存在合法方案当且仅当去掉\(s\)后剩下的连通块中,最大的连通块大小不超过其余连通块大小之和。

这样的点一定是重心(反过来不一定),因此最多就不超过两个。

答案就是以这个点为根后所有点深度和×2-最大深度。

注意如果恰好有一个子树大小为\(\frac n2\),则最后删掉的深度一定在这棵子树中。

源代码:

#include<cstdio>
#include<cctype>
#include<vector>
#include<climits>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=1e6+1;
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
int n,size[N],max[N],dep[N],far[N];
void dfs1(const int &x,const int &par) {
size[x]=1;
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
dfs1(y,x);
size[x]+=size[y];
max[x]=std::max(max[x],size[y]);
}
max[x]=std::max(max[x],n-size[x]);
}
void dfs2(const int &x,const int &par) {
far[x]=0;
size[x]=1;
dep[x]=dep[par]+1;
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
dfs2(y,x);
size[x]+=size[y];
far[x]=std::max(far[x],far[y]+1);
}
}
int main() {
n=getint();
max[0]=n+1;
dep[0]=-1;
for(register int i=1;i<n;i++) {
add_edge(getint(),getint());
}
dfs1(1,0);
for(register int x=1;x<=n;x++) {
if(max[x]>n/2) {
puts("-1");
continue;
}
dfs2(x,0);
int max=0;
int64 sum=0;
for(register int i=1;i<=n;i++) {
sum+=dep[i];
max=std::max(max,dep[i]);
}
for(register unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(size[y]==n/2) max=far[y]+1;
}
printf("%lld\n",sum*2-max);
}
return 0;
}

[POI2011]Inspekcja的更多相关文章

  1. BZOJ2527: [Poi2011]Meteors

    补一发题解.. 整体二分这个东西,一开始感觉复杂度不是很靠谱的样子 问了po姐姐,说套主定理硬干.. #include<bits/stdc++.h> #define ll long lon ...

  2. BZOJ2276: [Poi2011]Temperature

    2276: [Poi2011]Temperature Time Limit: 20 Sec  Memory Limit: 32 MBSubmit: 293  Solved: 117[Submit][S ...

  3. BZOJ2213: [Poi2011]Difference

    2213: [Poi2011]Difference Time Limit: 10 Sec  Memory Limit: 32 MBSubmit: 343  Solved: 108[Submit][St ...

  4. BZOJ2212: [Poi2011]Tree Rotations

    2212: [Poi2011]Tree Rotations Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 391  Solved: 127[Submi ...

  5. BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )

    线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...

  6. bzoj 2217 [Poi2011]Lollipop 乱搞 贪心

    2217: [Poi2011]Lollipop Time Limit: 15 Sec  Memory Limit: 64 MBSec  Special JudgeSubmit: 383  Solved ...

  7. BZOJ_2529_[Poi2011]Sticks_贪心

    BZOJ_2529_[Poi2011]Sticks_贪心 Description Little Johnny was given a birthday present by his grandpare ...

  8. BZOJ_2212_[Poi2011]Tree Rotations_线段树合并

    BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...

  9. BZOJ_2527_[Poi2011]Meteors_整体二分

    BZOJ_2527_[Poi2011]Meteors_整体二分 Description Byteotian Interstellar Union (BIU) has recently discover ...

随机推荐

  1. 关于MySQL 8.0的几个重点【转】

    转自 关于MySQL .0的几个重点,都在这里 https://mp.weixin.qq.com/s/QUpk9uuS2JTli1GT6HuORA 一.关于MySQL Server的改进 1.1 re ...

  2. ulimit -n 修改

    Linux系统里打开文件描述符的最大值,一般缺省值是1024,对一台繁忙的服务器来说,这个值偏小,所以有必要重新设置linux系统里打开文件描述符的最大值.那么应该在哪里设置呢? [root@loca ...

  3. 【转】wpf中的xmlns命名空间为什么是一个网址,代表了什么意思

    wpf中的xmlns命名空间为什么是一个网址,代表了什么意思 http://blog.csdn.net/catshitone/article/details/71213371

  4. C#实现Zip压缩解压实例

    原文地址:https://www.cnblogs.com/GoCircle/p/6544678.html 本文只列举一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZi ...

  5. CentOS6.5环境使用keepalived实现nginx服务的高可用性及配置详解

    keepalived基础概念    Keepalived是一个基于VRRP协议来实现的WEB服务高可用方案,可以利用其来避免单点故障.一个WEB服务至少会有2台服务器运行Keepalived,一台为主 ...

  6. Node.js Error: listen EADDRNOTAVAIL

    1 前言 nodejs部署在云服务器,外网用域名加端口访问不进来,但在服务器本地用127.0.0.1加端口可以访问,并且端口已经放开,然后只能排查配置.此文章仅作为记录使用. 如果端口和另一个的端口一 ...

  7. 队列queue 代码

    import queue q=queue.Queue(3) #队列:先进先出 q.put(1) q.put(2) q.put(3) q.put(4) print(q.get()) print(q.ge ...

  8. Pycharm4.5注册码 激活

    name : newasp ===== LICENSE BEGIN ===== 09086-12042010 00001EBwqd8wkmP2FM34Z05iXch1Ak KI0bAod8jkIffy ...

  9. Java 清理和垃圾回收

    java.lang.ref.cleaner包 finalize()//该方法已过时,有风险,慎用 1.对象不可能被垃圾回收 2.垃圾回收并不等于"析构" 只有当垃圾回收发生时fin ...

  10. java根据word模板导出word文件

    1.word模板文件处理,如下图所示在word 文档中填值的地方写入占位变量 2.将word文档另存为xml文件.编辑如下图,找到填写的占位,修改为${bcrxm}格式 3.将文件后缀名改为.ftl文 ...