New Barns

时间限制: 1 Sec  内存限制: 128 MB

题目描述

Farmer John notices that his cows tend to get into arguments if they are packed too closely together, so he wants to open a series of new barns to help spread them out.
Whenever FJ constructs a new barn, he
connects it with at most one bidirectional pathway to an existing barn.
In order to make sure his cows are spread sufficiently far apart, he
sometimes wants to determine the distance from a certain barn to the
farthest possible barn reachable from it (the distance between two barns
is the number of paths one must traverse to go from one barn to the
other).

FJ will give a total of Q (1≤Q≤105)
queries, each either of the form "build" or "distance". For a build
query, FJ builds a barn and links it with at most one previously built
barn. For a distance query, FJ asks you the distance from a certain barn
to the farthest barn reachable from it via a series of pathways. It is
guaranteed that the queried barn has already been built. Please help FJ
answer all of these queries.

输入

The
first line contains the integer Q. Each of the next Q lines contains a
query. Each query is of the form "B p" or "Q k", respectively telling
you to build a barn and connect it with barn pp, or give the farthest
distance, as defined, from barn k. If p=−1, then the new barn will be
connected to no other barn. Otherwise, p is the index of a barn that has
already been built. The barn indices start from 1, so the first barn
built is barn 1, the second is barn 2, and so on.

输出

Please
write one line of output for each distance query. Note that a barn
which is connected to no other barns has farthest distance 0.

样例输入

  1. 7
  2. B -1
  3. Q 1
  4. B 1
  5. B 2
  6. Q 3
  7. B 2
  8. Q 2

样例输出

  1. 0
  2. 2
  3. 1

提示

The example input corresponds to this network of barns:
  (1) 
    \   
     (2)---(4)
    /
  (3)
In query 1, we build barn number 1. In query 2, we ask for the distance
of 1 to the farthest connected barn. Since barn 1 is connected to no
other barns, the answer is 0. In query 3, we build barn number 2 and
connect it to barn 1. In query 4, we build barn number 3 and connect it
to barn 2. In query 5, we ask for the distance of 3 to the farthest
connected barn. In this case, the farthest is barn 1, which is 2 units
away. In query 6, we build barn number 4 and connect it to barn 2. In
query 7, we ask for the distance of 2 to the farthest connected barn.
All three barns 1, 3, 4 are the same distance away, which is 1, so this
is our answer.

分析:题意:给定一个森林,在建森林的过程中有一些询问,距离某个点最远的点的距离;

   考虑分治,对于每个点的祖先,要么经过祖先,要么不经过;

   如果经过,考虑维护这个祖先的最大的两个点的深度,这两个点不在同一棵子树且已经被标记;更新答案分在不在同一子树里即可;

   如果不经过,则可以递归到子树,对于分治来说,维护重心,每个点有log个祖先;

   注意如果两个点被标记则lca也被标记过;

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=1e5+,mod=1e9+,inf=0x3f3f3f3f;
  4. int n,m,k,t,rt,a[maxn],sz[maxn],all;
  5. bool vis[maxn];
  6. vector<int>e[maxn];
  7. struct node
  8. {
  9. int mx1,mx2,mxid;
  10. vector<pair<int,int> >anc;
  11. }p[maxn];
  12. int getroot(int x,int y=)
  13. {
  14. for(int i=;i<e[x].size();i++)
  15. {
  16. int z=e[x][i];
  17. if(z==y||vis[z])continue;
  18. if(sz[z]*>=all)return getroot(z,x);
  19. }
  20. return x;
  21. }
  22. void getdep(int x,int y,int dep)
  23. {
  24. p[x].anc.push_back({rt,dep});
  25. for(int i=;i<e[x].size();i++)
  26. {
  27. int z=e[x][i];
  28. if(z==y||vis[z])continue;
  29. getdep(z,x,dep+);
  30. }
  31. }
  32. void getsz(int x,int y=)
  33. {
  34. sz[x]=;
  35. for(int i=;i<e[x].size();i++)
  36. {
  37. int z=e[x][i];
  38. if(z==y||vis[z])continue;
  39. getsz(z,x);
  40. sz[x]+=sz[z];
  41. }
  42. }
  43. void dfs(int x)
  44. {
  45. getsz(x);
  46. all=sz[x];
  47. rt=getroot(x);
  48. getdep(rt,,);
  49. vis[rt]=true;
  50. for(int i=;i<e[rt].size();i++)
  51. {
  52. int z=e[rt][i];
  53. if(!vis[z])dfs(z);
  54. }
  55. }
  56. int main()
  57. {
  58. int i,j;
  59. scanf("%d",&m);
  60. int pos=;
  61. for(i=;i<=m;i++)
  62. {
  63. char str[];
  64. scanf("%s%d",str,&n);
  65. if(str[]=='B')
  66. {
  67. a[i]=++pos;
  68. if(n==-)continue;
  69. else e[n].push_back(pos),e[pos].push_back(n);
  70. }
  71. else a[i]=-n;
  72. }
  73. for(i=;i<=pos;i++)if(!vis[i])dfs(i);
  74. pos=;
  75. for(i=;i<=m;i++)
  76. {
  77. int last=-;
  78. if(a[i]>)
  79. {
  80. ++pos;
  81. for(j=p[pos].anc.size()-;j>=;j--)
  82. {
  83. int fa=p[pos].anc[j].first,w=p[pos].anc[j].second;
  84. if(p[fa].mx1<=w)
  85. {
  86. if(p[fa].mxid!=last)p[fa].mx2=p[fa].mx1;
  87. p[fa].mx1=w;
  88. p[fa].mxid=last;
  89. }
  90. else if(p[fa].mx2<=w)
  91. {
  92. if(last!=p[fa].mxid)p[fa].mx2=w;
  93. }
  94. last=fa;
  95. }
  96. }
  97. else
  98. {
  99. int now=-a[i];
  100. int ret=p[now].mx1;
  101. for(j=p[now].anc.size()-;j>=;j--)
  102. {
  103. int fa=p[now].anc[j].first,w=p[now].anc[j].second;
  104. if(fa>pos)
  105. {
  106. last=fa;
  107. continue;
  108. }
  109. if(last==p[fa].mxid)ret=max(ret,w+p[fa].mx2);
  110. else ret=max(ret,w+p[fa].mx1);
  111. last=fa;
  112. }
  113. printf("%d\n",ret);
  114. }
  115. }
  116. return ;
  117. }

