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. Windows消息传递机制具体解释

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka Windows是一个消息(Message)驱动系统.Windows的消息提供了应用程序之间.应 ...

  2. [ES6] Object.assign (with defaults value object)

    function spinner(target, options = {}){ let defaults = { message: "Please wait", spinningS ...

  3. sae storage 使用uploadify插件进行文件批量上传

    uploadify插件在文件上传方面还是很不错的,这不我需要往sae 的storage上上传文件,就用了它.下面我就分享一下如何实现的吧.我们先到官网下载最新的uploadify最新的插件包.在页面中 ...

  4. session过期后自动跳转到登陆页

    项目需要做一个自动登出的功能,查询了网上的资料,一开始准备用session监听做,按照下面方式配置监听器 1.在项目的web.xml文件中添加如下代码: <!--添加Session监听器--&g ...

  5. TreeSet与TreeMap

    TreeSet底层使用的存储容器为TreeMap TreeMap使用红黑树(一种自平衡的排序二叉树)实现,检索效率为O(logn) 红黑树的三个基本操作:左旋.右旋.着色 平衡二叉树:空树或左右子树高 ...

  6. css06背景图片

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. animation-timing-function中的cubic-bezier(n,n,n,n)

    cubic-bezier即为贝兹曲线中的绘制方法.图上有四点,P0-3,其中P0.P3是默认的点,对应了[0,0], [1,1].而剩下的P1.P2两点则是我们通过cubic-bezier()自定义的 ...

  8. mysql source命令导入sql文件效率分析和索引整理

    Query OK, 24918 rows affected (0.90 sec)Records: 24918  Duplicates: 0  Warnings: 0 Query OK, 24923 r ...

  9. Python 中 open()文件操作的方式

    Python的open方法用来打开一个文件.第一个参数是文件的位置和文件名,第二个参数是读写模式: f=open('/1.txt','w') 读写模式的类型有: rU 或 Ua 以读方式打开, 同时提 ...

  10. 使用SOCKET TCP

    刚刚接触SOCKET编程,网上看了一些资料,发现做些简单的应用还是不难.但是要深入了解SOCKET编程还需要系统的看一些书.一般在进程间通信TCP是一种不错的方式. ---XXX TCP链接是面向流的 ...