【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+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;
}

原文地址:https://www.cnblogs.com/kannyi/p/9768593.html

时间: 2024-07-30 07:20:14

【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)的相关文章

2014ACM/ICPC亚洲区域赛牡丹江站总结

我在集训队里面也就一般水平,这次学校史无前例的拿到了8个名额,由于大三的只有两个队伍,所以我们13及能分到名额,由于13及人数很多,组长就按照谁在oj上面a的题多就让谁去,我和tyh,sxk,doubleq幸运的在大二就有机会参加亚洲现场赛,非常激动.牡丹江赛区是我和sxk和doubleq组成rainbow战队,我们对这次区域赛其实就是去张张见识,增加大赛经验(外加公费旅游2333),可是当我们真正来到赛场的时候不知道为上面我非常渴望拿一块牌子.第一天热身赛,double迅速切下水题,我一直再弄

2014ACM/ICPC亚洲区域赛牡丹江站汇总

球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doubleq运的在大二就有机会參加亚洲现场赛,非常激动.牡丹江赛区是我和sxk和doubleq组成rainbow战队,我们对这次区域赛事实上就是去张张见识,添加大赛经验(外加公费旅游2333),但是当我们真正来到赛场的时候不知道为上面我非常渴望拿一块牌子. 第一天热身赛,double迅速切下水题.我一直再

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score

Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar University. He is clever and diligent. However, he is not good at math, especially in Mathematical Analysis. After a mid-term exam, Bob was anxious about his

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy

Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Information Theory is one of the most popular courses in Marjar University. In this course, there is an important chapter about information entropy. Entropy is t

2014ACM/ICPC亚洲区域赛牡丹江站D和K题

Known Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expre

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation

Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expres

2015 ACM / ICPC 亚洲区域赛总结(长春站&amp;&amp;北京站)

队名:Unlimited Code Works(无尽编码)  队员:Wu.Wang.Zhou 先说一下队伍阵容:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之前只学了大部分图论内容,以及一些数据结构.动态规划等等,但是还不能熟练运用... ... 先从长春站说起吧... ... 长春站是我加入ACM以来参加的第一场ICPC,因此无比的激动!从杭州出发,乘了整整24小时的火车,终于到达了长春:第一次到大东北,沿途的风景与南方有很大差异,体会到了东北的寒

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

开场各自读题 I题签到..很快就过了.. 然后开了F..He写了个暴力..Hong看了看找了个递推式..喂给Huang拿JAVA写了一下..很快就过了.. 然后发现K有很多人过..然后He就去开了K.. Hong发现C是个几何..就让Huang去搞了..自己在搞A.. He推了很久K..没有进展..就和Huang讨论了.. Hong推了一会..上了A..没过全样例..下机看代码.. Huang想完C..发现交大板子有原题..就上机抄板去了..抄完就过了.. 然后Hong上机搞A..换了longl

2014ACM/ICPC亚洲区域赛牡丹江现场赛总结

不知道如何说起-- 感觉还没那个比赛的感觉呢?现在就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,因为要来比赛,所以那些事情队友的国奖不能答辩,自己的励志奖班里乱搞要投票,自己又不在,真是无语了--烦得要死,然后在这些事情还没处理好之前我们就这样10号中午从地大去北京站上火车了--那时真感觉这场带着这种心情来现场赛感觉要打铁了-- 然后10号晚上队友的国奖让琦神帮答辩完了,得国奖无疑了,然后自己的励志奖也定下来一定得了,在火车上的我们也松了一口气,不能因为来比赛国奖励志奖都不得是不--