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 <stdio.h>
#include <time.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
//#pragma comment(linker, "/STACK:1024000000")
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=50000+10;
int head[MAXN], cnt, ans, min1, root, tot;
int siz[MAXN], dep[MAXN], vis[MAXN], d[MAXN];
int n, k;
struct node {
        int v, w, next;
} edge[MAXN<<1];
void add(int u, int v, int w)
{
        edge[cnt].v=v;
        edge[cnt].w=w;
        edge[cnt].next=head[u];
        head[u]=cnt++;
}
void init()
{
        memset(head,-1,sizeof(head));
        cnt=ans=0;
        memset(vis,0,sizeof(vis));
}
void getroot(int u, int fa, int s)
{
        int i, max1=0;
        siz[u]=1;
        for(i=head[u]; i!=-1; i=edge[i].next) {
                int v=edge[i].v;
                if(v==fa||vis[v]) continue ;
                getroot(v,u,s);
                siz[u]+=siz[v];
                max1=max(max1,siz[v]);
        }
        max1=max(max1,s-siz[u]);
        if(min1>max1) {
                min1=max1;
                root=u;
        }
}
void getdep(int u, int fa)
{
        d[tot++]=dep[u];
        siz[u]=1;
        for(int i=head[u]; i!=-1; i=edge[i].next) {
                int v=edge[i].v;
                if(v==fa||vis[v]) continue ;
                dep[v]=dep[u]+edge[i].w;
                getdep(v,u);
                siz[u]+=siz[v];
        }
}
int Cal(int u, int len)
{
        int l, r, res=0;
        tot=0;
        dep[u]=len;
        getdep(u,-1);
        sort(d,d+tot);
        r=tot-1;
        for(l=0; l<tot; l++) {
                while(d[r]+d[l]>k) r--;
                if(r<=l) break;
                res+=r-l;
        }
        return res;
}
void work(int u)
{
        ans+=Cal(u,0);
        vis[u]=1;
        for(int i=head[u]; i!=-1; i=edge[i].next) {
                int v=edge[i].v;
                if(vis[v]) continue ;
                ans-=Cal(v,edge[i].w);
                min1=INF;
                getroot(v,-1,siz[v]);
                work(root);
        }
}
int main()
{
        int u, v, w, i, m;
        char c;
        scanf("%d%d",&n,&m);
        init();
        for(i=0; i<m; i++) {
                scanf("%d%d%d %c",&u,&v,&w,&c);
                add(u,v,w);
                add(v,u,w);
        }
        scanf("%d",&k);
        min1=INF;
        getroot(1,-1,n);
        work(root);
        printf("%d\n",ans);
        return 0;
}
时间: 2024-10-21 18:05:44

POJ 1987 Distance Statistics (树上点分治)的相关文章

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[

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