【题目链接】

https://www.lydsy.com/JudgeOnline/problem.php?id=2599

【算法】

点分治

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 200010
#define MAXK 1000010
typedef long long ll;
const ll INF = 2e9; ll i,n,k,u,v,w,tot,root,len,ans;
ll head[MAXN],size[MAXN],weight[MAXN],sum[MAXN],dep[MAXN],num[MAXK];
bool visited[MAXN]; struct Edge
{
ll to,w,nxt;
} e[MAXN<<]; inline void addedge(ll u,ll v,ll w)
{
tot++;
e[tot] = (Edge){v,w,head[u]};
head[u] = tot;
}
inline void getroot(ll u,ll fa,ll total)
{
ll i,v;
size[u] = ;
weight[u] = ;
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
if (fa != v && !visited[v])
{
getroot(v,u,total);
size[u] += size[v];
weight[u] = max(weight[u],size[v]);
}
}
weight[u] = max(weight[u],total - size[u]);
if (weight[u] < weight[root]) root = u;
}
inline void dfs(ll u,ll fa)
{
ll i,v,w;
if (sum[u] <= k) ans = min(ans,dep[u]+num[k-sum[u]]);
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (fa != v && !visited[v])
{
dep[v] = dep[u] + ;
sum[v] = sum[u] + w;
dfs(v,u);
}
}
}
inline void update(ll u,ll fa)
{
ll i,v;
if (sum[u] <= k) num[sum[u]] = min(num[sum[u]],dep[u]);
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
if (fa != v && !visited[v]) update(v,u);
}
}
inline void clear(ll u,ll fa)
{
ll i,v;
if (sum[u] <= k) num[sum[u]] = INF;
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
if (fa != v && !visited[v])
clear(v,u);
}
}
inline void work(ll u)
{
ll i,v,w;
dep[u] = ;
visited[u] = true;
num[] = ;
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (!visited[v])
{
dep[v] = ;
sum[v] = w;
dfs(v,u);
update(v,u);
}
}
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
if (!visited[v]) clear(v,u);
}
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
if (!visited[v])
{
root = ;
getroot(v,,size[v]);
work(root);
}
}
} int main()
{ scanf("%lld%lld",&n,&k);
for (i = ; i <= k; i++) num[i] = INF;
for (i = ; i < n; i++)
{
scanf("%lld%lld%lld",&u,&v,&w);
u++; v++;
addedge(u,v,w);
addedge(v,u,w);
}
root = ;
size[] = weight[] = n;
getroot(,,n);
ans = INF;
work(root);
if (ans < n) printf("%lld\n",ans);
else printf("-1\n");
return ; }

最新文章

  1. centos添加和删除用户及 xxx is not in the sudoers file.This incident will be reported.的解决方法
  2. Codeforces Round #262 (Div. 2) 1004
  3. 安装memcache扩展
  4. oracle-5-的升级步骤
  5. java的String类(一)
  6. Windows 10正式版密钥大全,Win10激活序列号KEY大全
  7. OC基础 NSData
  8. Tyvj P3276
  9. Sublime常用插件
  10. org.apache.commons.lang3.tuple.Pair 作为更新参数,XML 中的 Sql 取不到值、报错
  11. WINDOWS系统注册表取得管理权限研究
  12. LeetCode算法题-Arranging Coins(Java实现)
  13. php 文件远程下载
  14. Scrum:The Definition of Done —— 作业有没有写完呢?
  15. QQ去除未读状态的动画
  16. 7.1 服务暴露前的准备-ServiceBean的装配
  17. Gradle 1.12用户指南翻译
  18. csu1356 :判断一个环是否为奇数环
  19. deb软件包安装和卸载
  20. PHP SHA1withRSA加密生成签名及验签

热门文章

  1. ProgressDialog的关键几个函数
  2. ubuntu安装-Caffe依赖
  3. box-shadow 阴影剖析
  4. layui 多选下拉框 控件 样式改变原因
  5. C# 生成Model和DAL
  6. mac 安装卸载python
  7. C语言基础 (12) 文件的操作 FILE
  8. CSS - display:inline-block 相邻元素间有4px的空白间距
  9. Project Euler 8 Largest product in a series
  10. nyoj314-斐波那契数列四吧