"CF700B" Connecting universities (idea problem, greed, shortest path on tree)

Source: Internet
Author: User

Test instructions: Give the 2*K nodes on a tree, pair them up, make them the distance between them and maximum.

Idea: The sides of a side if there is no given node on one side will not be passed ...

If there are 1 nodes will be passed 1 times ...

If there are x, Y nodes on each side, they will be passed min (x, y) times

Because the biggest way is to make each road walk the most times

Must be on each side to take one of the rest can only be solved in one side

So click here to Count

That's a big answer, INT64.

1 varHead,vet,next,a,b,c,dep,flag,f:Array[1..500000] ofLongint;2 N,k,tot,i:longint;3 Ans:int64;4 5 procedureAdd (a,b:longint);6 begin7 Inc (TOT);8next[tot]:=Head[a];9vet[tot]:=b;Tenhead[a]:=tot; One End; A  - functionmin (x,y:longint): Longint; - begin the  ifX<y Thenexit (x); - exit (y); - End; -  + procedureDFS (u,fa:longint); - varE,v:longint; + begin Aflag[u]:=1; ate:=Head[u]; -   whileE<>0  Do -  begin -v:=Vet[e]; -   if(V&LT;&GT;FA) and(flag[v]=0) Then -   begin indep[v]:=dep[u]+1; - DFS (v,u); tof[u]:=f[u]+F[v]; +   End; -e:=Next[e]; the  End; * End; $ Panax Notoginseng begin - read (n,k); the   fori:=1  to 2*k Do +  begin A read (c[i]); thef[c[i]]:=1; +  End; -   fori:=1  toN-1  Do $  begin $ read (a[i],b[i]); - Add (A[i],b[i]); - Add (B[i],a[i]); the  End; -Dfs1,-1);Wuyi   fori:=1  toN-1  Do the   ifDep[a[i]]>dep[b[i]] ThenAns:=ans+min (F[a[i]),2*k-F[a[i]]) -    ElseAns:=ans+min (F[b[i]),2*k-F[b[i]]); Wu writeln (ans); -  About End.

"CF700B" Connecting universities (idea problem, greed, shortest path on tree)

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.