【UTR #1】ydc的大树

全网唯一一篇题解我看不懂

所以说一下我的O(nlogn)做法:

以1号点为根节点

一个黑点如果有多个相邻的节点出去都能找到最远的黑点,那么这个黑点就是无敌的

所以考虑每个黑点x的最远距离和最远点是否仅在一个“方向”

然后这个方向的一些连续白点割掉可以使得x不高兴

1.如果都在一个方向,假设是x的子树,那就是这个子树最远黑点们的lca到x路径上的任意白点割掉,都可以使得x不高兴

2.如果都在往父亲的方向,找到最浅的点p,使得每个最远黑点到x的路径都经过p,p到x的路径上的任意白点割掉,都可以使得x不高兴

树形DP即可。

struct,记录最远距离、最远的方向个数、决策位置(1的lca或者是2的p)

转移较麻烦

树上差分打标记即可。

求lca,所以O(nlogn)

写了四个dfs。。。

  1. #include<bits/stdc++.h>
  2. #define reg register int
  3. #define il inline
  4. #define fi first
  5. #define se second
  6. #define mk(a,b) make_pair(a,b)
  7. #define numb (ch^'0')
  8. #define pb push_back
  9. #define solid const auto &
  10. #define enter cout<<endl
  11. #define pii pair<int,int>
  12. using namespace std;
  13. typedef long long ll;
  14. template<class T>il void rd(T &x){
  15. char ch;x=;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
  16. for(x=numb;isdigit(ch=getchar());x=x*+numb);(fl==true)&&(x=-x);}
  17. template<class T>il void output(T x){if(x/)output(x/);putchar(x%+'');}
  18. template<class T>il void ot(T x){if(x<) putchar('-'),x=-x;output(x);putchar(' ');}
  19. template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');}
  20. namespace Modulo{
  21. const int mod=;
  22. int ad(int x,int y){return (x+y)>=mod?x+y-mod:x+y;}
  23. void inc(int &x,int y){x=ad(x,y);}
  24. int mul(int x,int y){return (ll)x*y%mod;}
  25. void inc2(int &x,int y){x=mul(x,y);}
  26. int qm(int x,int y=mod-){int ret=;while(y){if(y&) ret=mul(x,ret);x=mul(x,x);y>>=;}return ret;}
  27. }
  28. //using namespace Modulo;
  29. namespace Miracle{
  30. const int N=1e5+;
  31. const int inf=0x3f3f3f3f;
  32. int n,m;
  33. int b[N];
  34. struct node{
  35. int nxt,to;
  36. int val;
  37. }e[*N];
  38. int hd[N],cnt;
  39. void add(int x,int y,int z){
  40. e[++cnt].nxt=hd[x];
  41. e[cnt].to=y;e[cnt].val=z;
  42. hd[x]=cnt;
  43. }
  44. struct po{
  45. int mx,cnt,pos;
  46. po(){mx=-inf,cnt=,pos=;}//warning!! -inf
  47. po(int v,int c,int p){mx=v;cnt=c;pos=p;}
  48. po friend operator +(po a,po b){
  49. if(a.mx==b.mx) return po(a.mx,a.cnt+b.cnt,a.pos);
  50. else if(a.mx>b.mx) return a;
  51. else return b;
  52. }
  53. }pr[N],bc[N],f[N],g[N];
  54. int sta[N],top;
  55. int fa[N][];
  56. int dep[N],vf[N];
  57. void pre(int x){
  58. dep[x]=dep[fa[x][]]+;
  59. for(reg i=hd[x];i;i=e[i].nxt){
  60. int y=e[i].to;
  61. if(y==fa[x][]) continue;
  62. fa[y][]=x;
  63. pre(y);
  64. }
  65. }
  66. void dfs(int x){
  67. int st=top;
  68. if(b[x]) f[x]=po(,,x);
  69. for(reg i=hd[x];i;i=e[i].nxt){
  70. int y=e[i].to;
  71. if(y==fa[x][]) continue;
  72. vf[y]=e[i].val;
  73. dfs(y);
  74. sta[++top]=y;
  75. pr[y]=f[x];
  76. f[x]=f[x]+po(f[y].mx+e[i].val,,f[y].pos);
  77. }
  78. if(f[x].mx<){
  79. f[x].cnt=;f[x].pos=;
  80. }else{
  81. if(f[x].cnt>) f[x].cnt=,f[x].pos=x;
  82. }
  83. po now;
  84. while(top!=st){
  85. bc[sta[top]]=now;
  86. now=now+po(f[sta[top]].mx+vf[sta[top]],,f[sta[top]].pos);
  87. --top;
  88. }
  89. }
  90. void gf(int x){
  91. if(fa[x][]){
  92. int pa=fa[x][];
  93. g[x]=g[pa]+pr[x]+bc[x];
  94. g[x].mx+=vf[x];
  95. if(g[x].mx<){
  96. g[x].cnt=;g[x].pos=;
  97. }else{
  98. if(g[x].cnt>) g[x].cnt=,g[x].pos=pa;
  99. }
  100. }
  101. for(reg i=hd[x];i;i=e[i].nxt){
  102. int y=e[i].to;
  103. if(y==fa[x][]) continue;
  104. gf(y);
  105. }
  106. }
  107. int lca(int x,int y){
  108. if(dep[x]<dep[y]) swap(x,y);
  109. for(reg j=;j>=;--j){
  110. if(dep[fa[x][j]]>=dep[y]) x=fa[x][j];
  111. }
  112. if(x==y) return x;
  113. for(reg j=;j>=;--j){
  114. if(fa[x][j]!=fa[y][j]) x=fa[x][j],y=fa[y][j];
  115. }
  116. return fa[x][];
  117. }
  118. int tag[N];
  119. int ans,tot;
  120. void fin(int x){
  121. for(reg i=hd[x];i;i=e[i].nxt){
  122. int y=e[i].to;
  123. if(y==fa[x][]) continue;
  124. fin(y);
  125. tag[x]+=tag[y];
  126. }
  127. if(!b[x]){
  128. if(tag[x]>ans) {
  129. ans=tag[x];tot=;
  130. }else if(tag[x]==ans){
  131. ++tot;
  132. }
  133. }
  134. }
  135. int main(){
  136. rd(n);rd(m);
  137. for(reg i=;i<=m;++i) {
  138. int x;rd(x);b[x]=;
  139. }
  140. int x,y,z;
  141. for(reg i=;i<n;++i){
  142. rd(x);rd(y);rd(z);
  143. add(x,y,z);add(y,x,z);
  144. }
  145. pre();
  146. dfs();
  147. gf();
  148. for(reg j=;j<=;++j){
  149. for(reg i=;i<=n;++i){
  150. fa[i][j]=fa[fa[i][j-]][j-];
  151. // cout<<" fa "<<i<<" "<<j<<" : "<<fa[i][j]<<endl;
  152. }
  153. }
  154. for(reg i=;i<=n;++i){
  155. if(b[i]){
  156. int x=i;
  157. po now=f[x]+g[x];
  158. if(now.cnt==){
  159. // cout<<" tag? "<<x<<" "<<now.pos<<endl;
  160. int anc=lca(x,now.pos);
  161. // cout<<" anc "<<anc<<endl;
  162. ++tag[x];++tag[now.pos];
  163. --tag[anc];--tag[fa[anc][]];
  164. }
  165. }
  166. }
  167. ans=-inf;
  168. fin();
  169. ot(ans);ot(tot);
  170. return ;
  171. }
  172.  
  173. }
  174. signed main(){
  175. Miracle::main();
  176. return ;
  177. }
  178.  
  179. /*
  180. Author: *Miracle*
  181. */

