这题数据范围变成了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. 2.React 生命周期函数

    什么是生命周期函数:在某一时刻组件会自动调用执行的函数. import React,{ Component,Fragment } from 'react' class Note extends Com ...

  2. oracle+mybatis批量插入踩坑记

    最近在项目中需要使用oracle+mybatis批量插入数据,因为自增主键,遇到问题,现记录如下: 一.常用的两种sql写法报错 1.insert ... values ... <insert ...

  3. DEVOPS ROADMAP

  4. VS开发】如何给console控制台程序更换应用程序图标

    [VS开发]如何给console控制台程序更换应用程序图标 标签:[VS开发] 实际上非常简单,就是增加一个图标资源,在资源视图里,然后修改其ID为IDC_MAINFRAME,然后编译生成即可! 20 ...

  5. SQL ----- JDBC 用ID查询某条记录

    package demo; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; ...

  6. day17 包与相对路径

    """ 今日内容: 1.导入模块的细节 2.包的概念及使用 3.包的相对导入 """ """ 1.导入模块的细 ...

  7. Linux系列(2):入门之线上求助

    前言:Linux命令那么多,你是否为记不住Linux的命令而烦恼呢? 这一章节就是来解决这个问题的. 1.Linux系统的线上求助 1.指令补全 在上一章节提到过使用[Tab]快捷键可以根据用户输入的 ...

  8. Mysql中多表删除

    1.从MySQL数据表A中把那些id值在数据表B里有匹配的记录全删除掉 DELETE t2 FROM A t1,B t2 WHERE t1.id = t2.id DELETE FROM t2 USIN ...

  9. [HAOI2010]软件安装 题解

    题面 这道题比较显然地,是一道树形背包: 但是会有环,怎么办呢? 缩点!tarjan缩点! 然后在新图上跑树形背包就可以AC了 #include <bits/stdc++.h> #defi ...

  10. 【hash】A Horrible Poem

    [题目链接] # 10038. 「一本通 2.1 练习 4」A Horrible Poem [参考博客] A Horrible Poem (字符串hash+数论) [题目描述] 给出一个由小写英文字母 ...