国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

【POJ1741】樹中點(diǎn)對(duì)統(tǒng)計(jì) 點(diǎn)分治

2019-11-06 06:35:08
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

題目描述

  給定一棵N(1<=N<=100000)個(gè)結(jié)點(diǎn)的帶權(quán)樹,每條邊都有一個(gè)權(quán)值(為正整數(shù),小于等于1001)。定義dis(u,v)為u,v兩點(diǎn)間的最短路徑長(zhǎng)度,路徑的長(zhǎng)度定義為路徑上所有邊的權(quán)和。再給定一個(gè)K(1<=K<=10^9),如果對(duì)于不同的兩個(gè)結(jié)點(diǎn)u,v,如果滿足dist(u,v)<=K,則稱(u,v)為合法點(diǎn)對(duì)。求合法點(diǎn)對(duì)個(gè)數(shù)。

題目大意

  求樹中距離小于k的點(diǎn)對(duì)個(gè)數(shù)

數(shù)據(jù)范圍

對(duì)于50%的數(shù)據(jù),n<=1000,k<=1000; 對(duì)于100%的數(shù)據(jù),n<=100000,k<=10^9;

樣例輸入

5 4 1 2 3 1 3 1 1 4 2 3 5 1

樣例輸出

8

解題思路

不寫了233

代碼

#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <ctime>#define Maxn 100005using namespace std;inline int Getint(){int x=0,f=1;char ch=getchar();while('0'>ch||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}int fa[Maxn],h[Maxn],size[Maxn],dep[Maxn];bool vis[Maxn];int n,k,cnt=0,L,r,Min;struct node{int to,next,v;}e[Maxn*2];void AddEdge(int x,int y,int v){e[++cnt]=(node){y,h[x],v};h[x]=cnt;}void Init(){ n=Getint(),k=Getint(); for(int i=1;i<n;i++){ int x,y,v; x=Getint(),y=Getint(),v=Getint(); fa[y]=x; AddEdge(x,y,v); AddEdge(y,x,v); } memset(vis,0,sizeof(vis));}int dfssize(int u,int PRe){ size[u]=1; for(int p=h[u];p;p=e[p].next){ int y=e[p].to; if(vis[y]||y==pre)continue; size[u]+=dfssize(y,u); } return size[u];}void Getroot(int u,int pre,int tot,int &root){ int Max=tot-size[u]; for(int p=h[u];p;p=e[p].next){ int y=e[p].to; if(vis[y]||y==pre)continue; Getroot(y,u,tot,root); Max=max(Max,size[y]); } if(Max<Min){ Min=Max; root=u; }}void Getlen(int u,int pre,int d){ dep[r++]=d; for(int p=h[u];p;p=e[p].next){ int y=e[p].to; if(vis[y]||y==pre)continue; Getlen(y,u,d+e[p].v); }}int Calc(int L,int r){ sort(dep+L,dep+r); int ret=0,Pos=r-1; for(int i=L;i<r;i++){ if(dep[i]>k)break; while(Pos>=L&&dep[i]+dep[Pos]>k)Pos--; ret+=Pos-L+1; if(Pos>i)ret--; } return ret/2;}int Solve(int u){ int tot=dfssize(u,0),ret=0,root; Min=0x7fffffff; Getroot(u,0,tot,root); vis[root]=true; for(int p=h[root];p;p=e[p].next){ int y=e[p].to; if(vis[y])continue; ret+=Solve(y); } L=r=0; for(int p=h[root];p;p=e[p].next){ int y=e[p].to; if(vis[y])continue; Getlen(y,root,e[p].v); ret-=Calc(L,r); L=r; } ret+=Calc(0,r); for(int i=0;i<r;i++) if(dep[i]<=k)ret++; else break; vis[root]=false; return ret;}int main(){ Init(); cout<<Solve(1)<<"/n";}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高密市| 阿图什市| 松江区| 鹤壁市| 丹阳市| 南涧| 铁岭县| 黑水县| 东海县| 望奎县| 庐江县| 右玉县| 岳阳县| 嫩江县| 南开区| 福建省| 莱西市| 江油市| 赤城县| 乐亭县| 庄河市| 营口市| 凌海市| 会宁县| 龙江县| 屏东市| 永德县| 巫山县| 甘南县| 东莞市| 文安县| 海口市| 乌兰察布市| 通辽市| 海丰县| 炎陵县| 新干县| 福建省| 巍山| 西林县| 枝江市|