2017 ACM/ICPC 沈阳 L题 Tree
2024-09-01 20:24:19
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.
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+;
int n,k,ans,num[maxn];
vector<int> vec[maxn]; void dfs(int u,int fa)
{
num[u]=;
for(int i=;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,,n+); ans=;
for(int i=;i<=n;i++) vec[i].clear();
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
}
dfs(,-);
//for(int i=1;i<=n;++i) cout<<num[i]<<endl;
printf("%d\n",ans);
}
return ;
}
最新文章
- Java开发web的几种开发模式
- [转]Eclipse SVN冲突解决
- viewPager 的可滑动 Title
- Linux:安装图形界面
- 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)
- centos 6.5 安装 buildbot-slave 0.8.9
- iOS开发——网络Swift篇&;JSON与XML数据解析
- 【最大流,二分图匹配】【hdu2063】【过山车】
- Android setOnTouchListener识别滑动手势
- Mac编程的官方文档(类似MSDN)
- for clean
- 32bit程序在64bit操作系统下处理重定向细节(转自http://bbs.pediy.com/showthread.php?t=89054)
- weblogic11g 配置数据源
- HDU 2665 Kth number(划分树)
- (二)部署solr7.1.0到tomcat
- C# 获取当前服务器运行程序的根目录,获取当前运行程序物理路径
- 这可能是史上最全的css布局教程
- process(进程)
- Agile敏捷开发Planning Poker简介
- Apache poi简介及代码操作Excel