GCPC11j Time to live

As you might know, most computer networks are organized in a tree-like fashion, i.e. each computer is reachable by each other computer but only over one, unique path.

The so-called Time to live (TTL) specifies after how many hops a network packet is dropped if it has not reached its destination yet. The purpose of the TTL is to avoid situations in which a packet circulates through the network caused by errors in the routing tables.

The placement of a router that connects the network to another network is optimal when the maximal needed TTL for packets that are sent from this router to any other computer within the same network is minimal. Given a network as specified above, you should calculate the maximal needed TTL in this network if you can select the computer that should be used as router.

Input

The first line of the input consists of the number of test cases c that follow (1 ≤ c ≤ 100). Each test case starts with a line specifying N, the number of computers in this network (1 < N ≤ 105). Computers are numbered from 0 to N-1. Then follow N-1 lines, each specifying a network connection by two numbers a and b which means that computer a is connected to computer b and vice versa, of course (0 ≤ a,b < N).

Output

For each test case in the input, print one line containing the optimal TTL as specified above.

Example
Input:
3
2
1 0
5
3 2
2 1
0 2
2 4
9
3 1
6 5
3 4
0 3
8 1
1 7
1 6
2 3

Output:
1
1
2
——————————————————————————悲伤————————————————————————————————
  这道题的大意是说你有一个树形图,需要找到一个节点,这个节点到任意点的最大距离最小。
  那么我们首先得知道这个树形图的直径,换句话说,就是所有两点距离之间的最大值,那么这个节点就可以设在最大的路径的中点。可以保证没有任何一个点能优于其自身。
  然后就是求最大距离的方法:随便找一个节点x,dfs找到离它最远的点y,然后对于点y,dfs找到离它最远的点z,yz之间的距离即是该树形图的直径。
  以下是一句话简单的结论:
  设最长距离一端为y,另外一端为z.
  树形图中任何一个点x,离其最远的点要不就是y,要不就是z.
    如果该点在最长路径上,不存在另外的点p使得xp>xz||xp>xy,若有,则xy+xp>xy+xz,前提不成立。
    若该点不在最长路径上,那么存在且只存在一条路径,使得该点能链接到最长路径上面的一个点j上。
    然后该点的最长距离同j的情况相同,若不是,则说明有点p,使得xp>xy||xp>xz,这个结论同上与前提违背。
  以上即是这个方法的证明。
 
——————————————————————code————————————————————————————————————

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<climits>
#include<map>
#include<stack>
#define file_in freopen("input.txt","r",stdin)
#define MAX 100005
#define maxn 5005
using namespace std;
#define LL long long
#define FF(x,y) for(int i=x;i<y;i++)
vector<vector<int>>sto;
int dis[MAX] = { 0 };
int vst[MAX] = { 0 };
int cnt = 0;
int maxx= 0;
void dfs(int id, int dis)
{
    if (vst[id])return;
    vst[id] = 1;
    if (dis > maxx)
    {
        cnt = id;
        maxx = dis;
    }
    for (int i = 0; i < sto[id].size(); i++)
    {
        dfs(sto[id][i], dis + 1);
    }
}
int main()
{
    int t;
    scanf("%d", &t);
    for (int i = 0; i < t; i++)
    {
        int n;
        sto.clear();
        scanf("%d", &n);
        sto.resize(n);
        for (int i = 1; i < n; i++)
        {
            int x, y;
            scanf("%d %d", &x, &y);
            sto[x].push_back(y);
            sto[y].push_back(x);
        }
        memset(vst, 0, sizeof(vst));
        maxx = 0;
        dfs(0, 0);
        memset(vst, 0, sizeof(vst));
        maxx = 0;
        dfs(cnt, 0);
        //cout << maxx << endl;
        if (maxx % 2 == 0) cout << maxx / 2 << endl;
        else cout << (maxx+1) / 2 << endl;
    }
}
/*
3
5
3 2
2 1
0 2
2 4
9
3 1
6 5
3 4
0 3
8 1
1 7
1 6
2 3
*/

  
 
posted @ 2017-02-28 20:19  duskcloudxu  阅读(237)  评论(0编辑  收藏  举报