【poj1741】【Tree】,poj1741tree

來源:互聯網
上載者:User

【poj1741】【Tree】,poj1741tree
Tree

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 13068 Accepted: 4195
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

這道題是樓教主的男人八題
題意是這樣的:給你一棵樹,讓你求出這棵樹中的距離<=k的點對的數目。
這是一道點分的模板題,和聰聰可可一樣,唯一不同的只是在calc上。
我們考慮我們已經得到了這棵樹的子樹中的點到重心的距離,那麼我們怎樣找到距離<=k的點對?
暴力枚舉?? 顯然會逾時。
所以這道題的不同就是我們需要對於所有找到的距離進行排序。
這樣我們從左右端點開始向中間找,如果遇到了某一段的左端點和右端點的和<=k,那麼說明在這一段中的點都符合,就擴大左端點。
如果發現左端點於右端點的和大於k,就縮小右端點。
這樣我們就可以統計出來了。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=10100;const int inf=210000000;int point[N],next[N*4],sum,son[N],n,k,root,f[N],deep[N],ans,dis[N],tot;struct S{int st,en,va;}aa[N*4];bool use[N];inline void add(int x,int y,int z){    tot+=1;next[tot]=point[x];point[x]=tot;    aa[tot].st=x;aa[tot].en=y;aa[tot].va=z;    tot+=1;next[tot]=point[y];point[y]=tot;    aa[tot].st=y;aa[tot].en=x;aa[tot].va=z;}/*-------找重心--------*/ inline void get_root(int x,int fa){    int i,u;    son[x]=1;f[x]=0;    for(i=point[x];i;i=next[i])      if(use[aa[i].en]&&aa[i].en!=fa){        u=aa[i].en;        get_root(u,x);        son[x]+=son[u];        f[x]=max(f[x],son[u]);      }    f[x]=max(f[x],sum-son[x]);    if(f[x]<f[root]) root=x;}/*-------計算距離---------*/ inline void get_deep(int x,int fa){    int i;    deep[0]+=1;    deep[deep[0]]=dis[x];    for(i=point[x];i;i=next[i])      if(use[aa[i].en]&&aa[i].en!=fa){        dis[aa[i].en]=dis[x]+aa[i].va;        get_deep(aa[i].en,x);      }}/*---------分治-----------*/ inline int calc(int x,int now){    deep[0]=0;dis[x]=now;    get_deep(x,0);    sort(deep+1,deep+deep[0]+1);    int l=1,r=deep[0],all=0;    while(l<=r){        if(deep[l]+deep[r]<=k){            all+=r-l;            l+=1;        }        else r-=1;    }    return all;}inline void work(int x){    int i;    ans+=calc(x,0);    use[x]=false;    for(i=point[x];i;i=next[i])      if(use[aa[i].en]){        ans-=calc(aa[i].en,aa[i].va);        root=0;        sum=son[aa[i].en];        get_root(aa[i].en,0);        work(root);      }}int main(){    int i,j,x,y,z;    while(scanf("%d%d",&n,&k)==2){        if(n==0&&k==0) break;        tot=root=ans=0;             memset(son,0,sizeof(son));        memset(use,1,sizeof(use));        memset(point,0,sizeof(point));        memset(next,0,sizeof(next));        for(i=1;i<n;++i){            scanf("%d%d%d",&x,&y,&z);            add(x,y,z);        }        sum=n;f[0]=inf;        get_root(1,0);        work(root);        printf("%d\n",ans);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.