【BZOJ-1468】Tree 树分治
1468: Tree
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 1025 Solved: 534
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
Sample Output
HINT
Source
Solution
写LTC还不如直接写ACRush,楼教主的男人八题.暑假看过什么都看不懂,现在似乎可以做了?
裸的树分治,统计一下路径的权值和,排序计算答案即可
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
#define maxn 80010
int n,root,siz,ans,stack[maxn],top,K;
struct Edgenode{int to,next,val;}edge[maxn<<];
int head[maxn],cnt=;
void add(int u,int v,int w)
{cnt++; edge[cnt].val=w; edge[cnt].to=v; edge[cnt].next=head[u]; head[u]=cnt;}
void insert(int u,int v,int w)
{add(u,v,w);add(v,u,w);}
int gcd(int a,int b)
{if (b==) return a; return gcd(b,a%b);}
int size[maxn],maxx[maxn],val[maxn]; bool visit[maxn];
void DFSroot(int now,int last)
{
size[now]=; maxx[now]=;
for (int i=head[now]; i; i=edge[i].next)
if (edge[i].to!=last && !visit[edge[i].to])
{
DFSroot(edge[i].to,now);
size[now]+=size[edge[i].to];
maxx[now]=max(maxx[now],size[edge[i].to]);
}
maxx[now]=max(maxx[now],siz-size[now]);
if (maxx[now]<maxx[root]) root=now;
}
void DFSval(int now,int last)
{
stack[++top]=val[now];
for (int i=head[now]; i; i=edge[i].next)
if (edge[i].to!=last && !visit[edge[i].to])
{
val[edge[i].to]=val[now]+edge[i].val;
DFSval(edge[i].to,now);
}
}
int Getans(int now,int va)
{
val[now]=va; top=; DFSval(now,);
sort(stack+,stack+top+);
int re=,l=,r=top;
while (l<r) if (stack[l]+stack[r]<=K) re+=r-l,l++; else r--;
return re;
}
void work(int now)
{
ans+=Getans(now,);
visit[now]=;
for (int i=head[now]; i; i=edge[i].next)
if (!visit[edge[i].to])
{
ans-=Getans(edge[i].to,edge[i].val);
root=; siz=size[edge[i].to];
DFSroot(edge[i].to,); work(root);
}
}
int main()
{
n=read();
for (int u,v,w,i=; i<=n-; i++)
u=read(),v=read(),w=read(),insert(u,v,w);
K=read();
maxx[]=siz=n;
DFSroot(,);
work(root);
printf("%d\n",ans);
return ;
}
吃饭前想10分钟Rush出来...手速不够TAT
【BZOJ-1468】Tree 树分治的更多相关文章
- bzoj 1468 Tree(点分治模板)
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1527 Solved: 818[Submit][Status][Discuss] ...
- BZOJ.1468.Tree(点分治)
BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- poj 1744 tree 树分治
Tree Time Limit: 1000MS Memory Limit: 30000K Description Give a tree with n vertices,each ed ...
- HDU4871 Shortest-path tree(树分治)
好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...
- HDU4670 Cube number on a tree 树分治
人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...
- HDU 4812 D Tree 树分治
题意: 给出一棵树,每个节点上有个权值.要找到一对字典序最小的点对\((u, v)(u < v)\),使得路径\(u \to v\)上所有节点权值的乘积模\(10^6 + 3\)的值为\(k\) ...
随机推荐
- SQL SERVER 系统库查询
本文内容主要来自网络,如有错误请路过的大牛指点迷津. 1.sqlserver 数据库最大并发连接数 sqlserver的最大连接数虽然说是不限制,但实际的限制数量是32767,如果需要超出这个数量,一 ...
- struts2: 通过流输出实现exce导出
参考下面代码,在Action中加一个方法: // 导出excel public String excel() throws Exception { StringBuffer excelBuf = ne ...
- 安装Ubuntu时的硬盘分区方案
如果你准备在硬盘里只安装Ubuntu一个操作系统的话,建议你采用一个“/”.一个“swap”和一个“/home”的三分区方案:/ :10GB-15GB.swap:物理内存小于或等于 512MB,建议分 ...
- PAT 1067. Sort with Swap(0,*)
1067. Sort with Swap(0,*) (25) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy ...
- Linux 中 Weblogic 启动和关闭
a.start weblogic1)找到 /Oracle/Middleware/user_projects/domains/ 用户_domain目录. nohup ./startWebLogic.sh ...
- 对《重建中国.NET生态系统》评论贴的总结
Neuzilla官方微信公众号:搜 架构师联盟 或 neuzilla,也可以扫下面二维码 在看了<重建中国.NET生态系统>的各种哭爹喊娘骂街的评论之后,我觉得哦,淫才确实很多,但是么真正 ...
- 建立时间和保持时间(setup time 和 hold time)
建立时间和保持时间贯穿了整个时序分析过程.只要涉及到同步时序电路,那么必然有上升沿.下降沿采样,那么无法避免setup-time 和 hold-time这两个概念.本文内容相对独立于该系列其他文章,是 ...
- des解密不完整,前面几位是乱码的解决办法
在工作中遇到的Des解密问题,第三方发来的数据需要我们进行des解密,但是解密的结果前几位始终是乱码.废了半天劲,终于找到了问题所在. 下面先介绍一下des,了解des的同学可以直接看下面的解决办法. ...
- java并发:简单面试问题集锦
多线程:Simultaneous Multithreading,简称SMT. 并行.并发 并行性(parallelism)指两个或两个以上的事件在同一时刻发生,在多道程序环境下,并行性使多个程序同一时 ...
- 创建Maven项目
在MyEclipse10中创建Maven Web项目 1.构建maven项目 2.将maven项目转换成Dynamic Web Project 3.设置部署集 4.pom.xml文件配置 参考: ht ...