codeforces 455C C. Civilization(树形dp+树的直径+并查集)

题目链接:

codeforces 455C


题目大意:

给出一些点,他们之间初始存在一些边,给出两种操作,第一种是查询某个点所在的树的直径,另一种是将两个树合并,要求使合并后的树的直径最小。


题目分析:

  • 首先算取没做操作前的连通块里的树的直径,也就是先dfs一遍,找到深度最大的点,然后从这个点再搜,找到的最远的距离就是这棵树的直径,因为可以证明从根搜深度最大的点一定是树的直径的一个端点,因为它可以通过到达次大的深度的点或者找到与它公共祖先不在根处的获得树的直径。
  • 然后每次合并,我们可以知道得到的新的树的直径是
    max{max{L1,L2},L1+12+L2+12+1}
  • 然后利用并查集记录连通块即可。

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
#define MAX 300007

using namespace std;

int n,m,q;
vector<int> e[MAX];
int fa[MAX];
int ans[MAX];

void clear()
{
    for ( int i = 0 ; i < MAX ; i++ )
        e[i].clear();
    for ( int i = 1 ; i < MAX ; i++ )
        fa[i] = i;
}

void add ( int u , int v )
{
    e[u].push_back ( v );
    e[v].push_back ( u );
}

int _find ( int x )
{
    return x == fa[x]?x:fa[x] = _find ( fa[x] );
}

void _union ( int x , int y , int f )
{
    x = _find ( x );
    y = _find ( y );
    if ( x > y ) swap ( x , y );
    fa[y] = x;
    if ( f ) return;
    int xx = ans[x];
    int yy = ans[y];
    ans[x] = max ( max ( xx , yy ) , (xx+1)/2 + (yy+1)/2 +1 );
}

bool used[MAX];
bool mark[MAX];
int pp,dis;

void dfs ( int u , int d , int p )
{
    if ( d > dis )
    {
        dis = d;
        pp = u;
    }
    for ( int i = 0 ; i < e[u].size() ; i++ )
    {
        int v = e[u][i];
        if ( v == p ) continue;
        dfs  ( v , d+1 , u );
    }
}


int main ( )
{
    while ( ~scanf ( "%d%d%d" , &n , &m , &q) )
    {
        clear();
        int x,y,z;
        for ( int i = 0 ; i < m ; i++ )
        {
            scanf ( "%d%d" , &x, &y);
            add ( x , y );
            _union ( x , y , 1 );
        }
        memset ( used , 0 , sizeof ( used ) );
        for ( int i = 1 ; i <= n ; i++ )
        {
            int x = _find ( i );
            if ( used[x] ) continue;
            pp = dis = -1;
            used[x] = true;
            dfs ( x , 1 , -1 );
            dis = 0;
            dfs ( pp , 0 , -1 );
            ans[x] = dis;
        }
        for ( int i = 0 ; i < q ; i++ )
        {
            scanf ( "%d" , &z );
            if ( z == 1 )
            {
                scanf ( "%d" , &x );
                printf ( "%d\n" , ans[_find(x)] );
            }
            else
            {
                scanf ( "%d%d" , &x , &y );
                if ( _find(x) == _find(y) ) continue;
                _union ( x , y , 0 );
            }
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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.csdn.net/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、付费专栏及课程。

余额充值