Title: http://115.28.138.223/view.page?gpid=T24
Test instructions is the maximum value of the distance between any two nodes on a tree. is the diameter of the tree.
First the tree's diameter template is reproduced from http://www.07net01.com/2015/08/908766.html
The template idea is to first arbitrarily determine a point a, added to the queue. And then to this point with a wide search find and it distance from the point B. Then use the same method for point B to find the farthest point from it. This distance is the required diameter.
Implementation: because the diameter of the tree is often a large number of data, so the adjacency table to store data; As for the two endpoint lookup through the BFS implementation, first will first select the starting point a team. Then start searching all the edges of a connection and storing the weights in the dis[] array dis[x]=dis[top]+edge[i].w (DIS[X) is the total weight of the currently searched point associated with the previously searched Edge, Dis[top] Refers to the total weight of the search edge that was previously connected to the point that is currently being searched, EDGE[I].W is the weight of the line from the point top to the currently searched point), completes the search for the current point, and then makes this operation on the next point, each time the search At the same time, the maximum weight is stored in sum so repeated until the queue is empty, so that after the end of the search, the value of sum is a point to the distance from its farthest point B of the sum of the total weight; the second search begins with B (the second search step is the same as the first)
is actually a simple adjacency table application. It's just not very familiar at the bottom. So, feel very witty .....
#include <stdio.h>#include<iostream>#include<string.h>#defineMAXN 10010using namespacestd;inthead[maxn*3];intvis[maxn*3];//determine if each node has been accessed. intdis[maxn*3];//long path for storing each node to the current nodeintque[maxn*3];intEdgenum;intN, M;intans;intsum;structEdge {intV, C, NXT;} EDGE[MAXN*4];//t_t Cry Dizzy ... Incredibly three times times the will tle ... TLE ...voidAddedge (intAintBintc) {edge[edgenum].v=b; EDGE[EDGENUM].C=C; EDGE[EDGENUM].NXT=Head[a]; Head[a]= edgenum++;}voidGetmap () {Edgenum=0; memset (Head,-1,sizeof(head)); for(intI=2; i<=n; ++i) {inttemp; scanf ("%d", &temp); Addedge (temp, I,1); Addedge (i, temp,1); } for(intI=1; i<=m; ++i) {inttemp; scanf ("%d", &temp); Addedge (temp, I+n,1); Addedge (i+n, temp,1); }}voidBFsintnum) {memset (Vis,0,sizeof(VIS)); memset (DIS,0,sizeof(DIS)); Ans=num; Sum=0; intFront =-1, rear =0; que[++front] =num; while(Front >=rear) { inttop = que[front--]; for(intI=head[top]; i!=-1; I=edge[i].nxt) { intv =edge[i].v; if(!Vis[v]) {Dis[v]= Dis[top] +edge[i].c; VIS[V]=1; que[++front] =v; if(Sum <Dis[v]) {Sum=Dis[v]; Ans=v; } } } }}intMain () { while(~SCANF ("%d%d", &n, &m)) {Getmap (); BFS (1); BFS (ANS); printf ("%d\n", sum); } return 0;}
View Code
There is a great God do a kind of, reproduced from: http://www.cnblogs.com/hate13/p/4643573.html
With DP. Find the maximum and the second largest value of the distance between each point and all points. Then their maximum value is the diameter of the tree.
The two distance to initialize the Father node is 0. Then if the child node maximum value of +1 is greater than the maximum value of the parent node. Update the two values of the Father node. Otherwise, if the child node maximum value of +1 is greater than the second largest value of the Father node. Then just update the second largest value.
More witty. There are wood there. (⊙o⊙) ... Amount: Or at lower levels.
#include <stdio.h>#include<iostream>#include<string.h>#defineMAXN 10010using namespacestd;inthead[maxn*3];intN, M;intdp[maxn*4][2];intEdgenum;structEdge {intV,NXT;} EDGE[MAXN*4];//t_t Cry Dizzy ... Incredibly three times times the will tle ... TLE ...voidAddedge (intAintb) {edge[edgenum].v=b; EDGE[EDGENUM].NXT=Head[a]; Head[a]= edgenum++;}voidGetmap () {Edgenum=0; memset (Head,-1,sizeof(head)); for(intI=2; i<=n; ++i) {inttemp; scanf ("%d", &temp); Addedge (temp, i); //Addedge (I, temp, 1); } for(intI=1; i<=m; ++i) {inttemp; scanf ("%d", &temp); Addedge (temp, I+N); //Addedge (I+n, temp, 1); }}voidDfsintu) {dp[u][0] = dp[u][1] =0; for(intI=head[u]; i!=-1; I=edge[i].nxt) { intv =edge[i].v; DFS (v); if(dp[v][0]+1>= dp[u][0]) {dp[u][1] = dp[u][0]; dp[u][0] = dp[v][0]+1; } Else if(dp[v][0]+1> dp[u][1]) {dp[u][1] = dp[v][0]+1; } }}intMain () { while(~SCANF ("%d%d", &n, &m)) {Getmap (); DFS (1); intsum =0; for(intI=1; i<=n+m; ++i) {inttemp = dp[i][0] + dp[i][1]; Sum=Max (temp, sum); } printf ("%d\n", sum); } return 0;}
View Code
CCF 2015-03-4 Network Latency