hdu 6228 Tree(DFS)

22 篇文章 0 订阅
16 篇文章 1 订阅
Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 539    Accepted Submission(s): 343


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
 
 
34 21 22 33 44 21 21 31 46 31 22 33 43 56 2
 
Sample Output
 
 
101
 

Source

 


题意:

让你给一个n个节点的树形图涂色,有k中颜色,Ei表示最小的边集,使得所有涂颜色i的点连通。让你选定一种涂色方案,使得res=E1∩E2∩。。。∩Ek最大


解析:

将问题局部化,考虑一条边连接两个点,假若A,B有边,且A边的子树种节点数为w+1(包括A),那么B边的子树的大小就是n-w-1(包括B),若两边子树的大小都大于k的话,那么这一定是一条所有颜色的公共边,即答案+1.这样用DFS就可以预处理出子树大小,然后在遍历边就可以得出答案。

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;

const int MAXN = 200000+10;

int n,k;
vector<int> qq[MAXN];
int sont[MAXN];
int visit[MAXN];

int edge[MAXN][2];

int  dfs(int x,int depth)
{
    visit[x]=depth;
    int num=0;

    for(int i=0;i<qq[x].size();i++)
    {
        int u=qq[x][i];
        if(visit[u]==0)
        {
            num+=dfs(u,depth+1);
        }
    }
    sont[x]=num;
    return num+1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
        {
            qq[i].clear();
        }
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            edge[i][0]=u;
            edge[i][1]=v;
            qq[u].push_back(v);
            qq[v].push_back(u);
        }
        memset(visit,0,sizeof(visit));
        dfs(1,1);
        int ans=0;
        for(int i=1;i<n;i++)
        {
            int u=edge[i][0];
            int v=edge[i][1];
            if(visit[u]>visit[v])
            {
                if(sont[u]+1>=k&&sont[u]+1<=n-k)
                {
                    ans++;
                }
            }
            else
            {
                if(sont[v]+1>=k&&sont[v]+1<=n-k)
                {
                    ans++;
                }
            }
        }
        printf("%d\n",ans);

    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值