Codeforces 690C2 Brain Network (medium) (树的直径)

通过研究僵尸脑网络结构,本文提出了一种算法来计算脑网络的最大延迟,即树的直径。此算法首先找到离某一随机起点最远的节点,然后从该节点出发再次搜索,返回的最远距离即为脑网络的最大延迟。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Brain Network (medium)

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Further research on zombie thought processes yielded interesting results. As we know from the previous problem, the nervous system of a zombie consists of n brains and m brain connectors joining some pairs of brains together. It was observed that the intellectual abilities of a zombie depend mainly on the topology of its nervous system. More precisely, we define the distance between two brains u and v (1 ≤ u, v ≤ n) as the minimum number of brain connectors used when transmitting a thought between these two brains. The brain latency of a zombie is defined to be the maximum distance between any two of its brains. Researchers conjecture that the brain latency is the crucial parameter which determines how smart a given zombie is. Help them test this conjecture by writing a program to compute brain latencies of nervous systems.

In this problem you may assume that any nervous system given in the input is valid, i.e., it satisfies conditions (1) and (2) from the easy version.
Input

The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 100000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains a b it connects (1 ≤ a, b ≤ n and a ≠ b).
Output

Print one number – the brain latency.
Examples
Input

4 3
1 2
1 3
1 4

Output

2

Input

5 4
1 2
2 3
3 4
3 5

Output

3

注: 题目一中的满足条件:1 图中不存在环 2 所有点都在图中;
思路: 题目得知该图为一棵树,所求为为树最长路径即树的直径;首先取任意点bfs求出具该点最远的点即树的直径的一段再对这个点进行bfs()所求的最远距离即为树的直径。
下面ac代码

#include<iostream>
#include<queue>
#include<cstdio>
#include<stack>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>

using namespace std;

struct node
{
    int n;
    int far;
}tmp;
int m,n;
int stu[100005];
bool vis[100005];
queue<node>Q;
vector<int>num[100005];

void bfs(int n)
{
    tmp.n = n;
    tmp.far = 0;
    Q.push(tmp);
    vis[n] = true;
    stu[n] = 0;
    while(!Q.empty())
    {
        node now = Q.front();
        Q.pop();
        vector<int>::iterator it;
        for(it = num[now.n].begin() ; it != num[now.n].end() ; ++it)
        {
            if(!vis[*it])
            {
                stu[*it] = now.far + 1;
                tmp.n = *it;
                tmp.far = stu[*it];
                Q.push(tmp);
                vis[*it] = true;
            }
        }
    }
}

int main()
{
    while(cin>>n>>m)
    {
        memset(vis , 0 ,sizeof(vis));
        memset(stu , 0 ,sizeof(stu));
        for(int i = 1 ; i <= n ; ++i)
        {
            num[i].clear();
        }
        for(int i = 1 ; i <= m ; ++i)
        {
            int st , en;
            cin>>st>>en;
            num[st].push_back(en);
            num[en].push_back(st);
        }
        bfs(1);
        int lenmax = 0;
        int weizhi = 1;
        for(int i = 1 ; i <= n ; ++i)
        {
            if(stu[i] > lenmax)
            {
                lenmax = stu[i];
                weizhi = i;
            }
        }
        memset(vis, 0 ,sizeof(vis));
        bfs(weizhi);
        lenmax = 0;
        weizhi = 1;
        for(int i = 1 ; i <= n ; ++i)
        {
            if(stu[i] > lenmax)
            {
                lenmax = stu[i];
                weizhi = i;
            }
        }
        cout<<lenmax<<endl;
    }
    return 0;
}
引用\[1\]中提到了一种形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子。为了使cnt_white - cnt_black尽可能大,可以使用两次形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是链所代表的子的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值