"BZOJ-2599" Race point Division

Source: Internet
Author: User

2599: [ioi2011]race time limit:70 Sec Memory limit:128 MB
submit:2590 solved:769
[Submit] [Status] [Discuss] Description

To a tree, to each edge. For a simple path, weights and equals K, and the number of edges is the smallest. N <= 200000, K <= 1000000

Input

First row of two integers n, k
Second.. n rows three integers per line represents the ends and weights of a non-forward edge (note numbering starts with 0)

Output

An integer representing the minimum number of edges if there is no such path output-1

Sample Input4 3
0 1 1
1 2 2
1 3 4
Sample Output2
HINT

$N <=200000,k<=1000000$

SourceSolution

The point of the tree for the bare topic, but the practice is a little interesting

The first consideration is:

Normal points, and then each time you start the BFS subtree with the current root to get the Benquan and path lengths of each node in the subtree to the root, and update the answer with previous results and current results

But it looks like it hangs up around 50s and blows up--

Discover that it is not necessary, only need to open a 100W array to record the current distance to the root of $i$ path length of the shortest value

This keeps the tree updated and the answer updated.

The complexity of time is still $o (NLOGN) $

Code
#include <iostream>#include<cmath>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 200010#defineMaxk 1000010#defineINF 0x3f3f3f3fintn,k;structedgenode{intNext,to,val;} edge[maxn<<1];intHead[maxn],cnt=1;voidAddintUintVintW) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v; edge[cnt].val=W;}voidInsertintUintVintW) {Add (u,v,w); add (v,u,w);}intSize[maxn],maxx[maxn],num[maxk],siz,root,ans;BOOLVISIT[MAXN];voidGetroot (intNowintLast ) {Size[now]=1; maxx[now]=0;  for(intI=head[now]; I I=edge[i].next)if(Edge[i].to!=last &&!)Visit[edge[i].to])                {getroot (Edge[i].to,now); Size[now]+=Size[edge[i].to]; Maxx[now]=Max (maxx[now],size[edge[i].to]); } Maxx[now]=max (maxx[now],siz-Size[now]); if(Maxx[now]<maxx[root]) root=Now ;}structnode{intd,t;} ST[MAXN];intS,top;voidDFS (intNowintDisintTtintLast ) {    if(dis>k)return; Ans=min (ans,tt+num[k-dis]); St[top++]=Node{dis,tt};  for(intI=head[now]; I I=edge[i].next)if(Edge[i].to!=last &&!)Visit[edge[i].to]) DFS (Edge[i].to,dis+edge[i].val,tt+1, now);}voidSolve (intNow ) {Root=0; Getroot (now,0); now=Root; S=0, top=0;  for(intI=head[now]; I I=edge[i].next)if(!Visit[edge[i].to]) {DFS (Edge[i].to,edge[i].val,1, now);  for(intJ=s; j<=top-1; J + +) NUM[ST[J].D]=min (num[st[j].d],st[j].t); S=top; }     for(intI=0; i<=top-1; i++) Num[st[i].d]=inf; num[0]=0; Visit[now]=1;  for(intI=head[now]; I I=edge[i].next)if(!visit[edge[i].to]) siz=size[edge[i].to],solve (edge[i].to);}intMain () {n=read (); k=read ();  for(intU,v,w,i=1; i<=n-1; i++) U=read () +1, V=read () +1, w=read (), insert (U,V,W); memset (Num,inf,sizeof(num)); num[0]=0; Ans=inf; Siz=maxx[0]=n; Solve (1); if(Ans!=inf) {printf ("%d\n", ans);return 0;} Puts ("-1"); return 0;}

AC First Channel IOI ovo!

"BZOJ-2599" Race point Division

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.