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. Android 使用 RemoteViews 发送自定义通知 ,遇到 Couldn't expand RemoteViews问题总结

    在RemoteViews这种调用方式中,你只能使用以下几种界面组件: Layout:     FrameLayout, LinearLayout, RelativeLayout Component: ...

  2. C# string.Format谨慎使用

    string.Format string.Format在处理文本的时候很有用处,但是在使用占位符的时候一定要注意内容中的特殊字符{}. 示例 string.Format("你好{0},这是{ ...

  3. Android 交错 GridView

    原文地址 本文演示在你的 Android 应用程序中显示交错 GridView(Staggered GridView ). 下载 Demo 交错 GridView 交错 GridView 只是具有不等 ...

  4. Lesson 5: Typography in Product Design

    Lesson 5: Typography in Product Design Article 1: Interactive Guide to Blog Typography 布局(Layout) 用空 ...

  5. c-参数(argument)

    In  C,  array arguments behave as though they are passed by reference, and scalar variables and cons ...

  6. 设置Cacti图形标题能显示中文

    1.查看系统是否带有中文字体包 # ls /usr/share/fonts/chinese 如没有则安装 # yum -y install fonts-chinese   2.设置cacti使用的rr ...

  7. java关键字 (jdk6),各自的含义是什么?

    Abstract 抽象的 一个Java语言中的关键字,用在类的声明中来指明一个类是不能被实例化的,但是可以被其它类继承.一个抽象类可以使用抽象方法,抽象方法不需要实现,但是需要在子类中被实现. bre ...

  8. iOS 天气应用代码中文介绍

    天气应用 解释请求参数 q: 表示Location(可以给出城市名字;或者直接给城市的经纬度) 例子:q=beijing 例子 q=48.834,2.394 num_of_days: 需要预报的天数 ...

  9. 【转】#include,#import,@class的区别

    #include         #include  <>    :用于对系统文件的引用,编译器会在系统文件目录下去查找该文件.           #include "xx.h ...

  10. 你好,C++(25)函数调用和它背后的故事5.1.2 函数调用机制

    5.1.2  函数调用机制 在前面的学习中,我们多次提到了“调用函数”的概念.所谓调用函数,就是将程序的执行控制权从调用者(某个函数)交给被调用的函数,同时通过参数向被调用的函数传递数据,然后程序进入 ...