ecust暑假网络训练赛一 — Tree (dfs+思维)

给定一棵无根树,用k种颜色进行染色,目标是找到一种染色方案,使得每种颜色节点连接的最小边集交集最大化。题目要求输出这个最大交集的边数。可以通过深度优先搜索算法求解,计算每个节点的大小(size),最终找到满足条件的最大边数。

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

Consider a un-rooted tree T which is not the biological significance of tree or plant, but a tree as an
undirected graph in graph theory with n nodes, labelled from 1 to n. If you cannot understand the
concept of a tree here, please omit this problem.
Now we decide to colour its nodes with k distinct colours, labelled from 1 to k. Then for each colour
i = 1, 2, . . . , k, define Ei as the minimum subset of edges connecting all nodes coloured by i. If there is
no node of the tree coloured by a specified colour i, Ei will be empty.
Try to decide a colour scheme to maximize the size of E1 ∩ E2 ∩ · · · ∩ Ek, and output its size.
Input
The first line of input contains an integer T (1 ≤ T ≤ 1000), indicating the total number of test cases.
For each case, the first line contains two positive integers n which is the size of the tree and k
(k ≤ 500) which is the number of colours. Each of the following n − 1 lines contains two integers x and
y describing an edge between them. We are sure that the given graph is a tree.
The summation of n in input is smaller than or equal to 200000.
Output
For each test case, output the maximum size of E1 ∩ E2 ∩ · · · ∩ Ek.
Sample Input
3
4 2
1 2
2 3
3 4
4 2
1 2
1 3
1 4
6 3
1 2
2 3
3 4
3 5
6 2

Sample Output
1

取名坑了同学,就帮他写个题解把
这题就是读题有点难,读完题你就会发现这是个傻逼题
题意转换过来大致是,给你一棵树,让你把这些树上的节点用M种颜色染,然后问你在最优的染色方案下,相同颜色点连接的最小边集的交集最大是多少 ,再通俗点就是连线经过次数超过M次的边有几条。
那我们就从边出发去找,只要经历一边dfs就可以找出左右节点的size(当然节点初始化的size是1)。
看一下样例解释就懂了:
在这里插入图片描述
建图可以邻接表,也可以vector,怎么喜欢怎么来把,跑了跑了。
再也不敢到别的地方乱水了
在这里插入图片描述

留下代码就跑路了把。

#include<bits/stdc++.h>
#define  mes(a, b)  memset(a, b, sizeof a)
#define  fi    first
#define  se    second
#define  pii   pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 2e5 + 10;
const int    maxm = 1e5 + 10;
const ll    mod  = 998244353;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;

int size[maxn];
vector<int>vec[maxn];

void dfs(int x,int pre){
    size[x] = 1;
    for(int i = 0;i < vec[x].size();i++){
        int y = vec[x][i];
        if(y == pre) continue;
        dfs(y,x);
        size[x] += size[y];
    }
}

int main(){
    int T;
    cin >> T;
    while(T--){
        int n,m;
        cin >> n >> m;
        for(int i = 0;i <= n;i++) vec[i].clear();
        for(int i = 1;i < n;i++){
            int u,v;
            cin >> u >> v;
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
        int ans = 0;
        dfs(1,0);
        for(int i = 1;i <= n;i++){
            if(size[i] >= m && size[i] <= n - m) ans++;
        }
        printf("%d\n",ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值