【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的大树的更多相关文章

  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. kuangbin带我飞QAQ 线段树

    1. HDU1166 裸线段树点修改 #include <iostream> #include <string.h> #include <cstdio> #incl ...

  2. angular7.X配置同时使用localhost和本机IP访问项目

    1.项目中找到此文件“node_modules/webpack-dev-server/lib/Server.js”,按照下图修改: 二.修改配置文件package.json,见下图: 三.npm st ...

  3. Redis源码解析:23sentinel(四)故障转移流程

    十:故障转移流程中的状态转换 当哨兵针对某个主节点进行故障转移时,该主节点的故障转移状态master->failover_state,要依次经历下面六个状态: SENTINEL_FAILOVER ...

  4. c++设计模式:策略模式

    1.主要思想:例如针对不同的算法,创建不同的类. #include <iostream> using namespace std; // The abstract strategy cla ...

  5. ssh 连接vps

    默认端口22 ssh root@194.10.10.10 特定端口xxx 上面命令后面加上 "-p xxx" 就可以了

  6. 索尼微单cmos坏点屏蔽

    偶然发现相机有坏点了,果断去售后换了一台回来.然后试机,吐血 要不是我旧机器满是岁月的痕迹,我真以为原封给我退回来了 这样不行啊,难道再换一个回来?毕竟是脸皮薄的人.不好意思操作啊. 于是上网找找,果 ...

  7. python实例 条件和循环语句

    #! /usr/bin/python #条件和循环语句 x=int(input("Please enter an integer:")) if x<0:     x=0    ...

  8. 2016计蒜之道复赛A 百度地图的实时路况

    百度地图的实时路况功能相当强大,能方便出行的人们避开拥堵路段.一个地区的交通便捷程度就决定了该地区的拥堵情况.假设一个地区有 nnn 个观测点,编号从 111 到 nnn.定义 d(u,v,w)d(u ...

  9. SourceTree windows免注册免登陆使用方法

    问题描述: 安装SourceTree后,首次使用时,需要登录账号:但我们在注册或登录时,可能根本无法打开网页,导致不能进入. 如下截图: 解决方法: 在目录C:\Users\XXXXX\AppData ...

  10. 将自己的代码托管到github - 秦时明月 - CSDN博客

    步骤: 1.建立自己的github 2.安装github客户端,并配置身份 3.建立github项目 4.将github项目库下载到本地 5.提交本地代码到github 详细操作: 1.github网 ...