【 LightOJ - 1094】Farthest Nodes in a Tree(求树的直径)链式向前星 + DFS or BFS

11 篇文章 0 订阅

题意:求一棵树的直径(两点间的最长距离)

解题思路:求一颗树的最长直径,就是从树上任意一点出发找到一条最长的路,再找一条除了这条路以外的次长路,加起来就是树上的最长路(自己画画就明白了),所以用两种做法,DFS遍历一次全图找到最长路和次长路,也可以先BFS一次找到一条最长路,在从此路再找一条最长路(可不是原路返回),类似于有a,b,c,d四个节点它们的关系是

a<->b, a<->c<->d随便找一点假设以a为起点,bfs一次找到最长路 a->c->d, 那么在以d为起点再找一次最长路就是整棵树的直径d->c>a->b。采用链式向前星的结构可以大大加快存储和输出速度。链式向前星 + DFS or BFS 应该是最快的速度。


Description

Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum amongst all nodes.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with an integer n (2 ≤ n ≤ 30000) denoting the total number of nodes in the tree. The nodes are numbered from 0 to n-1. Each of the next n-1 lines will contain three integers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 10000) denoting that node u and v are connected by an edge whose weight is w. You can assume that the input will form a valid tree.

Output

For each case, print the case number and the maximum distance.

Sample Input

2

4

0 1 20

1 2 30

2 3 50

5

0 2 20

2 1 10

0 3 29

0 4 50

Sample Output

Case 1: 100

Case 2: 80

Source

Problem Setter: Jane Alam Jan



1.链式向前星 + dfs

Memory: 4260 KB Time: 136 MS
Language: C++ Result: Accepted
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>

using namespace std;

#define MAX_N 30005
const int INF = 0x3f3f3f3f;


//使用链式向前星存图
int top;
int head[MAX_N];
struct Edge
{
    int to;
    int w;
    int next;
} edge[MAX_N<<1];
int ans;
inline void init()
{
    top = 0;
    ans = 0;
    memset(head , -1, sizeof(head));
}

void add_edge(int u, int v, int w)
{
    edge[top].to = v;
    edge[top].w = w;
    edge[top].next = head[u];
    head[u] = top++;
}

int dfs(int pos , int pre)
{
    int first = 0 ,second = 0;

    for(int i = head[pos]; i != -1 ; i = edge[i].next)
    {
        int to = edge[i].to;
        int w = edge[i].w;
        if( to == pre) continue;
        int tmp = w + dfs(to, pos);
        if(tmp > first)
        {
            second = first;
            first = tmp;
        }
        else if(tmp > second)
        {
            second = tmp;
        }
    }

    if(first + second > ans)
    {
        ans = first + second;
    }
//    cout<<first<<endl;
    return first;
}


int main()
{
    int t;
    int cas = 1;
    scanf("%d", &t);
    while(t--)
    {
        init();
        int n;
        scanf("%d", &n);
        for(int i = 1 ; i < n  ; i++)
        {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            add_edge(u, v, w);
            add_edge(v, u, w);
        }
//        for(int i = 0 ; i < top ; i++)
//            cout<<edge[head[i]].to<<endl;
        dfs(0, -1);
        printf("Case %d: %d\n", cas++, ans);
    }
    return 0;
}

2.链式向前星 + bfs

Memory: 2656 KB Time: 152 MS
Language: C++ Result: Accepted
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>

using namespace std;

#define MAX_N 30005
const int INF = 0x3f3f3f3f;
int top;
bool vis[MAX_N];
int d[MAX_N];
int head[MAX_N];
queue<int> que;
struct Edge
{
    int to;
    int w;
    int next;
}edge[MAX_N << 1];

int add_edge(int u, int v, int w)
{
    edge[top].to = v;
    edge[top].w = w;
    edge[top].next = head[u];
    head[u] = top++;
}

inline void init()
{
    top = 0;
    memset(head, -1, sizeof(head));
}

int bfs(int s)
{
    memset(vis, false, sizeof(vis));
    memset(d, -1, sizeof(d));
    while(!que.empty()) que.pop();
    vis[s] = true;
    d[s] = 0;
    que.push(s);
    int ans = -1;
    int max_dis = -1;
    while(!que.empty())
    {
        int n = que.front();
        que.pop();
        for(int i = head[n] ; i != -1 ; i = edge[i].next)
        {
            int to = edge[i].to;
            int w = edge[i].w;

            if(vis[to]) continue;
            vis[to] = true;
            d[to] = d[n] + w;
            if(d[to] > max_dis)
            {
                max_dis = d[to];
                ans = to;
            }
            que.push(to);
        }
    }
    return ans;
}

int main()
{
    int t, cas = 1;
    scanf("%d", &t);
    while(t--)
    {
        init();
        int n;
        scanf("%d", &n);
        for(int i = 1 ; i < n ; i++)
        {
            int u, v, w;
            scanf("%d%d%d",&u, &v, &w);
            add_edge(u, v, w);
            add_edge(v, u ,w);
        }

        int tmp = bfs(0);
        int ans = bfs(tmp);

        printf("Case %d: %d\n", cas++, d[ans]);
    }

    return 0;
}

可以看出dfs的内存开销较bfs大不少,而时间只快了一点点


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值