Tree POJ

来源:互联网 发布:最好的中国象棋软件 编辑:程序博客网 时间:2024/06/09 00:40

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

思路:点分治模板

代码如下

#include<iostream>#include<algorithm>#include<string>#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<cstring>using namespace std;const int maxn=1e4+5;struct Edge//链式向前星存边{    int to,cost;    int next;}edge[maxn*2];int tol,head[maxn];int vis[maxn];int size[maxn];//子树的大小 int maxv[maxn];//最大孩子节点的size int dis[maxn];int ans,root,MAX;int N,K;int num;void init(){    tol=0;    ans=0;    memset(head,-1,sizeof(head));    memset(vis,0,sizeof(vis));}void add(int u,int v,int w){    edge[tol].to=v;    edge[tol].cost=w;    edge[tol].next=head[u];    head[u]=tol++;}//处理子树大小 void dfs_size(int u,int f){    size[u]=1;    maxv[u]=0;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int to=edge[i].to;        if(to==f||vis[to]) continue;        dfs_size(to,u);        size[u]+=size[to];        maxv[u]=max(maxv[u],size[to]);      } }//找重心 void dfs_root(int r,int u,int f){    maxv[u]=max(maxv[u],size[r]-size[u]);//size[r]-size[u]是u上面部分的树的尺寸,跟u的最大孩子比,找到最大孩子的最小差值节点    if(maxv[u]<MAX)     {        MAX=maxv[u];        root=u;    }    for(int i=head[u];i!=-1;i=edge[i].next)    {        int to=edge[i].to;        if(to==f||vis[to]) continue;        dfs_root(r,to,u);    }}//求每个点离重心的距离 void dfs_dis(int u,int d,int f){    dis[num++]=d;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int to=edge[i].to;        if(to==f||vis[to]) continue;        dfs_dis(to,d+edge[i].cost,u);     } }//计算以u为根的子树中有多少点对的距离小于等于Kint calc(int u,int d){    int ret=0;    num=0;    dfs_dis(u,d,0);    sort(dis,dis+num);    int i=0,j=num-1;    while(i<j)    {        while(dis[i]+dis[j]>K&&i<j)        j--;        ret+=j-i;        i++;    }    return ret;}void dfs(int u){    MAX=N;    dfs_size(u,0);    dfs_root(u,u,0);    ans+=calc(root,0);    vis[root]=1;    for(int i=head[root];i!=-1;i=edge[i].next)    {        int to=edge[i].to;        if(!vis[to])        {            ans-=calc(to,edge[i].cost);            dfs(to);        }    }}int main()  {      while(scanf("%d%d",&N,&K)!=EOF&&N&&K)    {        int u,v,w;        init();         for(int i=1;i<N;i++)        {            scanf("%d%d%d",&u,&v,&w);            add(u,v,w);            add(v,u,w);        }        dfs(1);        printf("%d\n",ans);             }    return 0;  }