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);
}
}