Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 413 Accepted Submission(s): 281
Problem Description
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.
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.
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 ∩ E1 ... ∩ 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 0 1
Source
Recommend
jiangzijing2015
题意:一棵树有n个点,有k种颜色,对这n个点进行染色,对于每种颜色,把染成这种颜色的点连接起来的边拿出来形成一个集合,问这k个集合交集的最大值为多少
解题思路:因为交集要尽量大,所以必然k中颜色要染在尽量小的且大小大于等于k的子树中。先找到树的重心,然后k个k个染即可
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
int s[200009],nt[4000009],e[400009],cnt;
int n,m,u,v,tot;
int sum[200009], mx[200009], vis[200009];
int dfs(int k, int fa, int p)
{
int ans = 0;
sum[k] = 1; mx[k] = 0;
for (int i = s[k]; ~i; i = nt[i])
{
if (e[i] == fa || vis[e[i]]) continue;
int temp = dfs(e[i], k, p);
sum[k] += sum[e[i]];
if(sum[e[i]]>=m) tot++;
mx[k] = max(mx[k], sum[e[i]]);
if (mx[temp] < mx[ans]) ans = temp;
}
mx[k] = max(mx[k], p - sum[k]);
return mx[k] < mx[ans] ? k : ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(s,-1,sizeof s);
mx[cnt=0]=INF;
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
nt[cnt]=s[u],s[u]=cnt,e[cnt++]=v;
nt[cnt]=s[v],s[v]=cnt,e[cnt++]=u;
}
int y=dfs(1,1,n);
tot=0;
dfs(y,y,n);
printf("%d\n",tot);
}
return 0;
}