The diameter of the tree - truant child (Los Valley)

Look at the title:

Chris home phone rings, and there came the teacher Chris anxious voice: "?? Hey, Chris's parents do not any of your children to class, do not want to take the test yet," was told that exam, Chris the anxious parents, they decided to find Chris in the shortest possible time. They told the teacher Chris: "Based on past experience, Chris is now inevitable Shermie hiding in a friend or family Yashiro steal play" King of Fighters "game now, we set off to find Chris from home, once found, we immediately call you. . "Having a bang hung up.

Chris city inhabited by settlements and the N two-way street connecting several strips of settlements, through the streets x takes Tx minutes. I can guarantee, either between two settlements and only a path. Chris home in point C, Shermie and Yashiro were living in point A and point B. Chris Chris teachers and parents have a map of the city, but Chris's parents know the specific location points A, B, C's and Chris's teacher know.

In order to find as soon as Chris, Chris's parents will abide by the following two rules:

  1. If the distance A B C than C near distance, so Chris's parents to go home to find Chris Shermie, if not found, Chris's parents Yashiro go home; and vice versa.
  2. Chris's parents always walk along the only path between two points.

Obviously, the teachers know Chris Chris Chris's parents in the process of looking for more than two will abide by the rules, but because he did not know the specific location of A, B, C, and so now he wants you to tell him, in the worst case Chris parents to spend much time to find Chris?

Meaning of the questions are well understood. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 

How to do it?

I think it is personal, then I can think of tree diameter.

Focus is on how to use?

Simplification Title: a topic is intended: to find a tree three points A, B, C such that the maximum value of the AC AB + (satisfies AC <= BC)

This is not to know how to do it.

AB want the maximum good, obviously the diameter of the tree, no longer than the diameter of a tree in the distance in

Then the maximum AC it, we can point the way to each of the A, B are worth seeking out in dfs diameter of the tree when seeking, whichever is less value in both value (since the first to the more recent point)

Finally, the enumeration again seek optimal decision (maximum), which is actually a greedy thoughts

Proof, then you can go to Los Valley point of view.

Here is the code tightening plate, it may be difficult to understand.

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 const int maxn=500010;
 8 typedef long long ll;
 9 struct node{
10     ll from,to,next,value;
11 }b[maxn];
12 ll n,m,head[maxn],tot=1;
13 void add(ll xx,ll yy,ll zz){
14     b[tot].from=xx;b[tot].to=yy;b[tot].next=head[xx];b[tot].value=zz;head[xx]=tot++;
15 }
16 ll dis[maxn],disa[maxn],disb[maxn],A,B;
17 ll mostlong,JL=-0x3f3f3f3f3f3f3f3f,ID=0;
18 void DFS(ll now,ll fa){
19     for(ll i=head[now];i!=-1;i=b[i].next){
20         ll u=b[i].to;ll da=b[i].value;
21         if(u==fa) continue;
22         dis[u]=dis[now]+da;
23         if(dis[u]>JL){
24             JL=dis[u];ID=u;
25         }
26         DFS(u,now);
27     }
28 }
29 int main(){
30     //freopen("a.in","r",stdin);
31     scanf("%lld%lld",&n,&m);
32     memset(head,-1,sizeof(head));
33     for(ll i=1;i<=m;i++){
34         ll xx,yy,zz;
35         scanf("%lld%lld%lld",&xx,&yy,&zz);
36         add(xx,yy,zz);add(yy,xx,zz);
37     }DFS(1,0);A=ID;
38     memset(dis,0,sizeof(dis));
39     ID=0,JL=-0x3f3f3f3f3f3f3f3f;
40     DFS(A,0);B=ID,mostlong=JL;
41     ID=0,JL=-0x3f3f3f3f3f3f3f3f;
42     for(ll i=1;i<=n;i++)disa[i]=dis[i];
43     memset(dis,0,sizeof(dis));DFS(B,0);
44     for(ll i=1;i<=n;i++)disb[i]=dis[i];
45     ll ans=-0x3f3f3f3f3f3f3f3f;
46     for(ll i=1;i<=n;i++)ans=max(ans,min(disa[i],disb[i]));
47     printf("%lld\n",ans+mostlong);
48     return 0;
49 }
Write a bit rotten

Summary, then, in fact, is the title track of the board, over water.

But note strive out.

1, open long long, and assigned to write eight 3f infinity: 0x3f3f3f3f3f3f3f3f

2, two-way side, when open array must be multiplied by 2

Precautions

 

Guess you like

Origin www.cnblogs.com/DZN2004/p/12662081.html