New Barns的更多相关文章

  1. P4271 [USACO18FEB]New Barns

    题目 P4271 [USACO18FEB]New Barns 做法 这题很长见识啊!! 知识点:两棵树\((A,B)\)联通后,新树的径端点为\(A\)的径端点与\(B\)的径端点的两点 不断加边,那 ...

  2. 题解【[USACO18FEB]New Barns 】

    浅谈一下对于这题做完之后的感受(不看题解也是敲不出来啊qwq--) 题意翻译 Farmer John注意到他的奶牛们如果被关得太紧就容易吵架,所以他想开放一些新的牛棚来分散她们. 每当FJ建造一个新牛 ...

  3. [usaco18Feb] New Barns

    题意 每次新建一个节点,并与一个已知节点连边.(或者不连).多次询问以某个已知点点出发的最远路径长度. 分析 显然,在任何时候图都是一个森林.由树的直径算法可知,与某点最远距的点必然是树的直径的一段. ...

  4. [Usaco2018 Feb] New Barns

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5192 [算法] 维护树的直径,在树上离一个点最远的点一定是一条直径的端点.     ...

  5. bzoj5192: [Usaco2018 Feb]New Barns

    不想写看zory大佬 #include<cstdio> #include<iostream> #include<cstring> #include<cstdl ...

  6. Luogu P4271 [USACO18FEB]New Barns P

    题意 给一个一开始没有点的图,有 \(q\) 次操作,每次为加点连边或者查询一个点到连通块内所有点的距离最大值. \(\texttt{Data Range}:1\leq q\leq 10^5\) 题解 ...

  7. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  8. 续并查集学习笔记——Closing the farm题解

    在很多时候,并查集并不是一个完整的解题方法,而是一种思路. 通过以下题目来体会并查集逆向运用的思想. Description Farmer John and his cows are planning ...

  9. 【BZOJ-3697&3127】采药人的路径&YinandYang 点分治 + 乱搞

    3697: 采药人的路径 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 681  Solved: 246[Submit][Status][Discus ...

随机推荐

  1. POJ 2452 Sticks Problem (暴力或者rmq+二分)

    题意:给你一组数a[n],求满足a[i] < a[k] < a[j] (i <= k <= j)的最大的 j - i . 析:在比赛时,我是暴力做的,虽然错了好多次,后来说理解 ...

  2. Rails5 Model Document

    创建: 2017/06/09 更新: 2017/06/21 更新: 2017/06/23 对待未完成的追加# TODO: 更新: 2017/06/29 修正文件名db/seed.rb ---> ...

  3. 无线网络发射选址 2014年NOIP全国联赛提高组(二维前缀和)

    P2038 无线网络发射器选址 题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南 ...

  4. noip2002矩阵覆盖(搜索)

    矩阵覆盖 题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4(0,7),见 ...

  5. ionic之AngularJS扩展动态组件

    目录: 1. 模态对话框 : $ionicModal 2. 上拉菜单 : $ionicActionSheet 3. 弹出框 : $ionicPopup 4. 浮动框 : $ionicPopover 5 ...

  6. MySQL-ProxySQL中间件(二)| Admin Schemas介绍

    目录     MySQL-ProxySQL中间件(一)| ProxySQL基本概念: https://www.cnblogs.com/SQLServer2012/p/10972593.html     ...

  7. spring cloud config搭建说明例子(四)-补充配置文件

    服务端 ConfigServer pom.xml <dependency> <groupId>org.springframework.cloud</groupId> ...

  8. Linux命令(007) -- systemctl

    systemctl命令是系统服务管理指令,它实际上是将service和chkconfig两个命令组合到一起. 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 httpd ...

  9. NodeJs学习记录(一)初步学习,杂乱备忘

    2016/12/26 星期一 1.在win7下安装了NodeJs 1)进入官网 https://nodejs.org/en/download/,下载对应的安装包,我目前下载的是node-v6.2.0- ...

  10. Verification Mind Games---how to think like a verifier像验证工程师一样思考

    1. 有效的验证需要验证工程师使用不同于设计者的思维方式思考问题.具体来说,验证更加关心在严格遵循协议的基础上发现设计里面的bug,搜索corner cases,对设计的不一致要保持零容忍的态度. m ...