hdu 6228 Tree
来源:互联网 发布:金马甲网络交易平台 编辑:程序博客网 时间:2024/06/06 02:34
TreeTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 539 Accepted Submission(s): 343
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.
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.
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
2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
题意:
让你给一个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
- Tree(HDU 6228)
- hdu 6228 Tree
- HDU 3333 Turing Tree
- hdu 3333 Turing Tree
- hdu 4347 【KD-TREE】
- hdu 2682 Tree
- hdu 3516 Tree Construction
- Hdu Binary Tree Traversals
- HDU 4601 Letter Tree
- hdu 4601 Letter Tree
- hdu 4680(splay tree)
- HDU 1504 Disk Tree
- hdu 3534 Tree
- hdu 3333 Turing Tree
- HDU 1561(Tree Dp)
- hdu Binary Tree Traversals
- HDU Minimal Ratio Tree
- HDU 3333 Turing Tree
- 关于使用ant-design设计可动态添加FormItem的过程中遇到的问题记录。
- spark-shell启动报错:Yarn application has already ended! It might have been killed or unable to launch ap
- 利用栈实现算术表达式的求值
- Caffe中HDF5Data例子
- Springmvc静态资源配置
- hdu 6228 Tree
- 机房收费系统之结账
- 已知满二叉树的先序遍历,求其后序遍历
- matlab——深度学习NN
- centos6.5安装solr集群版
- Longest Ordered Subsequence POJ
- 5分钟学会 CSS Grid 布局
- java代码连接oracle的DBUtil代码
- hadoop 集群系列 一:Linux 安装JDK