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 ...
随机推荐
- 记一次django学习1.0和2.0区别
依据学习课程的教学,在项目实战学习过程中教学使用django1.0,获取ManytoMany关联字段,源码使用的是 即django使用 models.Customer.tags.rel.to.obje ...
- keepalived脑裂问题
一.对脑裂的理解 在高可用(HA)系统中,当联系2个节点的“心跳线”断开时,本来为一整体.动作协调的HA系统,就分裂成为2个独立的个体.由于相互失去了联系,都以为是对方出了故障.两个节点上的HA软件像 ...
- 2019-2020 ICPC, Asia Jakarta Regional Contest A. Copying Homework
Danang and Darto are classmates. They are given homework to create a permutation of N integers from ...
- 纪录一次left join一对多关系而引起的BUG
纪录一次left join一对多关系而引起的BUG MySQL(11)---纪录一次left join一对多关系而引起的bug BUG背景 我们有一个订单表 和 一个 物流表 它们通过 订单ID 进行 ...
- C# 字典、集合、列表的时间复杂度
List列表是顺序线性表,Add操作是O(1)或O(N),因为List是动态扩容的,在未扩容之前,其Add操作是O(1),而在扩容的时候,Add操作是O(N)的.其Contains方法,是按照线性检索 ...
- hbase与hdfs的交互
hdfs和hbase的交互,和写MapReduce程序类似,只是需要修改输入输出数据和使用hbase的javaAPI对其进行操作处理即可 public class HBaseToHdfs extend ...
- PyCharm 格式化代码 常用快捷键
ctrl+alt+L 一 常用快捷键 编辑类:Ctrl + D 复制选定的区域或行Ctrl + Y 删除选定的行Ctrl + Alt + L 代码格 ...
- 百度地图的初始化应当在vue的mounted()函数里执行
今天使用百度地图出现了一个问题,百度地图初始化后宽.高都是0,但是地图容器宽高都设置好的, 一开始怎么都排除不出问题,后来无语了,把布局直接复制进入百度地图的示例里运行发现没有问题, 所以想到不是百度 ...
- Codeforces 1236A. Stones
传送门 注意到两种操作都要消耗中间的石头,并且两种操作每次都会得到 $3$ 个石头 那么显然优先做操作二是最优的,因为操作二只会消耗中间堆的一个石头 如果有剩下再进行操作 $1$ ,那么可以保证总操作 ...
- Windows authentication for WCF web services error
WCF Web服务的Windows身份验证在部署到IIS时,默认网站添加应用程序的方式.浏览运行.svc文件报错. 错误代码: The authentication schemes configure ...