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<>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)