hdu6223—tree(想法很好!)

Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 998    Accepted Submission(s): 596

 

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.

 

 

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 ∩ 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

2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

 

 

Recommend

jiangzijing2015   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

 

这道题的解题思路简直太牛掰了。

题意:

题意就是很迷,看了很久才明白。就是有n个点的无环图,然后有k种颜色,用这k种颜色涂色。然后相同颜色的点之间会连边(就是两点之间在这个图中的路),相同颜色连边以后的所有的边是一个子集Ei。然后k个子集。求这些子集交集的最大值。

思路:

解法很厉害啊!就是预处理以i为节点的子树的个数x,用n-x就是整个图除了他和他的子树剩下的。如果这两个值都>=k,那么ans+1,每个点枚举一遍。

让我想到了树的重心。

显然同种颜色要分布的越远越好;

虽然我们需要考虑的是边,但是我们可以转化为对每个节点去考虑;  令在这个节点的两侧每种颜色均有分布,那么一定会有一条公共边;

只算一条这样避免重复;

枚举每个节点i;

如果这个节点  左边的节点数和右边的节点数  都大于  k,那么这个点左右两边必有一条边在交集里面的;

代码:

#include<bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 200005;
const ll mod = 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

int n,k,ans;
int num[maxn];
vector<int>vec[maxn];

void dfs(int u,int pre)
{
    num[u]=1;
    for(int i=0;i<vec[u].size();i++)
    {
        int v=vec[u][i];
        if(v==pre) continue;
        dfs(v,u);
        num[u]+=num[v];
        if(num[v]>=k&&n-num[v]>=k)
            ans++;
    }
}

int main()
{
    int u,v;
    rush()
    {
        mst(num,0);
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
        {
            vec[i].clear();
        }
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&u,&v);
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
        ans=0;
        dfs(1,-1);
        printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值