P4149 距离为K的点对(最少边数) n=200000 点分治
这题数据范围变成了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 点分治的更多相关文章
- POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12276 Accepted: 3886 Description ...
- [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 ...
- [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 ...
- 距离为K的节点 All Nodes Distance K in Binary Tree
2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...
- poj 1741 两点距离小于K(树DP)
http://blog.csdn.net/woshi250hua/article/details/7723400 求两点间距离小于等于k的方案数 理一下思路: 求通过点A与另一点连接符合条件的个数 = ...
- 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 ...
- 洛谷 P3806 【模板】点分治1-树分治(点分治,容斥版) 模板题-树上距离为k的点对是否存在
P3806 [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入格式 n,m 接下来n-1条边a,b,c描述a到b有一条长度 ...
- P3806 离线多次询问 树上距离为K的点对是否存在 点分治
询问树上距离为k的点对是否存在 直接n^2暴力处理点对 桶排记录 可以过 #include<cstdio> #include<cstring> #include<algo ...
随机推荐
- mysql数据库之索引与慢查询优化
索引与慢查询优化 知识回顾:数据都是存在硬盘上的,那查询数据不可避免的需要进行IO操作 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构. primary key unique ...
- linux-关闭文件
1.打开参考: http://q.cnblogs.com/q/39275/ http://hi.baidu.com/auxor/item/49b6e929fdf16dc7ed10f197 2.关闭参考 ...
- sshpass和做软链接
参考: https://help.aliyun.com/document_detail/54530.html?spm=5176.11065259.1996646101.searchclickresul ...
- Linux下测试ZLAN 5800
今天师兄让帮忙测试ZLAN 5800八串口通信模块,windows下的测试按照手册来已经搞定,接下来是Linux下的测试. 因为厂家不提供Linux下的相关资料,所以需要在windows下设置好后直接 ...
- 【Python】【demo实验22】【练习实例】【猴子吃桃问题】
原题: 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第10天早上想再吃时,见只 ...
- 小记--------maxwell启动失败解决
查看报错日志信息: com.github.shyiko.mysql.binlog.network.ServerException: Could not find first log file name ...
- BOF和EOF详解2
在学生信息管理系统中有多处用到EOF和BOF,使用BOF和EOF属性可确定Recordset对象是否包含记录,或者从一个记录移动到另一个记录是否超出Recordset对象的限制. BOF和EOF他们是 ...
- h5中的分组元素figure、figcaption、hgroup元素介绍
分组元素用于对页面中的内容进行分组. figure元素和figcaption元素 figure元素用于定义独立的流内容(图像.图表.照片.代码等),一般指一个独立的单元.figure元素的内容应该与主 ...
- Linux磁盘挂载、分区、扩容操作
本文最早发布于 Rootrl's blog 注:以下操作系统环境为CentOS7 基本概念 在操作前,首先要了解一些基本概念 磁盘 在Linux系统中所有的设备都会以文件的形式存储.设备一般保存在/d ...
- sql server安装图解
1.进入安装中心:可以参考硬件和软件要求.可以看到一些说明文档 2.选择全新安装模式继续安装 3.输入产品秘钥:这里使用演示秘钥进行 4.在协议中,点击同意,并点击下一步按钮,继续安装 5.进入全局规 ...