看到现有的一篇 DSU on tree 的题解复杂度假了,于是我来再写一篇。

首先重新梳理思路,维护每棵子树内深度为某个值的节点是否存在。

维护这个东西可以直接 DSU on tree 也就是把小的子树内的信息加入大的子树。

然后加入点是判断是否能和已经存在的点构成长度为 \(K\) 的路径。

举个例子,对于经过点 \(rt\) 的从 \(x\) 到 \(y\) 的路径,长度是 \(dep_x - dep_{rt} + dep_y - dep_{rt}\)。

令上式值为 \(K\) 再移项就可以知道新加入的点需要满足如何的限制才可以做贡献。

最后注意一个点,我们不能暴力清空数组,正确做法是把这个桶用哈希维护,然后直接 \(O(1)\) 清空哈希表。

另外说一个特殊的地方:这份代码为了省事将加点和贡献放在了一起,如果计算方案数就会计重,如果想避免就需要对于每个子树中的所有节点先计算贡献再加入桶

最后上代码:

#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
const int maxn = 1e4+114;
int depth[maxn],n,sz[maxn],big[maxn],L[maxn],R[maxn],Node[maxn],tot;
__gnu_pbds::gp_hash_table<int,int> cnt;
vector< pair<int,int> > edge[maxn];
int K;
bool flag;
inline void add(int u,int rt){
cnt[depth[u]]=1;
int v=K+depth[rt]*2-depth[u];
if(cnt[v]==1) flag=true;
}
inline void dfs1(int u,int fa,int w){
L[u]=++tot;
Node[tot]=u;
R[u]=L[u];
sz[u]=1;
depth[u]=depth[fa]+w;
for(pair<int,int> v:edge[u]){
if(v.first==fa) continue;
dfs1(v.first,u,v.second);
sz[u]+=sz[v.first];
if(sz[v.first]>sz[big[u]]){
big[u]=v.first;
}
R[u]=max(R[u],R[v.first]);
}
}
inline void dfs2(int u,int fa,bool keep){
for(pair<int,int> v:edge[u]){
if(v.first!=fa&&v.first!=big[u]) dfs2(v.first,u,false);
if(flag==true){
return ;
}
}
if(big[u]){
dfs2(big[u],u,true);
if(flag==true){
return ;
}
}
add(u,u);
for(pair<int,int> v:edge[u]){
if(v.first!=fa&&v.first!=big[u]){
for(int i=L[v.first];i<=R[v.first];i++){
add(Node[i],u);
if(flag==true){
return ;
}
}
}
}
if(keep==false){
cnt.clear();
}
}
int m;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<n;i++){
int u,v,w;
cin>>u>>v>>w;
edge[u].push_back(make_pair(v,w));
edge[v].push_back(make_pair(u,w));
}
dfs1(1,0,1);
while(m--){
cin>>K;
flag=false;
dfs2(1,0,true);
cnt.clear();
cout<<(flag==true?"AYE\n":"NAY\n");
}
}

P3806 题解的更多相关文章

  1. P3806 【模板】点分治1(题解)(点分治)

    P3806 [模板]点分治1(题解)(点分治) 洛谷题目传送门 #include<iostream> #include<cstdlib> #include<cstdio& ...

  2. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  3. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  4. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  5. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  6. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  7. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  8. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  9. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  10. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. SpringBoot 利用Timer 在指定时间2小时后执行任务

    /** * @Description * @Author songwp * @Date 2022/8/5 12:51 * @Version 1.0.0 **/ @Component public cl ...

  2. csapp-datalab(菜鸟小白版)

    第1题: /* * bitXor - x^y using only ~ and & * Example: bitXor(4, 5) = 1 * Legal ops: ~ & * Max ...

  3. WordPress函数小结

    1.body_class()函数 为了区分不同的页面,可以用WordPress的body_class()函数 可以在head.php中给body添加:<body <?php body_cl ...

  4. vue特殊atribute-is

    1.解决dom内模板限制 有些 HTML 元素,诸如 <ul>.<ol>.<table> 和 <select>,对于哪些元素可以出现在其内部是有严格限制 ...

  5. IceRPC之调用管道Invocation pipeline与传出请求Outgoing request->快乐的RPC

    作者引言 .Net 8.0 下的新RPC 很高兴啊,我们来到了IceRPC之调用管道 Invocation pipeline与传出请求 Outgoing request->快乐的RPC, 基础引 ...

  6. 防患未然 | AIRIOT城市管廊智能运维解决方案

      城市管廊构建复杂,管道内部传感器和附属设备居多,且近年来事故频发,地下空间属性人员进出管理不便,紧急情况应急调度措施有限.传统人工管理模式,运营成本高,且管理水平和质量也无法得到有利保障.因此在管 ...

  7. centos7了解

    A,B,C,D四个任务有依赖关系①init:总时间T1+T2+T3+T4+T5+T6+T7②upstart:总时间T1+T2+T3,启动速度加快,但是有依赖关系的服务还是必须先后启动.③systemd ...

  8. rocketmq 搭建配置

    broker组1: # NameServer地址 namesrvAddr=192.168.1.100: 9876;192.168.1.101: 9876 # 集群名称 brokerClusterNam ...

  9. 【Sqlserver】查看所有数据库的大小 创建日期 名称 版本级别 状态

    EXEC  sp_helpdb

  10. requests + tkinter 获取网页数据

    代码: from tkinter import * import requests window = Tk() window.geometry('500x350+500+100') window.ti ...