POJ1741 Tree (点分治)

Tree

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 25772   Accepted: 8566

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

code

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4
 5 using namespace std;
 6
 7 const int N = 10100;
 8
 9 struct Edge{
10     int to,nxt,w;
11 }e[N<<1];
12 int head[N],son[N],f[N],deth[N],d[N];
13 bool vis[N];
14 int ans,tot,Root,sum,n,k;
15
16 void init() {
17     memset(head,0,sizeof(head));
18     memset(vis,false,sizeof(vis));
19     ans = tot = Root = 0;
20 }
21 void add_edge(int u,int v,int w) {
22     e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
23 }
24 void getroot(int u,int pa) {
25     son[u] = 1;f[u] = 0;
26     for (int i=head[u]; i; i=e[i].nxt) {
27         int v = e[i].to;
28         if (v==pa||vis[v]) continue;
29         getroot(v,u);
30         son[u] += son[v];
31         f[u] = max(f[u],son[v]);
32     }
33     f[u] = max(f[u],sum-son[u]);
34     if (f[u] < f[Root]) Root = u;
35 }
36 void getdeth(int u,int pa) {
37     deth[++deth[0]] = d[u];
38     for (int i=head[u]; i; i=e[i].nxt) {
39         int v = e[i].to,w = e[i].w;
40         if (v==pa||vis[v]) continue;
41         d[v] = d[u] + w;
42         getdeth(v,u);
43     }
44 }
45 int calcc(int u,int w) {
46     deth[0] = 0;
47     d[u] = w;
48     getdeth(u,0);
49     sort(deth+1,deth+deth[0]+1);
50     int l = 1,r = deth[0],ret = 0;
51     while (l < r) {
52         if (deth[l] + deth[r] <= k) ret += r-l,l++;
53         else r--;
54     }
55     return ret;
56 }
57 void work(int u) {
58     ans += calcc(u,0);
59     vis[u] = 1;
60     for (int i=head[u]; i; i=e[i].nxt) {
61         int v = e[i].to,w = e[i].w;
62         if (vis[v]) continue;
63         ans -= calcc(v,w);
64         sum = son[v];
65         Root = 0;
66         getroot(v,0);
67         work(Root);
68     }
69 }
70 int main() {
71     while (scanf("%d%d",&n,&k)!=EOF && !(n==0&&k==0)) {
72         init();
73         for (int a,b,c,i=1; i<n; ++i) {
74             scanf("%d%d%d",&a,&b,&c);
75             add_edge(a,b,c);add_edge(b,a,c);
76         }
77         f[0] = 1e9;
78         sum = n;
79         getroot(1,0);
80         work(Root);
81         printf("%d\n",ans);
82     }
83     return 0;
84 }

原文地址:https://www.cnblogs.com/mjtcn/p/8151250.html

时间: 2024-08-02 23:34:03

POJ1741 Tree (点分治)的相关文章

POJ1741:Tree——点分治

题面 POJ1741 解析  刚学了点分治,练一练模版题 过程就不多说了,主要说说细节 在每次查询下一棵子树时, 传进去的整棵子树大小是上一次的$siz$, 这个数据其实是错的, 但好像并不影响时间复杂度, 这样的话找重心就必须找最大子树最小的点了,否则会错.因此需要存一个当前最大子树最小的点的最大子树的大小, 以及当前的重心, 每次找重心之前,前者要赋为$inf$ 在每次统计以当前点为$lca$的链的答案时, 要先把这个重心加入数组中, 再按$dis$排序, 然后用双指针法或是其他可行的方法统

poj1741 Tree 点分治

入门题,算是对树分治有了初步的理解吧. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<vector> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using nam

[bzoj1468][poj1741]Tree[点分治]

可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树的值,这样算出的就是与这个重心有关的情况的答案,比如这道题,求路径,那么就考虑在重心所在的子树中所有的路径减去不过重心的路径就是过重心的路径了.之前重心没找对...poj时间卡的紧就T了.. 1 #include <iostream> 2 #include <algorithm> 3

[poj1741]Tree(点分治+容斥原理)

题意:求树中点对距离<=k的无序点对个数. 解题关键:树上点分治,这个分治并没有传统分治的合并过程,只是分成各个小问题,并将各个小问题的答案相加即可,也就是每层的复杂度并不在合并的过程,是在每层的处理过程. 此题维护的是树上路径,考虑点分治. 点分治的模板题,首先设点x到当前子树跟root的距离为,则满足${d_x} + {d_y} \le k$可以加进答案,但是注意如果x,y在同一棵子树中,就要删去对答案的贡献,因为x,y会在其所在的子树中在计算一次.同一棵子树中不必考虑是否在其同一棵子树中的

[LeetCode] Convert Sorted List to Binary Search Tree(分治)

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 方法:为了使BST高度平衡,要找链表中的中值作为当前根节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

树分治基础模板以及树的重心(poj1741 tree)

好久没有更新博文了,这里更新一发~~ 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

POJ-1741 Tree 【点分治】

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 e

poj1741 Tree(点分治)

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 e

[poj1741 Tree]树上点分治

题意:给一个N个节点的带权树,求长度小于等于K的路径条数 思路:选取一个点作为根root,假设f(root)是当前树的答案,那么答案来源于两部分: (1)路径不经过root,那么就是完全在子树内,这部分可以递归统计 (2)路径经过root,这部分可以通过容斥原理统计,具体见有关点分治资料... 点分治有个特点,当考虑的问题由根这个子树转为儿子的子树时,可以选取任意点作为新的根,只要它在儿子的子树内,这就使得我们可以通过选取特别的点使得树深度更小,这个点就是树的重心(在程序里面是不断找子树的重心)

POJ1741 Tree(树分治)

题意: 求树上距离小于等于K的点对有多少个 思路: 每次分治,我们首先算出重心,为了计算重心,需要进行两次dfs,第一次把以每个结点为根的子树大小求出来,第二次是从这些结点中找重心 找到重心后,需要统计所有结点到重心的距离,看其中有多少对小于等于K 但是这些求出来满足小于等于K的里面只有那些路径经过重心的点对才是有效的,也就是说在同一颗子树上的肯定不算数的,所以对每颗子树,把子树内部的满足条件的点对减去. /* ******************************************