POJ - 1987 Distance Statistics 树上的分治

题目大意:和poj 1741的那题和类似,求树上节点之间的距离小于等于k的节点对有多少对

解题思路:具体可参考:《分治算法在树的路径问题中的应用——漆子超》

给这题的输入坑了,注意输入,不然会超时

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 40010
int vis[maxn], Sum[maxn], d[maxn], dp[maxn];
int n, k, size, root, m, Ans;
struct node{
    int v, l;
    node() {}
    node(int vv, int ll) {
        v = vv;
        l = ll;
    }
};
vector<node> Node[maxn];
vector<int> dep;

void dfs(int cur, int fa) {
    Sum[cur] = 1;
    dp[cur] = 0;
    for(int i = 0; i < Node[cur].size(); i++) {
        if(!vis[Node[cur][i].v] && Node[cur][i].v != fa) {
            dfs(Node[cur][i].v, cur);
            Sum[cur] += Sum[Node[cur][i].v];
            dp[cur] = max(dp[cur], Sum[Node[cur][i].v]);
        }
    }
    dp[cur] = max(dp[cur], size - Sum[cur]);
    if(dp[cur] < dp[root])
        root = cur;
}

void init() {

    for(int i = 1; i <= n; i++) {
        Node[i].clear();
        vis[i] = 0;
    }

    int x, y, z;
    char c;
    for(int i = 0; i < m; i++) {
        scanf("%d%d%d %c", &x, &y, &z, &c);
        Node[x].push_back(node(y,z));
        Node[y].push_back(node(x,z));
    }
    scanf("%d", &k);
    dp[0] = size = n;
    Ans = root = 0;
}

void getdep(int cur, int fa) {
    dep.push_back(d[cur]);
    for(int i = 0; i < Node[cur].size(); i++)
        if(!vis[Node[cur][i].v] && Node[cur][i].v != fa) {
            d[Node[cur][i].v] = d[cur] + Node[cur][i].l;
            getdep(Node[cur][i].v, cur);
        }
}

int calc(int cur, int Num) {

    dep.clear();
    d[cur] = Num;
    getdep(cur, 0);
    sort(dep.begin(), dep.end());
    int cnt = 0;
    for(int l = 0, r = dep.size() - 1; l < r; )
        if(dep[l] + dep[r] <= k)
            cnt += r - l++;
        else
            r--;
    return cnt;
}

void solve(int cur) {

    Ans += calc(cur, 0);
    vis[cur] = 1;
    for(int i = 0; i < Node[cur].size(); i++) {
        if(!vis[Node[cur][i].v]) {
            Ans -= calc(Node[cur][i].v, Node[cur][i].l);
            dp[0] = size = Sum[Node[cur][i].v];
            root = 0;
            dfs(Node[cur][i].v, 0);
            solve(root);
        }
    }
}

int main() {
    scanf("%d%d", &n, &m);
    init();
    dfs(1,0);
    solve(root);
    printf("%d\n", Ans);
    return 0;
}
时间: 2024-10-12 15:03:09

POJ - 1987 Distance Statistics 树上的分治的相关文章

POJ 1987 Distance Statistics (树上点分治)

题目地址:POJ 1987 点分治模板题,跟POJ 1741几乎一样,.. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdi

POJ 1987 Distance Statistics 树分治

Distance Statistics Description Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decides to ask queries from which he can learn more information. Specifically, he supplies an integer K (1 <= K

POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)

题目大意:(同poj1741,刷一赠一系列) CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 40010 #define INF 0x3f3f3f3f using namespace std; int points,edges,k; int head[MAX],total; int next[MAX <<

POJ 1986 Distance Queries LCA树上两点的距离

题目来源:POJ 1986 Distance Queries 题意:给你一颗树 q次询问 每次询问你两点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + dis(root,v) - 2*dis(roor,LCA(u,v)) 求最近公共祖先和dis数组 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn =

树上点分治 poj 1741

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

POJ1987——Distance Statistics

Distance Statistics Time Limit: 2000MS   Memory Limit: 64000K Total Submissions: 1667   Accepted: 532 Case Time Limit: 1000MS Description Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decide

poj2114 寻找树上存在长度为k点对,树上的分治

寻找树上存在长度为k点对,树上的分治  代码和  这个  差不多 ,改一下判断的就好 #include <iostream> #include <algorithm> #include <cstdio> #include <vector> #include <string.h> using namespace std; const int maxn=10004; int H[maxn],nx[maxn*2],to[maxn*2],numofE,d

POJ 1986 Distance Queries LCA两点距离树

标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + dis(root,v) - 2*dis(roor,LCA(u,v)) 求近期公共祖先和dis数组 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int max

POJ 2468 Apple Tree 树上瞎搞分组背包

昨晚Debug了好久始终找不出哪里错了,今早再一看发现自己已荣升逗比Beta 2.0 Version. 个人感觉此题为HDU 4003 的弱化版. 把每棵子树都看成一类商品,在每类商品中至多选一件.则问题转化为最基本的分组背包问题. dp[s][c][k] c == 1时,表示在s结点不返回时走K的最大收益,c == 0时,表示在s结点重新返回时走k步的最大收益. 可以dfs从底到顶更新dp.值得一提的是,要保证每个结点的dp[s][c][k]只会被dfs一次,即不能重复更新. 因为重复的dfs