【UTR #1】ydc的大树的更多相关文章

  1. UOJ #11. 【UTR #1】ydc的大树

    题目描述: ydc有一棵n个结点的黑白相间的大树,从1到n编号. 这棵黑白树中有m个黑点,其它都是白点. 对于一个黑点我们定义他的好朋友为离他最远的黑点.如果有多个黑点离它最远那么都是它的好朋友.两点 ...

  2. UOJ #11 - 【UTR #1】ydc的大树(换根 dp)

    题面传送门 Emmm--这题似乎做法挺多的,那就提供一个想起来写起来都不太困难的做法吧. 首先不难想到一个时间复杂度 \(\mathcal O(n^2)\) 的做法:对于每个黑点我们以它为根求出离它距 ...

  3. uoj problem 11 ydc的大树

    题目大意: 给定一颗黑白树.允许删除一个白点.最大化删点后无法与删点前距自己最远的黑点连通的黑点个数.并求出方案数. 题解: 这道题很棒棒啊. 一开始想了一个做法,要用LCT去搞,特别麻烦而且还是\( ...

  4. Codeforces 468D Tree

    题目 给出一棵带边权的树,求一个排列\(p\),使得\(\sum_{i=1}^{n}{dis(i, p_i)}\)的值最大,其中\(dis(v, u)\)表示\(v\)到\(u\)的距离. 算法 这题 ...

  5. Codeforces-348E Pilgrims

    #4342. CF348 Pilgrims 此题同UOJ#11 ydc的大树 Online Judge:Bzoj-4342,Codeforces-348E,Luogu-CF348E,Uoj-#11 L ...

  6. uoj #9. 【UTR #1】vfk的数据 水题

    #9. [UTR #1]vfk的数据 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/9 Description ...

  7. UOJ #278. 【UTR #2】题目排列顺序(排序水题)

    #278. [UTR #2]题目排列顺序 丢个传送门:http://uoj.ac/problem/278 描述 “又要出题了.” 宇宙出题中心主任 —— 吉米多出题斯基,坐在办公桌前策划即将到来的 U ...

  8. [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  9. CDS & ORF & 启动子 & 终止子 & 转录因子 & 基因结构 & UTR

    ORF和CDS的区别 ORF的英文展开是open reading frame(开放阅读框). CDS的英文展开是coding sequences (编码区). CDS:DNA转录成mRNA,mRNA经 ...

随机推荐

  1. Luogu P1963 [NOI2009]变换序列(二分图匹配)

    P1963 [NOI2009]变换序列 题意 题目描述 对于\(N\)个整数\(0,1, \cdots ,N-1\),一个变换序列\(T\)可以将\(i\)变成\(T_i\),其中\(T_i \in ...

  2. 【vue】openshopping-vue

    这是一个基于Vue实现开箱即用H5移动端商城的单页应用 作者的开源地址是:https://github.com/yrinleung/openshopping-vue 我们一起来欣赏页面吧 看看代码有什 ...

  3. ECS应用管理最佳实践

    前言 即使在CloudNative发展如火如荼的当下,ECS应用(直接将应用部署在ECS上,不使用容器)仍然占了相当大的比重,原因主要在于相对容器化应用,ECS应用由于不需要容器的运行时环境和类似K8 ...

  4. import schedule ImportError: No module named schedule

    安装pip sudo apt-get install python-pip 安装schedule模块 pip install schedule PS: 如果已经安装pip,可能出现以下问题,按照提示重 ...

  5. 中断描述符表 IDT

    保护模式下三个重要的系统表——GDT.LDT和IDT 这里主要是解释中断描述符表 中断描述符表IDT将每个异常或中断向量分别与它们的处理过程联系起来.与GDT和LDT表类似,IDT也是由8字节长描述符 ...

  6. 直接在安装了redis的Linux机器上操作redis数据存储类型--List类型

    一.概述: 在Redis中,List类型是按照插入顺序排序的字符串链表.和数据结构中的普通链表一样,我们可以在其头部(left)和尾部(right)添加新的元素.在插入时,如果该键并不存在,Redis ...

  7. Eclipse 遇到的问题和快捷键记录

    一.the user operation is waiting: 选择菜单栏的"Project",然后把菜单栏中"Build Automatically"前面的 ...

  8. 获得浏览器User-agent的方法

    在浏览器的地址栏输入(不是全部都能用) javascript:alert(navigator.userAgent); 或者网页中 alert(navigator.userAgent) 或者后台中 St ...

  9. JSP内置对象解析

    out对象:(PrintWriter类的实例) 用来向客户端输出信息,除了输出各种信息外还负责对缓冲区进行管理: 主要方法: print / println void 输出数据 newLine() v ...

  10. 关于python的列表操作(二):排序,统计

    # 列表操作 num_list = [2, 5, 8, 6, 7, 9, 5, 7] # 升序 num_list.sort() print(num_list) # 降序 num_list.sort(r ...