询问树上距离为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的点对是否存在 点分治的更多相关文章

  1. 洛谷 P3806 【模板】点分治1-树分治(点分治,容斥版) 模板题-树上距离为k的点对是否存在

    P3806 [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入格式 n,m 接下来n-1条边a,b,c描述a到b有一条长度 ...

  2. POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12276   Accepted: 3886 Description ...

  3. 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 ...

  4. HDU 2874 Connections between cities(LCA(离线、在线)求树上距离+森林)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目大意:给出n个点,m条边,q个询问,每次询问(u,v)的最短距离,若(u,v)不连通即不在同 ...

  5. POJ 1741 单次询问树上距离<=K的点对数 点分治

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; ; ], ...

  6. [HNOI2003]消防局的设立(树上距离为k的最小覆盖问题)

    题目的大概意思现在有一棵树,在树上找半径小于等于2的最小覆盖点的最小个数. 题目链接 讲一讲此类题的贪心策略: 就是每次寻找最低没有被覆盖的点,显然对于覆盖它的所有点中,在他的祖先处设立一个点最优.所 ...

  7. poj1741 树上距离小于等于k的对数 点分治 入门题

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

  8. [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 ...

  9. Leetcode 863. 二叉树中所有距离为 K 的结点

    863. 二叉树中所有距离为 K 的结点  显示英文描述 我的提交返回竞赛   用户通过次数39 用户尝试次数59 通过次数39 提交次数174 题目难度Medium 给定一个二叉树(具有根结点 ro ...

随机推荐

  1. centos 自动挂载ISO

    创建挂载点并挂载光盘mkdir -p /media/cdrommount -t iso9660 -o loop /usr/ison/centos.iso /media/cdrom 设置开机自动挂载:方 ...

  2. docker清理

    # 删除退出的容器docker rm $(docker ps -qa --no-trunc --filter "status=exited") # 删除悬挂镜像docker rmi ...

  3. 小记--------sparksql和DataFrame的小小案例java、scala版本

    sparksql是spark中的一个模块,主要用于进行结构化数据的处理,他提供的最核心的编程抽象,就是DataFrame.同时,sparksql还可以作为分布式的sql查询引擎. 最最重要的功能就是从 ...

  4. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge 树上倍增

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  5. 第9章:LeetCode--算法:HASH表

    哈希表(Hash table,也叫散列表),关键值K和内容的映射表,通过映射函数实现,hashtable(key,value) 进行查询的时候,就是使用哈希函数将关键码key转换为对应的数组下标,并定 ...

  6. 10.Bash的安装

    10.Bash的安装本节提供了在 Bash支持的不同系统上的基本安装指导.本版本支持 GNU操作系统,几乎每个 UNIX版本,以及几个非 UNIX 系统,例如 BeOS 和 Interix.还有针对 ...

  7. Func<>委托、扩展方法、yield、linq ForEach综合运用

    1.先定义一个Model类    public class P1    {        public string name { get; set; }        public int age ...

  8. 洛谷题解P4314CPU监控--线段树

    题目链接 https://www.luogu.org/problemnew/show/P4314 https://www.lydsy.com/JudgeOnline/problem.php?id=30 ...

  9. linux core文件的打开和分析

    1. core文件生成打开方式: ulimit -c unlimited echo "1" > /proc/sys/kernel/core_uses_pid 看下服务器上是否 ...

  10. The method getContextPath() from the type HttpServletRequest

    在做java项目开发的时候,jsp页面很容易报出这个错误. 错误的原因很多,但是都和JRE有关. 一般在导入项目的时候容易报出这个错误,主要因为JRE(jdk版本不一致). 解决方法:就是重新配置路径 ...