P3806 离线多次询问 树上距离为K的点对是否存在 点分治
询问树上距离为k的点对是否存在
直接n^2暴力处理点对 桶排记录 可以过
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + ;
const int MAXM = 1e5 + ;
int to[MAXM << ], nxt[MAXM << ], Head[MAXN], ed = ;
int cost[MAXM << ];
int ok[];
inline void addedge(int u, int v, int c) {
to[++ed] = v;
cost[ed] = c;
nxt[ed] = Head[u];
Head[u] = ed;
}
inline void ADD(int u, int v, int c) {
addedge(u, v, c);
addedge(v, u, c);
}
int n, m, k;
int sz[MAXN], f[MAXN], dep[MAXN], sumsz, root;
bool vis[MAXN];
int o[MAXN], cnt;
void getroot(int x, int fa) {
sz[x] = ;
f[x] = ;
for (int i = Head[x]; i; i = nxt[i]) {
int v = to[i];
if (v == fa || vis[v]) {
continue;
}
getroot(v, x);
sz[x] += sz[v];
f[x] = max(f[x], sz[v]);
}
f[x] = max(f[x], sumsz - sz[x]);
if (f[x] < f[root]) {
root = x;
}
}
void getdeep(int x, int fa) {
o[++cnt] = dep[x];
for (int i = Head[x]; i; i = nxt[i]) {
int v = to[i];
if (v == fa || vis[v]) {
continue;
}
dep[v] = dep[x] + cost[i];
getdeep(v, x);
}
}
void calc(int x, int d, int add) {
cnt = ;
dep[x] = d;
getdeep(x, );
sort(o + , o + cnt + );
for (int i = ; i <= cnt; i++) {
for (int j = i + ; j <= cnt; j++) {
ok[o[i] + o[j]] += add;
}
}
}
void solve(int x) {
calc(x, , );
vis[x] = ;
for (int i = Head[x]; i; i = nxt[i]) {
int v = to[i];
if (vis[v]) {
continue;
}
calc(v, cost[i], -);
root = , sumsz = sz[v];
getroot(v, );
solve(root);
}
}
int main() {
scanf("%d %d", &n, &m);
cnt = ;
memset(Head, , sizeof(Head));
memset(vis, , sizeof(vis));
memset(ok, , sizeof(ok));
ed = ;
int u, v, c;
for (int i = ; i < n; i++) {
scanf("%d %d %d", &u, &v, &c);
ADD(u, v, c);
}
root = , sumsz = f[] = n;
getroot(, );
solve(root);
for (int i = ; i <= m; i++) {
scanf("%d", &k);
if (ok[k]) {
printf("AYE\n");
} else {
printf("NAY\n");
}
}
return ;
}
用类似poj1741的方法:对于每一次询问 calc()函数中求出<=k的和>=k的数量再减去总对数 则为=k的数量
复杂度为O(m*n*log2n)
#include<bits/stdc++.h>
#define MAXN 10005
#define INF 1e9+7
using namespace std;
struct front_star{
int to,next,w;
}edge[MAXN<<];
int n,cnt=,k,mx,root,ans=,tot=,siz,m;
int head[MAXN],sz[MAXN],temp[MAXN],idx[MAXN];
bool vis[MAXN];
int maxn(int a,int b)
{
return a>b?a:b;
}
void addedge(int u,int v,int c)
{
cnt++;
edge[cnt].to=v;
edge[cnt].w=c;
edge[cnt].next=head[u];
head[u]=cnt;
}
void findroot(int u,int fa)
{
sz[u]=;
int msz=;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v!=fa&&!vis[v])
{
findroot(v,u);
sz[u]+=sz[v];
msz=maxn(msz,sz[v]);
}
}
msz=maxn(msz,siz-sz[u]);
if(msz<mx)
{
mx=msz;
root=u;
}
}
void init()
{
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=;i<=n-;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
}
void dist(int u,int fa)
{
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v]&&v!=fa)
{
tot++;
idx[v]=tot;
temp[tot]=temp[idx[u]]+edge[i].w;
dist(v,u);
}
}
}
int count_ans(int u,int val)
{
tot=;
idx[u]=;
temp[]=val;
dist(u,u);
sort(temp+,temp++tot);
int L=,R=tot,res1=,res2=,ret;
while(L<=R)
{
if(temp[L]+temp[R]<=k)
{
res1+=R-L;
L++;
}
else
R--;
}
L=,R=tot;
while(L<=R)
{
if(temp[L]+temp[R]>=k)
{
res2+=R-L;
R--;
}
else
L++;
}
ret=res1+res2-(tot*(tot-))/;
return ret;
}
void divide(int u)
{
ans+=count_ans(u,);
vis[u]=true;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v]&&!vis[v])
{
ans-=count_ans(v,edge[i].w);
siz=sz[v];
mx=INF;
findroot(v,u);
divide(root);
}
}
}
void query()
{
for(int i=;i<=m;i++)
{
memset(vis,false,sizeof(vis));
scanf("%d",&k);
siz=n;
mx=INF;
ans=;
findroot(,);
divide(root);
if(ans==)
printf("NAY\n");
else
printf("AYE\n");
}
}
int main()
{
init();
query();
return ;
}
P3806 离线多次询问 树上距离为K的点对是否存在 点分治的更多相关文章
- 洛谷 P3806 【模板】点分治1-树分治(点分治,容斥版) 模板题-树上距离为k的点对是否存在
P3806 [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入格式 n,m 接下来n-1条边a,b,c描述a到b有一条长度 ...
- POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12276 Accepted: 3886 Description ...
- Codeforces 161.D. Distance in Tree-树分治(点分治,不容斥版)-树上距离为K的点对数量-蜜汁TLE (VK Cup 2012 Round 1)
D. Distance in Tree time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- HDU 2874 Connections between cities(LCA(离线、在线)求树上距离+森林)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目大意:给出n个点,m条边,q个询问,每次询问(u,v)的最短距离,若(u,v)不连通即不在同 ...
- POJ 1741 单次询问树上距离<=K的点对数 点分治
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; ; ], ...
- [HNOI2003]消防局的设立(树上距离为k的最小覆盖问题)
题目的大概意思现在有一棵树,在树上找半径小于等于2的最小覆盖点的最小个数. 题目链接 讲一讲此类题的贪心策略: 就是每次寻找最低没有被覆盖的点,显然对于覆盖它的所有点中,在他的祖先处设立一个点最优.所 ...
- poj1741 树上距离小于等于k的对数 点分治 入门题
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- [Swift]LeetCode863. 二叉树中所有距离为 K 的结点 | All Nodes Distance K in Binary Tree
We are given a binary tree (with root node root), a targetnode, and an integer value K. Return a lis ...
- Leetcode 863. 二叉树中所有距离为 K 的结点
863. 二叉树中所有距离为 K 的结点 显示英文描述 我的提交返回竞赛 用户通过次数39 用户尝试次数59 通过次数39 提交次数174 题目难度Medium 给定一个二叉树(具有根结点 ro ...
随机推荐
- USACO 1.1 Greedy Gift Givers
模拟. 如果用$map$的话,会很好写. 如果你不会$map$的话,在此小广告:https://blog.csdn.net/CQBZLYTina/article/details/80063739 /* ...
- 【VS开发】【DSP开发】如何使用WinDriver为PCIe采集卡装驱动
如何使用WinDriver为PCIe采集卡装驱动 第一步:使用WinDriver生成驱动 1.运行Drier Wizard 2.点击New host driverproject 3.在列表中,选择待安 ...
- Ubuntu下安装Golong并用Vscode做IDE最有效方法,避免99%的坑 | 轻松学习GO
最详细的教程,避开99%的坑,亲测有效 由于大部分教程都是win版本的,所以专门总结了一个linux版本的,其核心在于环境配置和插件安装,经历本人通宵7小时解决了这个问题,用自己的踩坑帮助大家避坑,希 ...
- 计蒜客习题:蒜头君的积木 (状压DP 枚举子集)
问题描述 蒜头君酷爱搭积木,他用积木搭了 n 辆重量为 wi的小车和一艘最大载重量为 W 的小船,他想用这艘小船将 n 辆小车运输过河.每次小船运载的小车重量不能超过 W.另外,小船在运载小车时,每辆 ...
- shell 字符
Shell 中的符号: 在shell中有很多符号代表了一些意思,重点说说 键盘上的符号在shell中的意义. 通配符: ~ 匹配家目录 ? 匹配单个字符.( ?之匹配单一的一个字符.x11 这种的就 ...
- Linux系列(6):入门之文件与目录管理
你知道常见的目录操作吗? 知道如何查询文件内容吗? 了解 umask 指令吗,知道如何查看和设置文件的默认权限吗? 知道文件的隐藏属性吗,了解如何设置(chattr指令)并查看(lsattr指令)吗? ...
- STL priority_queue 常见用法详解
<算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...
- union和in哪个效率高
一直都认为是in的效率要高,但是这次确有点蒙圈. SELECT * FROM runinfo WHERE status in (0,2,1,3,4,7,9,10); 这个查询的效率是,经常是1秒多. ...
- STM32的堆与栈与编译信息查看
STM32的堆与栈与编译信息查看 因为一个项目中使用malloc函数动态分配内存400多个字节,返回为0,分配失败.查找失败原因,为堆空间不足分配导致.查看堆和栈分别设置了2K,按正常情况看应能满足分 ...
- tomcat 虚拟目录 连接池