HOJ 1030 && POJ 1383 - Labyrinth(树的直径、图的直径)

啊,今天本来想来一道水题练练DFS的,因为看了这题像DFS就打算写写,写完丢上去给我一个TLE,令我很费解。后来问了大佬,才知道这是(无根)树的直径问题,有多种求法,顺便就总结一下。
传送门:http://acm.hit.edu.cn/hojx/showproblem/1030/
1030 - Labyrinth

Time limit : 5 s Memory limit : 32 mb
Submitted : 991 Accepted : 427
Submit

Problem Description
The northern part of the Pyramid contains a very large and complicated
labyrinth. The labyrinth is divided into square blocks, each of them
either filled by rock, or free. There is also a little hook on the floor
in the center of every free block. The ACM have found that two of the
hooks must be connected by a rope that runs through the hooks in every
block on the path between the connected ones. When the rope is fastened, a
secret door opens. The problem is that we do not know which hooks to
connect. That means also that the neccessary length of the rope is
unknown. Your task is to determine the maximum length of the rope we could
need for a given labyrinth.

Input
The input consists of T test cases.
The number of them (T) is given on the first line of the input
file. Each test case begins with a line containing two integers
C and R (3 <= C,R <= 1000)
indicating the number of columns and rows. Then exactly R lines
follow, each containing C characters. These characters specify
the labyrinth. Each of them is either a hash mark (#) or a
period (.). Hash marks represent rocks, periods are free
blocks. It is possible to walk between neighbouring blocks only, where
neighbouring blocks are blocks sharing a common side. We cannot walk
diagonally and we cannot step out of the labyrinth.

The labyrinth is designed in such a way that there is exactly one path
between any two free blocks. Consequently, if we find the proper hooks to
connect, it is easy to find the right path connecting them.

Output
Your program must print exactly one line of
output for each test case. The line must contain the sentence
“Maximum rope length is X. where Xis
the length of the longest path between any two free blocks, measured in
blocks.

Sample Input

2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######

Sample Output

Maximum rope length is 0.
Maximum rope length is 8.

问题就是要求在一个迷宫中找距离最长的两个点。迷宫可以看作是一个无根树,因此,这个问题等价与在一个树形图中找最远的两个节点,也叫做这个图的直径。
迷宫、树形图有个很好的特点:任意两个节点之间的距离就是这两点之间的最短路径、也是两个节点的最长路径,也可以说任意两个节点之间的距离一定。基于这个想法,可以很快想到:穷举每个路径,计算其距离,取最大值。这就是我用DFS结果TLE的过程。。。

我查了一下,有这样一种方法,是利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点。这样就可以首先任取一个点,BFS求出离其最远的点,再用同样的方法BFS求出离这个叶子节点最远的点,此时两点间的距离就是树的直径。


**

转载别人的证明

**
证明:
树的直径(最长路) 的详细证明:http://www.cnblogs.com/wuyiqi/archive/2012/04/08/2437424.html
主要是利用了反证法:

假设 s-t这条路径为树的直径,或者称为树上的最长路

现有结论,从任意一点u出发搜到的最远的点一定是s、t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路

证明:

1 设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则

dis(u,T) >dis(u,s) 或 dis(u,T)>dis(u,t) 则最长路不是s-t了,与假设矛盾

2 设u不为s-t路径上的点

首先明确,假如u走到了s-t路径上的一点,那么接下来的路径肯定都在s-t上了,而且终点为s或t,在1中已经证明过了

所以现在又有两种情况了:

1:u走到了s-t路径上的某点,假设为X,最后肯定走到某个端点,假设是t ,则路径总长度为dis(u,X)+dis(X,t)

2:u走到最远点的路径u-T与s-t无交点,则dis(u-T) >dis(u,X)+dis(X,t);显然,如果这个式子成立,

则dis(u,T)+dis(s,X)+dis(u,X)>dis(s,X)+dis(X,t)=dis(s,t)最长路不是s-t矛盾

附上一张第二种情况的图

这里写图片描述


然后搞懂这些,就可以愉快的AC了!
AC代码如下:

#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 1005
using namespace std;
char la[maxn][maxn];
int vis[maxn][maxn];
int path = 0;
int c, r;
int sx, sy;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int MAX(int a, int b)
{
    return a > b ? a : b;
}
struct Node{
    int x, y;
    int step;
};
queue<Node> pa;
void BFS()
{
    while(!pa.empty())    pa.pop();
    memset(vis, 0, sizeof(vis));
    Node beg;
    beg.x = sx, beg.y = sy, beg.step = 0;
    vis[beg.x][beg.y] = 1;
    pa.push(beg);
    path = 0;
    while(!pa.empty())
    {
        Node cur = pa.front();
        pa.pop();

        for(int i = 0; i < 4; i++)
        {
            Node next;
            next.x = cur.x + dx[i];
            next.y = cur.y + dy[i];
            if(next.x < 0 || next.x >= r || next.y < 0 || next.y >= c)    continue;
            if(vis[next.x][next.y] || la[next.x][next.y] == '#')    continue;

            next.step = cur.step + 1;
            if(next.step > path)
            {
                path = next.step;
                sx = next.x;
                sy = next.y;
            }
            vis[next.x][next.y] = 1;
            pa.push(next);
        }   
    }
    return;
}
int main()
{
    int kase;
    scanf("%d", &kase);
    while(kase--)
    {
        scanf("%d %d", &c, &r);
        int st = 0;
        for(int i = 0; i < r; i++)
        {
            scanf("%s", la[i]);
            if(st)    continue;

            for(int j = 0; j < c; j++)
            {
                if(la[i][j] == '.')
                {
                    sx = i;
                    sy = j;
                    st = 1;
                    break;
                }
            }
        }
        BFS();
        BFS();
        printf("Maximum rope length is %d.\n", path);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值