Tree
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 12276   Accepted: 3886

Description

Give a tree with n vertices,each edge has a length(positive integer less than 1001). 
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 input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l. 
The last test case is followed by two zeros. 

Output

For each test case output the answer on a single line.

Sample Input

5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0

Sample Output

8

题意:求树上距离小于等于k的点对数。。

我是用 点的分治做的,,据说还可以用启发式合并做,,附上链接http://blog.csdn.net/asdfgh0308/article/details/39845489。。挖个坑。

定义树的重心 s 为 删除s点后的 最大子树的点数 小于n/2。  那么对于任意满足条件的点对 有两种情况,,

其路径 1要么经过s  2要么不经过s。。

对于1 我们只需要 求出 以s为根的子树的点到s的距离即可。。

对于2  可以递归处理 分解为 多个1。。然后就可以求出来了。。

复杂度为nlogn*logn

 #include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 1e4+;
struct Edge
{
int to, len;
Edge (int _x, int _len)
{
to = _x;
len = _len;
}
};
vector<Edge>G[maxn << ];
int siz[maxn];
bool vis[maxn];
void Get_size(int root, int father)
{
siz[root] = ;
for (int i = ; i < G[root].size(); i++)
{
int v = G[root][i].to;
if (v == father || vis[v] == true)
continue;
Get_size(v, root);
siz[root] += siz[v];
}
}
typedef pair<int,int>pii;
pii FindGravity(int root, int father, int t)
{
pii res = make_pair(inf, -);
int m = , sum = ;
for (int i = ; i < G[root].size(); i++)
{
int v = G[root][i].to;
if (v == father || vis[v] == true)
continue;
res = min(res, FindGravity(v, root, t));
m = max(m, siz[v]);
sum += siz[v];
}
m = max(m, t-sum);
res = min(res, make_pair(m, root));
return res;
}
void Get_len(int root, int father, int d, vector<int>&len)
{
len.push_back(d);
for (int i = ; i < G[root].size(); i++)
{
int v = G[root][i].to;
if (v == father || vis[v] == true)
continue;
Get_len(v, root, d+G[root][i].len, len);
}
}
int K;
int cnt_pair(vector<int>&ds)
{
int res = ;
sort (ds.begin(), ds.end());
int j = ds.size() - ;
for (int i = ; i < ds.size(); i++)
{
while (j > i && ds[i] + ds[j] > K)
{
j--;
}
res += (j > i ? j - i : );
}
return res;
}
int solve(int root)
{
int ans = ;
Get_size(root, -);
int s = FindGravity(root, -, siz[root]).second;
if (s == -)
return ;
vis[s] = true;
for (int i = ; i < G[s].size(); i++)
{
int v = G[s][i].to;
if (vis[v] == true)
continue;
ans += solve(v);
}
vector<int>ds;
ds.push_back();
for (int i = ; i < G[s].size(); i++)
{
int v = G[s][i].to;
if (vis[v] == true)
continue;
vector<int>rds;
Get_len(v, s, G[s][i].len, rds);
ans -= cnt_pair(rds);
ds.insert(ds.end(), rds.begin(), rds.end());
}
ans += cnt_pair(ds);
vis[s] = false;
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n;
while ( scanf ("%d%d", &n, &K), n && K)
{
int u, v, c;
for (int i = ; i <= n; i++)
G[i].clear();
for (int i = ; i < n-; i++)
{
scanf ("%d%d%d", &u, &v, &c);
G[u].push_back(Edge(v,c));
G[v].push_back(Edge(u,c));
}
printf("%d\n", solve(n/+));
}
return ;
}

②第二种姿势,,200ms左右

 #include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 1e4+;
