CSP Week6 ProblemA diameter of the tree to determine the search tree +

CSP solving search Nene tree diameter and tree

Knowledge Overview

In graph theory problem solving, we often encounter the need to solve a Unicom acyclic graph - the diameter of a tree structure (separated by two points furthest distance in the tree).
As shown in the figure, if we want to determine the diameter, can be solved in accordance with the following order:
1, to find any point, and the furthest away from his starting point to find from this point point1
(FIG node with arrows from departure, find the furthest point of the yellow dot)
2, from the yellow starting point, find its farthest point distance. Namely point2.
(FIG starting point of yellow, red spots are several farthest point)
. 3, that is, two diametrically Point1-Point2 two endpoints, the diameter of the tree is not unique
Here Insert Picture Description

Topic Overview

Original lab with a computer (No. 1), recently krypton gold band division Cuckoo East but also for the purchase of laboratory N-1 computers, numbered 2 to N. Every computer connected to a computer with a previously installed cable. But Cuckoo East worry speed is too slow, he wanted to know the i-computer network cable to a maximum length of other computers, but poor cuckoo East just recently suffered a drop of wisdom against cosmic rays, please help him.
Here Insert Picture DescriptionNote: this figure corresponding to the input sample, from this figure you can see, number 1 farthest from the computer is a PC computer 4, the distance between them is 3. No. 4 and No. 5 PC computers are farthest from the point of the computer 2, so the answer is No. 2.5 No. 3 farthest from the computer PC, the computer 3 is so for its answer is 3. The same we can calculate the number 4 and No. 5 PC computer answer is 4.

INPUT & SAMPLE INPUT

Input file contains several test cases. For each test, the first row of an integer N (N <= 10000), there are N-1 the next row, each row two numbers, for the i-th row of the two numbers that represent the number of computers connected to the i computer number and length of the cable between them. The total length of the cable does not exceed 10 ^ 9, separated by a space between each number.
Sample input:

5
1 1
2 1
3 1
1 1

OUTPUT & Sample Output

For each set of test data output line N, the answer i represents the i-th row number of the computer (1 <= i <= N ).
Sample output;

3
2
3
4
4

Title repeat

The title given conditions, there are N points, N-1 edges, and the entire network system which is an apparent link tree structure, the ring is not present. It is required to take the most distant points of each of other points, and Pictured are powerless to FIG. 1, the right side.

Analytical thinking

In a tree structure, the distance required to take the most distant point of the other, through simple analysis shows - equivalent to the distance between the two end points is obtained from the tree, i.e. the distance from the endpoint tree diameter.
Therefore the problem can be transformed into, strike two endpoints in the tree, two endpoints are determined distance to each point, to solve a maximum value comparison.

to sum up

An obvious problem tree, if each time point obtaining high complexity. Thinking can be converted, respectively, after two end points determined, to obtain two end points of a distance to the store, a large value can be selected from size comparison.

Source title

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int flag=1e5+1;
struct Edge
{
    int start;//起点
    int end;//终点
    int nxt;//下一条边的编号
    int value;
}Edges[flag];//边集
int Map[flag];//前向星数组
int key;//边数
void init(int point_number)
{
    key=0;
    for(int i=1;i<=point_number;i++)
    Map[i]=-1;
}
void addEdge(int x,int y,int z)
{
    Edges[key].start=x;
    Edges[key].end=y;
    Edges[key].value=z;
    Edges[key].nxt=Map[x];
    Map[x]=key;
    key++;
}
int max_length[flag];
int vis[flag];//访问数组
int max_point=0;
int maxLength=0;
queue<int> q;   
int pointNumber=0;
void BFS(int x)
{
    for(int i=0;i<=pointNumber;i++)
    {
        vis[i]=0;
        max_length[i]=0;
    }
    max_point=0;
    maxLength=0;
    q.push(x);
    vis[x]=1;
    while(!q.empty())
    {
        int vis_flag=0;//该点的边访问指针
        int head=q.front();//该点的一个哨兵,方便后面计算length,指向这个点
        vis_flag=Map[q.front()];
        q.pop();
        while (vis_flag!=-1)//有下一条边访问
        {
            if(vis[Edges[vis_flag].end]==0)//没访问过
            {
                q.push(Edges[vis_flag].end);
                vis[Edges[vis_flag].end]=1;//标为访问过
                max_length[Edges[vis_flag].end]=max_length[head]+Edges[vis_flag].value;//更新距离
                if(max_length[Edges[vis_flag].end]>maxLength)
                {
                    maxLength=max_length[Edges[vis_flag].end];
                    max_point=Edges[vis_flag].end;
                }
            }
            vis_flag=Edges[vis_flag].nxt;
        }
    }
}
int point1_dis[flag];
int point2_dis[flag];
void copy_dis1()
{
	for(int i=1;i<=pointNumber;i++)
	point1_dis[i]=0;
	for(int i=1;i<=pointNumber;i++)
    point1_dis[i]=max_length[i];
}
void copy_dis2()
{
	for(int i=1;i<=pointNumber;i++)
	point2_dis[i]=0;
	for(int i=1;i<=pointNumber;i++)
    point2_dis[i]=max_length[i];
}
int main()
{
    while(cin>>pointNumber)
    {
    init(pointNumber);
    int value,end_poiont=0;
    for(int i=2;i<=pointNumber;i++)
    {
        cin>>end_poiont>>value;
        addEdge(i,end_poiont,value);
        addEdge(end_poiont,i,value);
    }
    BFS(1);
    int point1=max_point;
    BFS(point1);
    copy_dis1();
    int point2=max_point;
    BFS(point2);
    copy_dis2();
    for(int i=1;i<=pointNumber;i++)
    {
        if(point1_dis[i]>point2_dis[i])
        printf("%d\n",point1_dis[i]);
        else
        printf("%d\n",point2_dis[i]);
    }
	/*for(int i=1;i<=pointNumber;i++)
	{
		int vis_flag=0;
		vis_flag=Map[i];
        cout<<i<<":";
		while(vis_flag!=-1)
		{
			cout<<Edges[vis_flag].end<<" "<<Edges[vis_flag].value<<"--";
            vis_flag=Edges[vis_flag].nxt;
		}
        cout<<endl;
	 }*/	
	}
    return 0;
}
Published 17 original articles · won praise 2 · Views 1652

Guess you like

Origin blog.csdn.net/qq_43942251/article/details/105238254