2018 ICPC亚洲区域赛沈阳站 LTree(思维+dfs)

Posted kannyi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018 ICPC亚洲区域赛沈阳站 LTree(思维+dfs)相关的知识,希望对你有一定的参考价值。

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

题意:

有一棵N个点的无根树,以及K种颜色,先用这K种颜色染色。将连通相同颜色的点所需要的最少数量的边作为一个集合(其实也就类似于一种最小生成树),然后将所有颜色所形成的集合树做一个交集,现在要找到一种染色方案,使得这个交集最大。若不使用某种颜色,那么该颜色的边集为空集。

思路:

题意真的好难读懂呀!看了半天也没思路!我一开始往点的方向思考,实则不行,必须要往边的方向想才对。

在给某一种颜色染色时,最好是只染最外边的两个点,因为要省出一些点给其他颜色,有空集颜色出现肯定是不好的情况。对于每一条边,它的两点分别延伸出去两棵子树,所以只要看每一条边所对应的两棵子树大小,是否都大于等于K即可。若满足条件,则必在交集之中,最后深搜遍历就好了!

题目链接:HDOJ 6228

#include<bits/stdc++.h>
#define MAX 200005
using namespace std;
typedef long long ll;
int n,k,sum,num[MAX];
vector<int>ve[MAX];
void dfs(int x,int next)
{
    num[x]=1;
    int i,j;
    for(i=0;i<ve[x].size();i++)
    {
        int y=ve[x][i];
        if(y==next)continue;
        dfs(y,x);
        num[x]+=num[y];
        if(num[y]>=k&&n-num[y]>=k)
            sum++;
    }
}
int main()
{
    int i,j,T;
    ios::sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        memset(num,0,sizeof(num));
        cin>>n>>k;
        for(i=1;i<=n;i++)
            ve[i].clear();
        for(i=1;i<n;i++)
        {
            int x,y;
            cin>>x>>y;
            ve[x].push_back(y);
            ve[y].push_back(x);
        }
        sum=0;
        dfs(1,-1);
        cout<<sum<<endl;
    }
    return 0;
}

 

以上是关于2018 ICPC亚洲区域赛沈阳站 LTree(思维+dfs)的主要内容,如果未能解决你的问题,请参考以下文章

第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(沈阳),签到题4题

第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(沈阳),签到题5题

第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(沈阳),签到题5题

2017.11.11 ACM-ICPC2017亚洲区域赛(沈阳)重现赛 7/13 Rank10

2016ACM/ICPC亚洲区沈阳站现场赛题解报告

2016ACM/ICPC亚洲区沈阳站现场赛题解报告