【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。。。
- #include<bits/stdc++.h>
- #define reg register int
- #define il inline
- #define fi first
- #define se second
- #define mk(a,b) make_pair(a,b)
- #define numb (ch^'0')
- #define pb push_back
- #define solid const auto &
- #define enter cout<<endl
- #define pii pair<int,int>
- using namespace std;
- typedef long long ll;
- template<class T>il void rd(T &x){
- char ch;x=;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
- for(x=numb;isdigit(ch=getchar());x=x*+numb);(fl==true)&&(x=-x);}
- template<class T>il void output(T x){if(x/)output(x/);putchar(x%+'');}
- template<class T>il void ot(T x){if(x<) putchar('-'),x=-x;output(x);putchar(' ');}
- template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');}
- namespace Modulo{
- const int mod=;
- int ad(int x,int y){return (x+y)>=mod?x+y-mod:x+y;}
- void inc(int &x,int y){x=ad(x,y);}
- int mul(int x,int y){return (ll)x*y%mod;}
- void inc2(int &x,int y){x=mul(x,y);}
- int qm(int x,int y=mod-){int ret=;while(y){if(y&) ret=mul(x,ret);x=mul(x,x);y>>=;}return ret;}
- }
- //using namespace Modulo;
- namespace Miracle{
- const int N=1e5+;
- const int inf=0x3f3f3f3f;
- int n,m;
- int b[N];
- struct node{
- int nxt,to;
- int val;
- }e[*N];
- int hd[N],cnt;
- void add(int x,int y,int z){
- e[++cnt].nxt=hd[x];
- e[cnt].to=y;e[cnt].val=z;
- hd[x]=cnt;
- }
- struct po{
- int mx,cnt,pos;
- po(){mx=-inf,cnt=,pos=;}//warning!! -inf
- po(int v,int c,int p){mx=v;cnt=c;pos=p;}
- po friend operator +(po a,po b){
- if(a.mx==b.mx) return po(a.mx,a.cnt+b.cnt,a.pos);
- else if(a.mx>b.mx) return a;
- else return b;
- }
- }pr[N],bc[N],f[N],g[N];
- int sta[N],top;
- int fa[N][];
- int dep[N],vf[N];
- void pre(int x){
- dep[x]=dep[fa[x][]]+;
- for(reg i=hd[x];i;i=e[i].nxt){
- int y=e[i].to;
- if(y==fa[x][]) continue;
- fa[y][]=x;
- pre(y);
- }
- }
- void dfs(int x){
- int st=top;
- if(b[x]) f[x]=po(,,x);
- for(reg i=hd[x];i;i=e[i].nxt){
- int y=e[i].to;
- if(y==fa[x][]) continue;
- vf[y]=e[i].val;
- dfs(y);
- sta[++top]=y;
- pr[y]=f[x];
- f[x]=f[x]+po(f[y].mx+e[i].val,,f[y].pos);
- }
- if(f[x].mx<){
- f[x].cnt=;f[x].pos=;
- }else{
- if(f[x].cnt>) f[x].cnt=,f[x].pos=x;
- }
- po now;
- while(top!=st){
- bc[sta[top]]=now;
- now=now+po(f[sta[top]].mx+vf[sta[top]],,f[sta[top]].pos);
- --top;
- }
- }
- void gf(int x){
- if(fa[x][]){
- int pa=fa[x][];
- g[x]=g[pa]+pr[x]+bc[x];
- g[x].mx+=vf[x];
- if(g[x].mx<){
- g[x].cnt=;g[x].pos=;
- }else{
- if(g[x].cnt>) g[x].cnt=,g[x].pos=pa;
- }
- }
- for(reg i=hd[x];i;i=e[i].nxt){
- int y=e[i].to;
- if(y==fa[x][]) continue;
- gf(y);
- }
- }
- int lca(int x,int y){
- if(dep[x]<dep[y]) swap(x,y);
- for(reg j=;j>=;--j){
- if(dep[fa[x][j]]>=dep[y]) x=fa[x][j];
- }
- if(x==y) return x;
- for(reg j=;j>=;--j){
- if(fa[x][j]!=fa[y][j]) x=fa[x][j],y=fa[y][j];
- }
- return fa[x][];
- }
- int tag[N];
- int ans,tot;
- void fin(int x){
- for(reg i=hd[x];i;i=e[i].nxt){
- int y=e[i].to;
- if(y==fa[x][]) continue;
- fin(y);
- tag[x]+=tag[y];
- }
- if(!b[x]){
- if(tag[x]>ans) {
- ans=tag[x];tot=;
- }else if(tag[x]==ans){
- ++tot;
- }
- }
- }
- int main(){
- rd(n);rd(m);
- for(reg i=;i<=m;++i) {
- int x;rd(x);b[x]=;
- }
- int x,y,z;
- for(reg i=;i<n;++i){
- rd(x);rd(y);rd(z);
- add(x,y,z);add(y,x,z);
- }
- pre();
- dfs();
- gf();
- for(reg j=;j<=;++j){
- for(reg i=;i<=n;++i){
- fa[i][j]=fa[fa[i][j-]][j-];
- // cout<<" fa "<<i<<" "<<j<<" : "<<fa[i][j]<<endl;
- }
- }
- for(reg i=;i<=n;++i){
- if(b[i]){
- int x=i;
- po now=f[x]+g[x];
- if(now.cnt==){
- // cout<<" tag? "<<x<<" "<<now.pos<<endl;
- int anc=lca(x,now.pos);
- // cout<<" anc "<<anc<<endl;
- ++tag[x];++tag[now.pos];
- --tag[anc];--tag[fa[anc][]];
- }
- }
- }
- ans=-inf;
- fin();
- ot(ans);ot(tot);
- return ;
- }
- }
- signed main(){
- Miracle::main();
- return ;
- }
- /*
- Author: *Miracle*
- */
【UTR #1】ydc的大树的更多相关文章
- UOJ #11. 【UTR #1】ydc的大树
题目描述: ydc有一棵n个结点的黑白相间的大树,从1到n编号. 这棵黑白树中有m个黑点,其它都是白点. 对于一个黑点我们定义他的好朋友为离他最远的黑点.如果有多个黑点离它最远那么都是它的好朋友.两点 ...
- UOJ #11 - 【UTR #1】ydc的大树(换根 dp)
题面传送门 Emmm--这题似乎做法挺多的,那就提供一个想起来写起来都不太困难的做法吧. 首先不难想到一个时间复杂度 \(\mathcal O(n^2)\) 的做法:对于每个黑点我们以它为根求出离它距 ...
- uoj problem 11 ydc的大树
题目大意: 给定一颗黑白树.允许删除一个白点.最大化删点后无法与删点前距自己最远的黑点连通的黑点个数.并求出方案数. 题解: 这道题很棒棒啊. 一开始想了一个做法,要用LCT去搞,特别麻烦而且还是\( ...
- Codeforces 468D Tree
题目 给出一棵带边权的树,求一个排列\(p\),使得\(\sum_{i=1}^{n}{dis(i, p_i)}\)的值最大,其中\(dis(v, u)\)表示\(v\)到\(u\)的距离. 算法 这题 ...
- Codeforces-348E Pilgrims
#4342. CF348 Pilgrims 此题同UOJ#11 ydc的大树 Online Judge:Bzoj-4342,Codeforces-348E,Luogu-CF348E,Uoj-#11 L ...
- uoj #9. 【UTR #1】vfk的数据 水题
#9. [UTR #1]vfk的数据 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/9 Description ...
- UOJ #278. 【UTR #2】题目排列顺序(排序水题)
#278. [UTR #2]题目排列顺序 丢个传送门:http://uoj.ac/problem/278 描述 “又要出题了.” 宇宙出题中心主任 —— 吉米多出题斯基,坐在办公桌前策划即将到来的 U ...
- [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 ...
- CDS & ORF & 启动子 & 终止子 & 转录因子 & 基因结构 & UTR
ORF和CDS的区别 ORF的英文展开是open reading frame(开放阅读框). CDS的英文展开是coding sequences (编码区). CDS:DNA转录成mRNA,mRNA经 ...
随机推荐
- Luogu P1963 [NOI2009]变换序列(二分图匹配)
P1963 [NOI2009]变换序列 题意 题目描述 对于\(N\)个整数\(0,1, \cdots ,N-1\),一个变换序列\(T\)可以将\(i\)变成\(T_i\),其中\(T_i \in ...
- 【vue】openshopping-vue
这是一个基于Vue实现开箱即用H5移动端商城的单页应用 作者的开源地址是:https://github.com/yrinleung/openshopping-vue 我们一起来欣赏页面吧 看看代码有什 ...
- ECS应用管理最佳实践
前言 即使在CloudNative发展如火如荼的当下,ECS应用(直接将应用部署在ECS上,不使用容器)仍然占了相当大的比重,原因主要在于相对容器化应用,ECS应用由于不需要容器的运行时环境和类似K8 ...
- import schedule ImportError: No module named schedule
安装pip sudo apt-get install python-pip 安装schedule模块 pip install schedule PS: 如果已经安装pip,可能出现以下问题,按照提示重 ...
- 中断描述符表 IDT
保护模式下三个重要的系统表——GDT.LDT和IDT 这里主要是解释中断描述符表 中断描述符表IDT将每个异常或中断向量分别与它们的处理过程联系起来.与GDT和LDT表类似,IDT也是由8字节长描述符 ...
- 直接在安装了redis的Linux机器上操作redis数据存储类型--List类型
一.概述: 在Redis中,List类型是按照插入顺序排序的字符串链表.和数据结构中的普通链表一样,我们可以在其头部(left)和尾部(right)添加新的元素.在插入时,如果该键并不存在,Redis ...
- Eclipse 遇到的问题和快捷键记录
一.the user operation is waiting: 选择菜单栏的"Project",然后把菜单栏中"Build Automatically"前面的 ...
- 获得浏览器User-agent的方法
在浏览器的地址栏输入(不是全部都能用) javascript:alert(navigator.userAgent); 或者网页中 alert(navigator.userAgent) 或者后台中 St ...
- JSP内置对象解析
out对象:(PrintWriter类的实例) 用来向客户端输出信息,除了输出各种信息外还负责对缓冲区进行管理: 主要方法: print / println void 输出数据 newLine() v ...
- 关于python的列表操作(二):排序,统计
# 列表操作 num_list = [2, 5, 8, 6, 7, 9, 5, 7] # 升序 num_list.sort() print(num_list) # 降序 num_list.sort(r ...