struct Edge
{
int to, len, next;
}e[maxn << ];
int head[maxn], tot, N, K;
void add_edge(int u, int v, int c)
{
e[tot].to = v;
e[tot].len = c;
e[tot].next = head[u];
head[u] = tot++;
}
int siz[maxn],Mtree[maxn]; // Mtree为最大子树的大小 siz为子树的大小
bool vis[maxn];
int center;
void FindGravity(int u, int father, int cnt) // 查找重心 center
{
siz[u] = ;
Mtree[u] = ;
for (int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
if (v == father || vis[v] == true)
continue;
FindGravity(v, u, cnt);
siz[u] += siz[v];
Mtree[u] = max(Mtree[u], siz[v]);
}
Mtree[u] = max(cnt - siz[u], Mtree[u]);
if (Mtree[center] > Mtree[u])
center = u;
}
int S[maxn], dep[maxn], top;
void Get_len(int u, int father, int d)
{
S[top++] = d;
for (int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
if (vis[v] == true || v == father)
continue;
// dep[v] = dep[u] + e[i].len;
Get_len(v, u, d+e[i].len);
}
}
int Get_cnt(int u, int d)
{
int res = ;
top = ;
//dep[u] = d;
Get_len(u, , d);
sort (S, S+top);
int j = top - ;
for (int i = ; i < top; i++)
{
while (j > i && S[i] + S[j] > K)
j--;
res += (j > i ? j-i : );
}
return res;
}
int ans;
void solve(int r)
{
vis[r] = true;
ans += Get_cnt(r, );
for (int i = head[r]; ~i; i = e[i].next)
{
int v = e[i].to;
if (vis[v] == true)
continue;
ans -= Get_cnt(v, e[i].len);
center = ;
FindGravity(v, r, siz[v]);
solve(center);
}
}
void init()
{
tot = ;
Mtree[] = N;
memset(head, -, sizeof(head));
memset(vis, false, sizeof(vis));
center = ;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while (scanf ("%d%d", &N,&K), N && K)
{
init();
int u, v, c;
for (int i = ; i < N-; i++)
{
scanf ("%d%d%d", &u, &v, &c);
add_edge(u, v, c);
add_edge(v, u, c);
}
ans = ;
FindGravity(N/+, -, N);
solve(center);
printf("%d\n", ans);
}
return ;
}

POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数的更多相关文章

  1. POJ1741——Tree(树的点分治)

    1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...

  2. poj1741 树上距离小于等于k的对数 点分治 入门题

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

  3. 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 ...

  4. POJ1741 Tree 树分治模板

    http://poj.org/problem?id=1741   题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数.   dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...

  5. POJ 1741 Tree(树的点分治,入门题)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description ...

  6. POJ 1741 Tree 求树上路径小于k的点对个数)

                                                                                                 POJ 174 ...

  7. 洛谷 P3806 【模板】点分治1-树分治(点分治,容斥版) 模板题-树上距离为k的点对是否存在

    P3806 [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入格式 n,m 接下来n-1条边a,b,c描述a到b有一条长度 ...

  8. P3806 离线多次询问 树上距离为K的点对是否存在 点分治

    询问树上距离为k的点对是否存在 直接n^2暴力处理点对 桶排记录 可以过 #include<cstdio> #include<cstring> #include<algo ...

  9. 【点分治】【路径小于等于k的条数】【路径恰好等于k是否存在】

    POJ1741:Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29574   Accepted: 9915 Des ...

随机推荐

  1. 【Hibernate步步为营】--继承映射具体解释

    上篇文章讨论了多对多映射,在使用多对多映射时重点是使用<many-to-many>标签,并在标签的两端加入外键这样在生成关系时会创建两个关系之间的关系表,通过关系表来维护它们之间的关系,另 ...

  2. (转)解析php中die(),exit(),return的区别

    本篇文章是对php中die(),exit(),return的区别进行了详细的分析介绍,需要的朋友参考下     die()停止程序运行,输出内容exit是停止程序运行,不输出内容return是返回值d ...

  3. 增加Android模拟器的内存

    1,在window中,打开'C:\Users\Administrator\.android\avd\4.4.2.avd\config.ini'文件(我的是win7,xp的貌似不是'Users',是'D ...

  4. linux 分区

    1.硬盘分区分为基本分区和扩展分区, 扩展分区分下去就是逻辑分区,而且逻辑分区没有数量上的限制. 2.查看linux系统分区具体情况 fdisk - l 3.查看某个目录是哪个分区下的 df /boo ...

  5. 禁止Windows远程桌面拷贝文件

    通过组策略,我们可以解决以上问题,开始菜单运行输入gpedit.msc,进入“计算机配置”项,按顺序进入“管理模板”.“windows组件”.“终端服务”.“客户端/服务器数据重定向”.在详细描述栏中 ...

  6. php pdo_mysql使用方法

    <?php $dsn='mysql:host=127.0.0.1;port=3306;dbname=bisai'; $username='root'; $password=''; $driver ...

  7. Python文件之----CSV

    # -*- coding:utf-8 -*- ''' Created on 2015年4月20日 @author: liuxue ''' import csv import sys reload(sy ...

  8. 在MyEclipse中设置Source folders和output folder

    在一个项目中可能会有多个资源文件,它们共同编译输出到输出文件.那么除了默认的src以外,如何把其他文件设置成资源文件(Source folders)呢?

  9. ubuntu11.10(TQ210)下移植boa服务器

    平台:ubuntu11.10 一.下载源码包www.boa.org   boa-0.94.13.tar.gz 二.解压,在其src目录下生产makefile #tar xvfz  boa-0.94.1 ...

  10. 【PHP】新浪、淘宝的地区 API调用

    /推荐使用新浪的: $ch = curl_init(); $url = "http://ip.dpool.sina.com.cn/iplookup/iplookup.php?format=j ...