POJ - 1741 - Tree - 点分治 模板

POJ-1741

题意:

对于带权的一棵树,求树中距离不超过k的点的对数。

思路:

点分治的裸题。 将这棵树分成很多小的树,分治求解。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl ‘\n‘

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFFLL;  //2147483647
const ll nmos = 0x80000000LL;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18
const int mod = 998244353;

const double PI=acos(-1.0);

// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------showtime----------------------*/
            const int maxn = 1e5+9;
            int root = 0,S,mx;
            int n,k;
            int sz[maxn],f[maxn],dis[maxn],cnt;
            bool used[maxn];
            struct node
            {
                int to,w,nx;
            }e[maxn];
            int h[maxn],tot = 0;
            void add(int u,int v,int w){
                e[tot].to = v;
                e[tot].w = w;
                e[tot].nx = h[u];
                h[u] = tot++;
            }
            void getRoot(int u, int fa){
                sz[u] = 1,f[u] = 1;
                for(int i = h[u] ; ~i; i= e[i].nx){
                    int v = e[i].to;
                    if(used[v] || fa == v)continue;
                    getRoot(v,u);
                    sz[u] += sz[v];
                    f[u] = max(f[u] , sz[v]);
                }
                f[u] = max(f[u],S - sz[u]);
                if(f[u] < mx){root = u;mx = f[u];}
            }

            void getDis(int u,int fa,int D){
                for(int i=h[u] ; ~i; i=e[i].nx){
                    int v = e[i].to;
                    if(used[v]||v == fa)continue;
                    dis[++cnt] = D + e[i].w;
                    getDis(v,u,dis[cnt]);
                }
            }

            int getAns(int x,int D){
                dis[cnt = 1] = D;
                getDis(x,0,D);
                sort(dis+1,dis+1+cnt);
                int le = 1,ri =cnt,ans = 0;
                while(le <= ri){
                    if(dis[le] + dis[ri] <= k)ans += ri - le,le++;
                    else ri--;
                }
                return ans;
            }

            int Divide(int x){
                used[x] = true;
                ll ans = getAns(x,0);
                for(int i=h[x]; ~i; i= e[i].nx){
                    int v = e[i].to;
                    if(used[v])continue;
                    ans -= getAns(v,e[i].w);
                    mx = inf,S = sz[v];
                    getRoot(v,x);ans += Divide(root);
                }
                return ans;
            }
int main(){

            while(~scanf("%d%d", &n, &k) && n+k)
            {
                memset(h,-1,sizeof(h));
                memset(used,false,sizeof(used));
                tot = 0;
                for(int i=1; i<n; i++){
                    int u,v,c;
                    scanf("%d%d%d", &u, &v,&c);
                    add(u,v,c);
                    add(v,u,c);
                }
                S = n;mx = inf;
                getRoot(1,-1);
                printf("%d\n",Divide(root));
            }
            return 0;
}

POJ-1741

原文地址:https://www.cnblogs.com/ckxkexing/p/9508386.html

时间: 2024-07-31 22:27:03

POJ - 1741 - Tree - 点分治 模板的相关文章

POJ 1741 Tree ——点分治

[题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ [代码] #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define maxn 20005 #define inf 0x3f3f3f3f using

POJ 1741 Tree 树分治(点分治)

题意:给你一颗带边权树,问你其中 dis(v,u) <= k 的对数 解题思路: 首先推荐大家看 09年国家集训队漆子超 的论文 看到这题  我们可以有三种思路 第一种是枚举起点,遍历整颗树找对数    时间复杂度 为  O(n^2),空间复杂度为 O(n) 第二种是树形dp的思想     每个节点用 长度为 K 数组维护 ,递归求解  时间复杂度为 O(n ^k)空间复杂度 为 O(n) 第三种就是我们要用到的点分治的思想. 这种思想的具体思路是  先找到一个  根  对这个根进行 深搜, 找

[POJ 1741] Tree 点分治

题意 给定 n 个节点的树. 求有多少对节点 (u, v) , 满足 dist(u, v) <= K . n <= 10000. 实现 #include <cstdio> #include <cstring> #include <cstdlib> #include <cctype> #include <algorithm> #include <vector> using namespace std; #define F(i

POJ 1741 Tree(树分治)

去网上搜题解大多数都是说论文,搜了论文看了看,写的确实挺好,直接复制过来. 不过代码中有些细节还是要注意的,参考这篇http://blog.sina.com.cn/s/blog_6d5aa19a0100o73m.html一段 设X为满足i<j且Depth[i]+Depth[j]<=K的数对(i,j)的个数设Y为满足i<j,Depth[i]+Depth[j]<=K且Belong[i]=Belong[j]数对(i,j)的个数那么我们要统计的量便等于X-Y 求X.Y的过程均可以转化为以下

poj 1741 Tree(树的点分治)

poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出来.接着对于每一个子树再次运算.如果不用点分治的技巧,时间复杂度可能退化成\(O(n^2)\)(链).如果对于子树重新选根,找到树的重心,就一定可以保证时间复杂度在\(O(nlogn)\)内. 具体技巧是:首先选出树的重心,将重心视为根.接着计算出每个结点的深度,以此统计答案.由于子树中可能出现重复

Poj 1741 Tree (树的分治)

题目链接: Poj 1741 Tree 这个题目Tle的好苦啊,原来一直是树的重心没找对,Tle好长时间,终于对了,好感动,先贴个代码. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 10010; 8 struct node 9 { 10 int

数据结构(树,点分治):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

POJ 1741 Tree (树上点分治)(楼教主男人八题之一)

题目地址:POJ 1741 树分治第一发! 树分治详情请看漆子超的国家集训队论文,论文传送门 树分治裸题. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #

poj 1744 tree 树分治

Tree Time Limit: 1000MS   Memory Limit: 30000K       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 ve