【POJ 1741】Tree
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 11570 | Accepted: 3626 |
Description
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.
Input
The last test case is followed by two zeros.
Output
Sample Input
5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0
Sample Output
8
Source
我的点分治第一题~~~
树上的路径分为两类:
经过根结点。不经过根结点。
那么我们能够选择每个点为根,然后计算经过这个根结点的长度<=k的路径,再依照同样的方法计算他的全部子树中的路径条数,这样就能不重不漏。
怎样计算经过根结点且长度<=k的路径条数?
用全部的减去在同一棵子树中的就能够。
怎么计算全部的长度<=k的路径?
用dfs求出每一个点的深度,存在一个数组里。然后从小到大排个序。用两个指针扫:l=1,r=tot,在l添加的过程中满足dep[l]+dep[r]<=k的r指针是不增的,所以O(n)能够出解,再加上sort的O(nlogn)。这个操作的复杂度为O(nlogn)。
所以总的复杂度为O(递归层数*nlogn)。怎样让递归层数最少?
每次让重心(找重心【POJ 1655】)当根结点,递归层数是logn的。
于是总复杂度为O(nlog^2n)~
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#define M 10005
using namespace std;
struct edge
{
int y,ne,l;
}e[M*100];
int ans,all,h[M],f[M],done[M],s[M],dep[M],tot=0,size,n,k,root;
void Addedge(int x,int y,int l)
{
tot++;
e[tot].y=y;
e[tot].ne=h[x];
e[tot].l=l;
h[x]=tot;
}
void Getroot(int x,int fa)
{
f[x]=0;
s[x]=1;
for (int i=h[x];i;i=e[i].ne)
{
int y=e[i].y;
if (y==fa||done[y]) continue;
Getroot(y,x);
s[x]+=s[y];
f[x]=max(f[x],s[y]);
}
f[x]=max(f[x],size-s[x]);
if (f[x]<f[root]) root=x;
}
void Getdep(int x,int fa,int de)
{
dep[++all]=de;
s[x]=1;
for (int i=h[x];i;i=e[i].ne)
{
int y=e[i].y,l=e[i].l;
if (y==fa||done[y]) continue;
Getdep(y,x,de+l);
s[x]+=s[y];
}
}
int calc(int x,int de)
{
int an=0;
all=0;
Getdep(x,0,de);
sort(dep+1,dep+1+all);
for (int l=1,r=all;l<r;)
if (dep[l]+dep[r]<=k) an+=r-l++;
else r--;
return an;
}
void Solve(int x)
{
ans+=calc(x,0);
done[x]=true;
for (int i=h[x];i;i=e[i].ne)
{
int y=e[i].y;
if (done[y]) continue;
ans-=calc(y,e[i].l);
size=f[0]=s[y];
Getroot(y,root=0);
Solve(root);
}
}
int main()
{
while (scanf("%d%d",&n,&k)==2)
{
if (n==k&&n==0) break;
tot=0;
for (int i=1;i<=n;i++)
h[i]=0,done[i]=false;
for (int i=1;i<n;i++)
{
int x,y,l;
scanf("%d%d%d",&x,&y,&l);
Addedge(x,y,l);
Addedge(y,x,l);
}
ans=0;
f[0]=size=n;
Getroot(1,root=0);
Solve(root);
printf("%d\n",ans);
}
return 0;
}
感悟:
1.TLE是由于Solve(root)。写成Solve(y)了。。
2.点分治的关键是要知道路径分经过根结点和不经过根结点,由此找到递归方法。
【POJ 1741】Tree的更多相关文章
- 【POJ 1741】 Tree (树的点分治)
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...
- 【POJ 1741】 Tree
[题目链接] http://poj.org/problem?id=1741 [算法] 点分治 要求距离不超过k的点对个数,不妨将路径分成两类 : 1. 经过根节点 2. 不经过根节点 考虑第1类路径, ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
- 【POJ 2750】 Potted Flower(线段树套dp)
[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4566 ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
随机推荐
- android设置eclipse中的自动提示功能
菜单window->Preferences->Java->Editor->Content Assist->Enable auto activation 选项要打上勾 (并 ...
- hdu2489 Minimal Ratio Tree
hdu2489 Minimal Ratio Tree 题意:一个 至多 n=15 的 完全图 ,求 含有 m 个节点的树 使 边权和 除 点权和 最小 题解:枚举 m 个 点 ,然后 求 最小生成树 ...
- Linux下可执行程序调试信息的分离及release程序的调试
前两天在群里看到在讨论如何把debug版中的符号表加到release版本中,觉得这个非常有用,所以学习一下. 使用的工具是objcopy. 如果要生成单独的调试信息文件,命令如下: objcopy ...
- 强大的Http监控工具Fidder
软件下载:http://fiddler2.com/get-fiddler 软件学习:http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728 ...
- CSS中position详解与常见应用实现
在web前台开发时候,我们必不可少的会用到postion属性进行布局定位.今天总结了一下position知识点,与常用功能与大家分享,欢迎大家交流指正. 首先我们对postion属性进行详解. 在CS ...
- 为什么要用BitSet
BitSet适用于一类型boolean判断,Java的BitSet在这类型判断中非常高效. 举例说明:在判断前2000万数字中素数个数的程序中,如果使用最基本的素数判断代码: package com; ...
- 单链表的插入删除操作(c++实现)
下列代码实现的是单链表的按序插入.链表元素的删除.链表的输出 // mylink.h 代码 #ifndef MYLINK_H #define MYLINK_H #include<iostream ...
- hadoop拷贝文件时 org.apache.hadoop.ipc.RemoteException异常的解决
1.系统或hdfs是否有空间 2.datanode数是否正常 3.是否在safemode 4.防火墙关闭 5.配置方面 6.把NameNode的tmp文件清空,然后重新格式化NameNode
- Java中@Deprecated、@SupressWarning、@Override的作用
Annotation注解在 Java 中有着很广泛的,他是做为一种标识 为javac所识别.每一个注解 都对应这一个Java类 在java.lang包中 有三个注解 分别是 Deprecated ...
- hdc和hwnd的区别
句柄概念在WINDOWS编程中是一个很重要的概念,在许多地方都扮演着重要的角色.但由此而产生的句柄概念也大同小异,比如:<<Microsoft Windows 3 Develo ...