2017 ACM/ICPC 沈阳 L题 Tree

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.

InputThe 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. 
OutputFor 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题解:考虑某条边,则只要两边的2个顶点都大于等于k,则连边时一定会经过这条边,ans++;

参看代码:
#include<bits/stdc++.h>
using namespace std;
#define clr(a,b,n) memset((a),(b),sizeof(int)*n)
typedef long long ll;
const int maxn = 2e5+10;
int n,k,ans,num[maxn];
vector<int> vec[maxn];

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

int main()
{
    int T,u,v;
    scanf("%d",&T);
	while(T--)
    {
        scanf("%d%d",&n,&k);
        clr(num,0,n+1); ans=0;
        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);
        }
        dfs(1,-1);
        //for(int i=1;i<=n;++i) cout<<num[i]<<endl;
        printf("%d\n",ans);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/songorz/p/9784973.html

时间: 2024-10-08 22:01:52

2017 ACM/ICPC 沈阳 L题 Tree的相关文章

2017 ACM/ICPC 沈阳 K题 Rabbits

Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number line, each occupying a different integer. In a single move, one of the outer rabbits jumps into a space between any other two. At no point may two rabbits occupy the same p

2017 ACM/ICPC 沈阳 F题 Heron and his triangle

A triangle is a Heron's triangle if it satisfies that the side lengths of it are consecutive integers t−1, t, t+ 1 and thatits area is an integer. Now, for given n you need to find a Heron's triangle associated with the smallest t bigger than or equa

2017 acm icpc 沈阳(网络赛)5/12 题解

比赛中较...能做的5道题 hdoj6195. cable cable cable 题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=6195 题目大意 : 略 规律 : 答案 = k+(m-k)*k hdoj6198. number number number 题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=6198 题目大意  : 给你一个整数n.问你n个斐波那契数(可重复)不能构成哪些数,输出

hdu6206 Apple 2017 ACM/ICPC Asia Regional Qingdao Online

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6206 题目: Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 530    Accepted Submission(s): 172 Problem Description Apple is Taotao's favouri

2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 1496    Accepted Submission(s): 723 Problem Description Kelukin is a businessman. Every day, he travels arou

2017 acm / icpc shenyang 双十一单身狗温馨重现赛

上午做实验老师看错时间来晚了,20与非门一侧坏掉..于是做完实验就,光荣迟到了!!!QAQ... 一开始..看B题...喵喵喵??? J题...窝只会暴力..算了算了.. 刷新~ I题AC ratio 好像还可以!戳进去一看,a+b+c+d...一克斯Q斯咪?哭叽叽写了个大数加法,piapiapia乱敲一气竟然没崩,AC~~~是个好开头吖有木有~o(* ̄▽ ̄*)ブ~ K题就是小兔子跳啊跳~piapiapia求和然后选择一下从哪一端起跳,紫欣sama1A~ L题一开始对题意有误解,看懂样例之后就发

2017 ACM/ICPC 新疆赛区 I 题 A Possible Tree 带权并查集

传送门 题意:给定一棵带权树的形态, 但是并不知道每天条边的具体权重. 然后给m个信息, 信息格式为u v val, 表示在树上u 到 v 的路径上经过的边的权重的异或和为val, 问前面最多有多少个信息是不冲突的. 思路:首先很明显的我们要维护一系列不知道的信息, 看冲不冲突的那就是带权并查集没跑了, 此时r[v] 表示v到这棵树的根节点(虽然题目没给, 但是我们可以假设一个)的路径异或和, 那么此时的每条信息相当于是告诉你r[u] ^ r[v]的值, 注意异或的特性. 所以对于每条信息维护好

2017 ACM/ICPC Asia Regional Shenyang Online 部分刷题记录

cable cable cable 题意: M个灯,K个盒子,求最少要连多少条线,使任选K个盒子每个灯都能装下 思路: 每个灯要连(M-K+1)个 总共M*(M-K+1) happy happy happy 题意: 左右取数,孩子每次都去左右两边最大的那个,父亲想让孩子赢(大于父亲)且最小化分差 思路: 限时搜索,先dp预处理出l,r区间最大分差和最小分差,然后\(2^n\)搜索,但每进入一层都要利用dp数组更新一次答案 #include<bits/stdc++.h> #define pii

2017 ACM/ICPC Asia Regional Shenyang Online 记录

这场比赛全程心态爆炸…… 开场脑子秀逗签到题WA了一发.之后0贡献. 前期状态全无 H题想复杂了,写了好久样例过不去. 然后这题还是队友过的…… 后期心态炸裂,A题后缀数组理解不深,无法特判k = 1时的情况. 然后也没有心思读题了,心静不下来. 比赛题目链接 Problem B $ans = k(n - k + 1)$ #include <bits/stdc++.h> using namespace std; typedef long long LL; LL n, k; int main()