询问树上距离为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. 【Web】如何注释?

    HTML 形式:<!-- 注释内容 --> 实例: <!-- <p>这是第一段</p> --> CSS 形式:/* 注释内容 */ 实例: /* 选中i ...

  2. 爬取汽车之家新闻图片的python爬虫代码

    import requestsfrom bs4 import BeautifulSouprespone=requests.get('https://www.autohome.com.cn/news/' ...

  3. Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)

    Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...

  4. 宝塔配置composer默认的PHP版本

    当安装composer时,提示PHP版本问题: rm -f /usr/bin/php //这是默认的版本,删除 ln -sf /www/server/php/71/bin/php /usr/bin/p ...

  5. 企业场景-网站目录安全权限深度讲解及umask知识

    站点目录的文件和目录给什么权限: 默认权限是安全权限的临界点,工作中尽量给这个临界点,或者小于临界点,不要大于临界点权限. 默认权限分配的命令 umask 在linux下文件的默认权限是由umask值 ...

  6. 使用nfsstat命令查看NFS服务器状态

    转载于:http://www.cnblogs.com/jankie/archive/2011/09/03/2165851.html nfsstat命令显示关于NFS和到内核的远程过程调用(RPC)接口 ...

  7. Design Circular Queue

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  8. 【Python】【demo实验17】【练习实例】【将一个正整数分解质因数】

    题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5. 我的源代码: #!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 ...

  9. 【转帖】Linux的桌面环境gnome、kde、xfce、lxde 等等使用比较

    Linux的桌面环境gnome.kde.xfce.lxde 等等使用比较 https://www.cnblogs.com/chenmingjun/p/8506995.html 文章目录 图形界面架起用 ...

  10. mysql语句(一)

    --建表CREATE TABLE IF NOT EXISTS `runoob_tbl`( `runoob_id` INT UNSIGNED AUTO_INCREMENT, `runoob_title` ...