POJ 2114 (点分治)
题目:https://vjudge.net/contest/307753#problem/B
题意:求树中路径和=k的点对是否存在
思路:点分治,这个题其实和上一题洛谷一样,只是这个数据强,我们不能直接预处理所有可能的路径长度,预处理所有路径长度复杂度 O(n^2) ,我们改为直接每次查询都分治一遍,我们只要把solve在O(n)求出来,那时间复杂度就是 O(n*logn*logn),时间上快了很多,其实等于k这个可以使用我们之前的方法。直接把不大于k的路径-小于k的路径=等于k的路径,然后搞一搞就可以了
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- #include<iostream>
- #include<vector>
- #define maxn 100005
- #define mod 1e19
- using namespace std;
- typedef long long ll;
- ll da;
- vector<pair<ll,ll> > mp[maxn];//存下图
- bool vis[maxn];//标记曾经使用过的重心
- ll maxsize[maxn],dis[maxn],d[maxn];//maxsize 当前节点的最大子树
- ll siz[maxn],e[maxn];// dis 到重心的距离 d 出现过的距离
- ll n,m,rt,sum,qe; // siz 当前节点的子树个数 e 出现的距离 rt代表当前重心
- void find(ll x,ll f){//找出重心
- siz[x]=;
- maxsize[x]=;
- for(int i=;i<mp[x].size();i++){
- pair<ll,ll> q=mp[x][i];
- if(q.first==f||vis[q.first]) continue;//vis数组标记曾经使用过的重心
- find(q.first,x);
- siz[x]+=siz[q.first];
- maxsize[x]=max(maxsize[x],siz[q.first]);
- }
- maxsize[x]=max(maxsize[x],sum-siz[x]);//节点总数减去当前的子树数=以当前节点为根的父亲点子树数
- if(maxsize[x]<maxsize[rt]){
- rt=x;
- }
- }
- void get_dis(ll x,ll f,ll len){
- e[++qe]=len;
- for(int i=;i<mp[x].size();i++){
- pair<ll,ll> q=mp[x][i];
- if(q.first==f||vis[q.first]) continue;
- dis[q.first]=dis[x]+len;
- get_dis(q.first,x,len+q.second);
- }
- }
- ll solve(ll x,ll len){
- ll ee=;
- qe=;
- dis[x]=len;
- get_dis(x,,len);
- sort(e+,e+qe+);
- ll l=,r=qe;
- while(l<r){
- if(e[l]+e[r]<=m){
- ee+=r-l;
- l++;
- }
- else{
- r--;
- }
- }
- return ee;
- }
- void divide(ll x){
- da+=solve(x,);
- vis[x]=;
- for(int i=;i<mp[x].size();i++){
- pair<ll,ll> q=mp[x][i];
- if(vis[q.first]) continue;
- da-=solve(q.first,q.second);
- sum=siz[q.first];
- rt=;
- maxsize[rt]=mod;
- find(q.first,x);
- divide(rt);
- }
- }
- void init(){
- da=;
- for(int i=;i<=n;i++) mp[i].clear();
- for(int i=;i<=n;i++) vis[i]=;
- }
- int main(){
- while(scanf("%lld%lld",&n,&m)!=EOF)
- {
- if(n==&&m==) break;
- ll a,b,c;
- init();
- char s[];
- for(int i=;i<n-;i++){
- scanf("%lld%lld%lld%s",&a,&b,&c,s);
- //a++;
- //b++;
- mp[a].push_back(make_pair(b,c));
- mp[b].push_back(make_pair(a,c));
- }
- scanf("%lld",&m);
- sum=n;//当前节点数
- rt=;
- maxsize[]=mod;//置初值
- find(,);
- divide(rt);
- printf("%lld\n",da);
- }
- }
POJ 2114 (点分治)的更多相关文章
- POJ 2114 点分治
思路: 点分治 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> u ...
- poj 2114 Boatherds (树分治)
链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...
- POJ 2114 Boatherds【Tree,点分治】
求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...
- poj 2114 Boatherds 树的分治
还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树. #include <iostream> #include <cstdio> #inc ...
- Poj 2114 Boatherds(点分治)
Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...
- 树分治 点分治poj 2114
存在2点间距离==k 输出AYE 否则输出NAY #include<stdio.h> #include<string.h> #include<algorithm> ...
- POJ 2114 Boatherds 树分治
Boatherds Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...
- poj 2114 树的分治 可作模板
/* 啊啊啊啊啊啊啊本题证明一个问题,在实际应用中sort比qsort块 还有memset这类初始化能不加尽量别加,很浪费时间 原来的程序把qsort该成sort,去掉一个无用memset就a了时间不 ...
- POJ 2114 - Boatherds
原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...
随机推荐
- 015-Spring Boot 定制和优化内嵌的Tomcat
一.内嵌web容器 参看http://www.cnblogs.com/bjlhx/p/8372584.html 查看源码可知提供以下三种: 二.定制优化tomcat 2.1.配置文件配置 通过appl ...
- delphi idhttpsever
http://blog.csdn.net/chelen_jak/article/details/50203809 delphi idhttpsever 2015-12-07 11:36 216人阅读 ...
- 关于C++ string 的神奇用法
c++里有大部分字符的操作都在#include<cstring>这个库中,这个库的函数在考试的时候都是可以用的,这个库里包含了很多字符串操作函数,特别是string这个数据类型特别优美,它 ...
- Python3-问题整理
TypeError: a bytes-like object is required, not 'str' json.decoder.JSONDecodeError: Extra data json文 ...
- Git004--版本回退
Git--版本回退 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...
- Google XSS game writeup
用过Chrome的应该知道它的XSS Auditor,它可是灭掉了不少XSS代码呢……Google对XSS是很有研究的,不然也不敢大张旗鼓的悬赏(7500刀哦亲),还开发了一个XSS小游戏 http: ...
- jmeter 添加header
接口说明文档: article.fetch(通用转码服务) 通用转码服务,获取任意 url 的正文以及 title 等基本信息,仅支持 post 方法请求. 参数 参数 类型 是否必须 示例 其它说明 ...
- [eclipse相关] 001 - 启动+运行优化
本随笔参考了其他博客内容,且在验证有效之下才或誊抄或摘录或加上自己经验组合而成. 参考博客: 1,http://zwd596257180.gitee.io/blog/2019/04/17/eclips ...
- golang简介
GO语言是Google于2009年推出的一门新的系统编程语言 特点: 静态编译 垃圾回收 简洁的符号和语法 平坦的类型系统 基于CSP的并发模型 高效简单的工具链 丰富的标准库 为什么选择go语言 编 ...
- 移动端web整理 移动端问题总结,移动web遇到的那些坑
meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 忽略将页面中的数字识别为电话号码 忽略Android平台中对邮箱地址的识别 当网站添加到主屏幕快速启动方式,可隐藏地址栏,仅针对i ...