D. Two Paths---cf14D

题目链接:http://codeforces.com/problemset/problem/14/D

题意:有n个city ; n-1条路:求断开一条路之后分成的两部分所构成的树的直径的积最大是多少;

n的取值范围不大,所以可以采用暴力枚举的方法’;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <map>

using namespace std;

typedef long long LL;

#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
#define N 210

int G[N][N], n, vis[N], dist[N], Max, Index;

void bfs(int s)
{
    met(vis, 0);
    vis[s] = 1;
    dist[s] = 0;

    queue<int> Q;

    Q.push(s);

    while( Q.size() )
    {
        int p = Q.front();
        Q.pop();

        for(int i=1; i<=n; i++)
        {
            if( !G[p][i] )continue;

            if( !vis[i] )
            {
                vis[i] = 1;

                dist[i] = dist[p] + 1;

                Q.push(i);

                if(dist[i] > Max)
                {
                    Max = dist[i];

                    Index = i;
                }
            }
        }
    }
}

int main()
{
    int a[N], b[N];

    while(scanf("%d", &n)!=EOF)
    {
        met(G, 0);

        for(int i=1; i<n; i++)
        {
            scanf("%d %d", &a[i], &b[i]);

            G[a[i]][b[i]] = G[b[i]][a[i]] = 1;
        }

        int Ans = 0;

        for(int i=1; i<n; i++)
        {
            G[a[i]][b[i]] = G[b[i]][a[i]] = 0;

            met(dist, 0);
            Max = Index = 0;
            bfs(a[i]);
            bfs(Index);

            int ans = Max;

            met(dist, 0);
            Max = Index = 0;
            bfs(b[i]);
            bfs(Index);

            Ans = max(Ans, ans * Max);

            G[a[i]][b[i]] = G[b[i]][a[i]] = 1;
        }
        printf("%d\n", Ans);
    }
    return 0;
}

时间: 2024-08-29 15:14:43

D. Two Paths---cf14D的相关文章

cf14D Two Paths(树的直径)

题意: N个点构成一棵树.树枝的长度都是1. 在当中找两条不相交[没有公共点]的路,使得二者长度之积最大. (2 ≤ n ≤ 200) 思路: 一开始思路好麻烦,好麻烦,好麻烦,,,,,,,而且WA,,,,, 正解: 必定存在第三条路径连接两条最长路径.[因为是一棵树]. 去掉第三条路径上的某根树枝就可以将原树分成两个区域了,两条最长路径分别在一个区域里. 然后分别求两个区域的直径,相乘. N不大,枚举. 代码: int const N=210; int n; vector<int> G[N]

leetcode笔记:Unique Paths

一. 题目描述 A robot is located at the top-left corner of a m n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish

LeetCode --- 62. Unique Paths

题目链接:Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (ma

62.Unique Paths (法1递归-动态规划法2数学公式)

A robot is located at the top-left corner of a m x n grid(marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. Therobot is trying to reach the bottom-right corner of the grid (marked 'Finish'in the

[LeetCode]Unique Paths

题目: 从左上角到右下角的所有可能路径. 思路1: 回溯法去递归遍历所有的路径,但是复杂度太大,无法通过.checkPath方法实现 动态规划法,从左上角到每一格的路径数与它的上面一格和左边一格的路径和: N(m,n)=N(m-1,n)+N(m,n-1): 注意:第一行和第一列的特殊情况. package com.example.medium; /** * A robot is located at the top-left corner of a m x n grid (marked 'Sta

tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accepted: 5330 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the re

LintCode : Unique Paths II

Problem Description: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. Code: public class Solutio

LintCode : Unique Paths

Problem Description: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid

62. 63. Unique Paths 64. Minimum Path Sum

1. A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' i

[LeetCode257]Binary Tree Paths

题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 求一个二叉树的所有路径 代码: /** * Definition for a binary tree node. * struct