这题数据范围变成了200000 n^2就过不了 同时要求求的是最少的边数 不能容斥

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ;
const int MAXM = 2e5 + ;
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, anser, k, cnt;
int sz[MAXN], f[MAXN], dep[MAXN], sumsz, root;
bool vis[MAXN];
pair<int, int> o[MAXN];
int num[MAXN];
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, int deep) {
if (dep[x] > k) {
return;
}
o[++cnt] = make_pair(dep[x], deep);
num[++num[]] = 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, deep + );
}
}
void calc(int x, int d) {
num[] = ;
dep[x] = d;
for (int i = Head[x]; i; i = nxt[i]) {
int v = to[i];
if (vis[v]) {
continue;
}
cnt = ;
dep[v] = dep[x] + cost[i];
getdeep(v, x, );
for (int j = ; j <= cnt; j++) {
if (o[j].first <= k) {
if (ok[k - o[j].first] != INT_MAX) {
anser = min(anser, ok[k - o[j].first] + o[j].second);
}
}
}
for (int j = ; j <= cnt; j++) {
if (o[j].first <= k) {
ok[o[j].first] = min(o[j].second, ok[o[j].first]);
}
}
}
for (int i = ; i <= num[]; i++) {
ok[num[i]] = INT_MAX;
}
}
void solve(int x) {
vis[x] = ;
calc(x, );
int totsz = sumsz;
for (int i = Head[x]; i; i = nxt[i]) {
int v = to[i];
if (vis[v]) {
continue;
}
root = ;
sumsz = sz[v] > sz[x] ? totsz - sz[x] : sz[v];
getroot(v, );
solve(root);
}
}
int main() {
scanf("%d %d", &n, &k);
anser = INT_MAX;
for (int i = ; i <= k; i++) {
ok[i] = INT_MAX;
}
cnt = ;
memset(Head, , sizeof(Head));
memset(vis, , sizeof(vis));
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);
printf("%d\n", anser == INT_MAX ? - : anser);
return ;
}

注意dep[x]>k的时候要return 不然会re

P4149 距离为K的点对(最少边数) n=200000 点分治的更多相关文章

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

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

  2. [LeetCode] All Nodes Distance K in Binary Tree 二叉树距离为K的所有结点

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

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

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

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

  5. 距离为K的节点 All Nodes Distance K in Binary Tree

    2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...

  6. poj 1741 两点距离小于K(树DP)

    http://blog.csdn.net/woshi250hua/article/details/7723400 求两点间距离小于等于k的方案数 理一下思路: 求通过点A与另一点连接符合条件的个数 = ...

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

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

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

  9. P3806 离线多次询问 树上距离为K的点对是否存在 点分治

    询问树上距离为k的点对是否存在 直接n^2暴力处理点对 桶排记录 可以过 #include<cstdio> #include<cstring> #include<algo ...

随机推荐

  1. webshell劫持工具【全站劫持】

    演示视频下载地址:https://share.weiyun.com/53hPGYj 需要联系QQ:1743685523 ---------------------------------------- ...

  2. 安装networking-sfc

    申明: 主参考:https://docs.openstack.org/networking-sfc/queens/ 辅参考: https://blog.csdn.net/linshenyuan1213 ...

  3. 数据结构与算法-queue

    队列和stack类似,stack是先进后出,而queue的先进先出,也是一种特殊的线性表 基本概念 概念 队列是一种特殊的线性表 队列仅在线性表的两端进行操作 队头(Front):取出数据元素的一端 ...

  4. 调用存储在session属性里的东西

    将对象放在session里面 request.getSession().setAttribute("username", username); //放到会话里 永EL表达式调用 $ ...

  5. Lua字符串及模式匹配

    字符类基础函数举例介绍: string.len( ‘string’ ) string.lower( ‘string’ ) string.upper( ‘string’ ) string.rep( ‘a ...

  6. React + Antd开发模式下的Excel导入功能

    具体js如下,配合的是antd里面的upload组件,使用的是xlsx插件 npm :  npm install xlsx 插件链接: https://github.com/SheetJS/sheet ...

  7. 人渣给我笔记本网络适配器里加的的“bluetooth设备(个人局域网)”,卸载不掉

  8. 使用Docker Maven 插件进行镜像的创建以及上传至私服

    1.在进行服务容器化部署的时候,需要将服务以及其运行的环境整个打包做成一个镜像,打包的过程有两种办法,第一种是首选通过maven打成jar包,然后再编写dockerfile,执行docker buil ...

  9. 理解twisted中的reactor和deferred(一)

    Deferred是一个延迟加载对象,这个概念类似于tornado future,是调用异步操作返回的一个对象,其中包括了操作成功后的回调处理,错误后的回调处理. 简单讲,当我们需要执行一个耗时操作,比 ...

  10. Cow and Snacks(吃点心--图论转换) Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)

    题意:https://codeforc.es/contest/1209/problem/D 有n个点心,有k个人,每个人都有喜欢的两个点心,现在给他们排个队,一个一个吃,每个人只要有自己喜欢的点心就会 ...