2015.10.15---2014 Mudanjiang

Source: Internet
Author: User

I've been practicing for a long time.

Over 4 questions.

It's only a supplement.

It's so lazy to be a dog---

A

Sign

B

While practicing, although the correct approach was discussed, but at that time did not feel right at all--

The procedure is to find the diameter of the tree three times

For the first time, find the diameter of the whole tree, locate the midpoint of the diameter, and divide the whole tree into two sub-trees.

The second time, the midpoint of the diameter of the first subtrees tree is calculated.

The third time, find the midpoint of the diameter of the second subtrees tree

-------wrote for several days----finally read the writing of the puzzle---

I wrote the WA and T-----good----rub----AH----

There are two places to write their own time did not get too clear,

First divides the whole subtrees tree into two halves, does not know where to disconnect from

Then is to find the midpoint of time, do not know how to find the diameter is odd, even when the time to deal with,,,

In short,,, that is,,,-----sad------

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<queue>using namespacestd;//Wei Ziji +u >_<Const intINF = (1<< -)-1;Const intMAXN = 5e5+5;intN;intD[maxn],vis[maxn],p[maxn],p1[maxn],p2[maxn];vector<int>G[MAXN];intst,ed;//The end of the diameter found in the ED recordintRr//diametervoidBFsints) {Queue<int>Q; P[s]=0;d [s] =1; RR=1; Ed=s;    Q.push (s);  while(!Q.empty ()) {        intU =Q.front (); Q.pop ();  for(inti =0; i < g[u].size (); i++){            intv =G[u][i]; if(Vis[v])Continue; if(v = = P[u])Continue; D[V]= d[u]+1; P[V]=u;            Q.push (v); if(D[v] >RR) {RR=D[v]; Ed=v; }        }    }}voidsolve () {//the diameter of the whole treememset (Vis,0,sizeof(VIS)); BFS (1); BFS (ed);//for (int i = 1;i <= n;i++) printf ("p[%d] =%d\n", i,p[i]);//printf ("rr =%d ed =%d\n", rr,ed);//for (int i = 1;i <= n;i++) printf ("d[%d] =%d\n", i,d[i]); //find the point that breaks the tree, the midpoint of the tree's diameter.    intMID =Ed; intx =d[ed];  while(x! = rr/2+1) {Mid=P[mid]; X=D[mid]; //printf ("---x =%d\n", x);    }//printf ("Mid =%d\n", mid);    intres =0, C1,C2; //the first sub-treeVis[mid] =1;    BFS (P[mid]); St=Ed;    BFS (ed); C1= Ed;x =d[ed];  while(x! = rr/2+1) {C1=P[C1]; X=D[C1]; } Res= Max (res,rr/2); //the second subtrees treeVis[mid] =0; Vis[p[mid]]=1;    BFS (mid); St=Ed;    BFS (ed); C2= Ed;x =d[ed];  while(x!=rr/2+1) {C2=P[C2]; X=D[C2]; } Res= Max (res,rr/2); printf ("%d%d%d\n", RES,C1,C2);} intMain () {intT; scanf ("%d",&T);  while(t--) {scanf ("%d",&N);  for(inti =1; I <= n;i++) g[i].clear ();  for(intI=1; I < n;i++){            intu,v; scanf ("%d%d",&u,&v);            G[u].push_back (v);        G[v].push_back (U);    } solve (); }    return 0; }
View Code

C

D

DP, and they made the

E

F

G

H

I

Set of formulas, note the time of 0

J

K

Greedy

2015.10.15---2014 Mudanjiang

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.