C++题解:树的重心

题目描述

给定一颗树,树中包含 n n n个结点(编号 1 1 1~ n n n)和 n − 1 n-1 n1条无向边。请你找到树的重心,并输出将重心删除后,剩余各个连通块中点数的最大值。

树的重心定义:重心是指树中的一个结点,如果将这个点删除后,剩余各个连通块中点数的最大值最小,那么这个节点被称为树的重心。

树的重心具有如下性质:

  1. 若求树中某个点到其他点的距离之和,则重心到其他点的距离和最小,如果有两个重心,则它们的距离和一样。
  2. 一棵树添加或者删除一个结点,树的重心最多只移动一条边的位置。
  3. 把两棵树通过某一点相连得到一棵新的树,新树的重心必然在连接原来两棵树重心的路径上。

现在输入一个有 n n n个结点的树,结点编号为 1 1 1 n n n。试求树的重心的所有的子树中最大的子树的结点数目。

算法思想

使用邻接表存储树,需要注意存储无向边时,每条边应存储两次,即 ( a , b ) (a,b) (a,b) ( b , a ) (b,a) (b,a)

以任意结点 u u u为根进行深度优先遍历,计算以 u u u为根的子树的结点的数量 s u m sum sum n − s u m n - sum nsum即求出树中去掉以 u u u为根的子树后,剩下连通块中结点的数量。

时间复杂度

树中的每条边都会访问一次,时间复杂度为 O ( m ) O(m) O(m)

代码实现(邻接表)

#include <iostream>
#include <vector>
using namespace std;
const int N = 100010;

int n, m, ans = N;
vector<int> g[N];
//计算以u为根的子树中所有结点的数量
int dfs(int u, int fa)
{
	//sum表示以u为根的树中,所有结点的数量
    //res 表示以u为根的所有子树的点的最大值
    int sum = 1, res = 0;
    for(auto v : g[u])
    {
        if(v != fa)
        {
            int t = dfs(v, u); // 求以该子结点v为根的子树中点的数量
            sum += t;
            res = max(res, t);
        }
    } 
    //n- sum 求剩余结点组成的连通块中点的数量 
    res = max(res, n - sum);    
    ans = min(ans, res);    
    return sum;
}

int main()
{
    cin >> n;
    for(int i = 0; i < n - 1; i ++)
    {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b), g[b].push_back(a);
    }
    dfs(1, 0);
    cout << ans << endl;
    return 0;
}

代码实现(链式前向星)

#include <iostream>
#include <cstring>

using namespace std;
//注意每条边存储两次
const int N = 100010, M = 2 * N;

int n, m, ans = N;
int h[N], e[M], ne[M], idx; //邻接表存储
int st[N];//st[i]表示i点是否已经访问过

void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
//计算以u为根的子树中所有结点的数量
int dfs(int u)
{
	//sum表示以u为根的树中,所有结点的数量
    //res 表示以u为根的所有子树的点的最大值
    int sum = 1, res = 0;
    
    st[u] = 1;
    
    for(int i = h[u]; ~i; i = ne[i])
    {
        int v = e[i];
        if(!st[v])
        {
            int t = dfs(v); // 求以该子结点v为根的子树中点的数量
            sum += t;
            res = max(res, t);
        }
    } 
    //n- sum 求剩余结点组成的连通块中点的数量 
    res = max(res, n - sum);    
    ans = min(ans, res);    
    return sum;
}

int main()
{
    cin >> n;
    
    memset(h, -1, sizeof h);    
    for(int i = 0; i < n - 1; i ++)
    {
        int a, b;
        cin >> a >> b;
        add(a, b), add(b, a);
    }
    
    dfs(1);
    
    cout << ans << endl;
    
    return 0;
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
你好!对于扫雷游戏的题解,我可以给你一些思路和代码示例。首先,你需要了解扫雷游戏的规则和要求。接下来,你可以使用C++语言来实现游戏逻辑和界面。 下面是一个简单的扫雷游戏的C++代码示例: ```cpp #include <iostream> #include <vector> #include <random> using namespace std; class MinesweeperGame { private: int rows; int cols; vector<vector<char>> board; vector<vector<bool>> revealed; vector<pair<int, int>> directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; public: MinesweeperGame(int m, int n, int mineCount) { rows = m; cols = n; board.resize(rows, vector<char>(cols, ' ')); revealed.resize(rows, vector<bool>(cols, false)); placeMines(mineCount); calculateNumbers(); } void printBoard() { cout << " "; for (int j = 0; j < cols; j++) { cout << j << " "; } cout << endl; for (int i = 0; i < rows; i++) { cout << i << " |"; for (int j = 0; j < cols; j++) { cout << board[i][j] << "|"; } cout << endl; } } bool isGameOver() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (board[i][j] == 'M' && revealed[i][j]) { return true; } } } return false; } void reveal(int row, int col) { if (row < 0 || row >= rows || col < 0 || col >= cols || revealed[row][col]) { return; } revealed[row][col] = true; if (board[row][col] == 'M') { return; } if (board[row][col] == '0') { for (auto dir : directions) { reveal(row + dir.first, col + dir.second); } } } private: void placeMines(int mineCount) { random_device rd; mt1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少儿编程乔老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值