bzoj1468 Tree
2016-06-25 11:03
381 查看
最经典的点分治题目,在递归子树的时候减去在算父亲时的不合法方案。
View Code
Submit: 1099 Solved: 581
[Submit][Status][Discuss]
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #include<cmath> #define ll long long #define N 40005 using namespace std; struct Node{ int to,next,v; }e[N<<1]; int sum,rt,n,k,head ,tot,cnt,mx ,sz ,tmp ,ans; bool vis ; void add(int x,int y,int z){ e[++tot]=(Node){y,head[x],z};head[x]=tot; e[++tot]=(Node){x,head[y],z};head[y]=tot; } void getroot(int x,int fa){ mx[x]=0;sz[x]=1; for(int i=head[x];i;i=e[i].next)if(e[i].to!=fa&&!vis[e[i].to]){ getroot(e[i].to,x); sz[x]+=sz[e[i].to]; mx[x]=max(mx[x],sz[e[i].to]); }mx[x]=max(mx[x],sum-sz[x]); if(mx[x]<mx[rt])rt=x; } void getdis(int x,int fa,int d){ tmp[++cnt]=d; for(int i=head[x];i;i=e[i].next)if(e[i].to!=fa&&!vis[e[i].to]){ getdis(e[i].to,x,d+e[i].v); } } void calc(int x,int dis,int type){ cnt=0; getdis(x,0,0); sort(tmp+1,tmp+1+cnt); int l=1,r=cnt; while(l<=r){ while(tmp[l]+tmp[r]>dis)r--; if(l>r)break; ans+=(r-l)*type;l++; } } void work(int x){ vis[x]=1; calc(x,k,1); for(int i=head[x];i;i=e[i].next)if(!vis[e[i].to]){ calc(e[i].to,k-e[i].v*2,-1); sum=sz[e[i].to];rt=0; getroot(e[i].to,0); work(rt); } } int main(){ // freopen("test.in","r",stdin); scanf("%d",&n); for(int i=1;i<n;i++){ int x,y,z;scanf("%d%d%d",&x,&y,&z); add(x,y,z); } scanf("%d",&k); sum=mx[0]=n;rt=0;ans=0; getroot(1,0); work(rt); printf("%d\n",ans); }
View Code
1468: Tree
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1099 Solved: 581
[Submit][Status][Discuss]
Description
给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于KInput
N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是kOutput
一行,有多少对点之间的距离小于等于kSample Input
71 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
Sample Output
5相关文章推荐
- android 四大组件之Service(6) 粘性与非粘性
- 最短路算法
- linux 下C语言学习路线
- 如何提问!很感谢原博主的分享,我自己也收获了许多!
- 图的基本算法
- sscanf整形输出误区
- 新浪微博腾讯微博Android开发笔记
- 用Redis的set指令实现锁
- java小知识点2
- docker 的LXC技术
- 在CentOS上搭建PHP服务器环境
- Oracle 数据库开启关闭 及 端口监听相关命令
- 怎样修改JTree中每级节点前面的图标
- 费用报销-不能走到已付款状态
- ACdream 1210 Chinese Girls' Amusement(高精度)
- Mysql 数据库的数据大小查看:
- 通过ServletContext类的getRealPath()方法获取服务器的真实地址
- window scipy install
- 【Python学习日记】函数式编程 之 高阶函数
- 对01背包问题的思考