poj 1383 树的直径 两种求树直径的方法

Labyrinth
Time Limit: 2000MS   Memory Limit: 32768K
Total Submissions: 3627   Accepted: 1379

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.

Hint

Huge input, scanf is recommended.
If you use recursion, maybe stack overflow. and now C++/c 's stack size is larger than G++/gcc

Source

 
求含边数最多的路径的边数,写了两种算法:
1.自己YY了一个DP解法,只要一次遍历DFS即可(显然只要求建立有向树):
设maxd[u], secd[u]分别为点u离叶节点最长、次长距离(不经过相同儿子),(距离指的是边数,下同),maxl[u]为以u为树根的树上直径长度,则有
maxl[u] = max{maxl[son], maxd[u]+secd[u]}, 其中 all son ∈ u,即枚举u的所有儿子。
AC代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define cint const int
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

#define MAXT 1111

int C, R;
char g[MAXT][MAXT];

cint dx[]={-1, 1, 0, 0},
     dy[]={0, 0, -1, 1};

inline bool cango(int x, int y){
    return (x>-1 && x<R && y>-1 && y<C && g[x][y]=='.');
}

int maxd[MAXT][MAXT];                   //离叶子节点的最大距离

int dfs(int x, int y){
    g[x][y]='#';                        //访问过
    int maxl = maxd[x][y] = 0, secd = 0;
    for(int i=0; i<4; i++){                 //四个方向
        int nx = dx[i] + x, ny = dy[i] + y;
        if(!cango(nx, ny)) continue;      //不可走,或不能走

        int tmp = dfs(nx, ny);
        maxl = MAX(tmp, maxl);                      //儿子节点的最大值
        if(maxd[nx][ny]+1 > maxd[x][y]){           //最大值路由2个儿子和当前访问点组成
            secd = maxd[x][y];
            maxd[x][y] = maxd[nx][ny] + 1;
        }
        else if(maxd[nx][ny]+1 > secd)
            secd = maxd[nx][ny] + 1;
    }
    maxl = MAX(maxl, secd + maxd[x][y]);
    return maxl;
}

int main(){
    int T;
//    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//    freopen("C:\\Users\\Administrator\\Desktop\\out2.txt","w",stdout);
    scanf(" %d", &T);
    while(T--){
        int i, j;
        scanf(" %d %d", &C, &R);
        for(i=0; i<R; i++) scanf(" %s", g[i]);
        int ans=0;
        for(i=0; i<R; i++){
            for(j=0; j<C; j++) if(g[i][j]=='.')
                ans=dfs(i, j);                      //多棵树
            if(j<C) break;
        }
        printf("Maximum rope length is %d.\n", ans);
    }
    return 0;
}

 2.基于树中两条最长路定相交于某点的结论。。。

(假如不相交,但它们必定连通,若通过一条边连通,则加入这条边后两条路径长度之和增大1,因此必须有一条路径长度增加,得到更长路径,矛盾。)

所以,先任找一点a,搜其最远点b,然后从b出发搜其最远点c,则c与b的距离就是答案,注意,这里要求建立无向树。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define cint const int
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

#define MAXN 1111
char g[MAXN][MAXN];
int d[MAXN][MAXN], R, C;

cint dx[]={-1, 1, 0, 0},
     dy[]={0, 0, -1, 1};

struct node{
    int x, y;
};

bool cango(int x, int y){
   if(!(x>-1 && y>-1 && x<R && y<C)) return false;
   if(g[x][y]=='#' || d[x][y]>-1) return false;
   return true;
}

void bfs(int sx, int sy, int &ex, int &ey){
    queue<node> q;   q.push((node){sx, sy});
    memset(d, -1, sizeof(d));
    d[sx][sy] = 0;
    ex = sx;      ey = sy;
    while(!q.empty()){
        node u = q.front();     q.pop();
        for(int i=0; i<4; i++){
            int nx = dx[i] + u.x, ny = dy[i] + u.y;
            if(!cango(nx, ny)) continue;
            d[nx][ny] = d[u.x][u.y] + 1;
            q.push((node){nx, ny});
            if(d[nx][ny]>d[ex][ey]){
                ex = nx;
                ey = ny;
            }
        }
    }
}

int main(){
//    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//    freopen("C:\\Users\\Administrator\\Desktop\\out1.txt","w",stdout);
    int T;
    scanf(" %d", &T);
    while(T--){
        int i, j;
        scanf(" %d %d", &C, &R);
        for(i=0; i<R; i++) scanf(" %s", g+i);
        int sx, sy;
        for(i=0; i<R; i++){
            for(j=0; j<C; j++) if(g[i][j]=='.'){
                bfs(i, j, sx, sy);
                break;
            }
            if(j<C) break;
        }
        bfs(sx, sy, i, j);
        printf("%d\n", d[i][j]);
    }
    return 0;
}

 

posted @ 2013-10-08 09:23  Ramanujan  阅读(267)  评论(0编辑  收藏  举报