【赛后补题】(HDU6228) Tree {2017-ACM/ICPC Shenyang Onsite}
这条题目当时卡了我们半天,于是成功打铁……今天回来一看,mmp,贪心思想怎么这么弱智。。。。。(怪不得场上那么多人A了
题意分析
这里是原题:
Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
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 2Sample Output
1
0
1
题意很简单,我们考虑一下做法。
要想交集尽可能大,颜色各自的分布应该尽可能“往顶层和底层分布”——这是贪心思想。然后具体怎么实现?很简单,对每一个点i,设它的子树的节点(包括它自身)有p个,那么只需要检查p≥k和n−p≥k即可。不需要查边,只需要查点,因为只要存在这样的点,那么一定存在这样的一条公共边。
实现查子树可以用dfs遍历一遍即可实现。
代码
#include <bits/stdc++.h>
using namespace std;
#define NQUICKIO
#define NFILE
struct Edge
{
int u,v;
Edge(int _u,int _v):u(_u),v(_v) {}
};
const int maxnode=200005;
vector<Edge> edges;
vector<int> G[maxnode];
int s[maxnode];
void addEdge(int u,int v)
{
edges.push_back(Edge(u,v));
G[u].push_back((int)edges.size()-1);
return;
}
int dfs(int f,int p)
{
//cout<<"now point:"<<p<<endl;
int nowsum=1;
for(int i=0;i!=(int)G[p].size();++i)
if(edges[G[p][i]].v!=f)
nowsum+=dfs(p,edges[G[p][i]].v);
return s[p]=nowsum;
}
int main()
{
#ifdef QUICKIO
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#endif
#ifdef FILE
freopen("datain.txt","r",stdin);
freopen("dataout.txt","w",stdout);
#endif
int T; cin>>T;
while(T--)
{
edges.clear();
int n,k; cin>>n>>k;
for(int i=1;i<=n;++i) G[i].clear();
memset(s,0,sizeof(s));
for(int i=1;i!=n;++i)
{
int tu,tv; cin>>tu>>tv;
addEdge(tu,tv);
addEdge(tv,tu);
}
dfs(-1,edges[0].u);
int ans=0;
/*for(int i=1;i<=n;++i)
cout<<s[i]<<" ";
cout<<endl;*/
for(int i=1;i<=n;++i)
{
if(s[i]>=k && n-s[i]>=k) ans++;
}
cout<<ans<<endl;
}
return 0;
}
【赛后补题】(HDU6228) Tree {2017-ACM/ICPC Shenyang Onsite}的更多相关文章
- 2017 ACM/ICPC Shenyang Online SPFA+无向图最长路
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- 2017 ACM/ICPC Asia Regional Qingdao Online
Apple Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submi ...
- 2018 HDU多校第三场赛后补题
2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...
- 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- 2018 HDU多校第四场赛后补题
2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...
- 2017 ACM ICPC Asia Regional - Daejeon
2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...
- 2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest
2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest A - Arranging Wine 题目描述:有\(R\)个红箱和\(W\)个白箱,将这 ...
- 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 ...
- 2017 ACM/ICPC 新疆赛区 I 题 A Possible Tree 带权并查集
传送门 题意:给定一棵带权树的形态, 但是并不知道每天条边的具体权重. 然后给m个信息, 信息格式为u v val, 表示在树上u 到 v 的路径上经过的边的权重的异或和为val, 问前面最多有多少个 ...
随机推荐
- Linux 和类 Unix 系统上5个最佳开源备份工具
转载:http://linux.cn/article-4623-weixin.html#rd?sukey=cbbc36a2500a2e6cb7678c4d38b691a9fa7403b259f898e ...
- php如何实现登陆后返回原页面
访问网站页面时,有的页面需要授权才能访问,这时候就会要求用户登录,跳转到登录页面login.php,怎么实现登录后返回到刚才访问的页面项目需求 访问网站页面时,有的页面需要授权才能访问,这时候就会要求 ...
- C#中的代码书写规范以及命名规范
C#代码书写规则: 1. 尽量使用接口,然后使用类实现接口,以提高程序的灵活性. 2.一行不要超过80个字符 3.尽量不要手动更改计算机生成的代码 4.关键的语句写注释 5.建议局部变量在最接近使用它 ...
- 路由器基本配置实验,静态路由和动态RIP路由
实验涉及命令以及知识补充 连线 PC和交换机FastEtherNet接口 交换机和路由器FastEtherNet接口 路由器和路由器Serial接口 serial是串行口,一般用于连接设备,不能连接电 ...
- CentOS 7 下 Oracle 11g 安装教程
一.准备工作 1.关闭selinux 查看selinux状态: getenforce或者sestatus -v 临时关闭: setenforce 0 永久关闭: vim /et ...
- Nacicat for Oracle 绿色版 亲测可用
参考: http://blog.csdn.net/u013107634/article/details/52741591 https://blog.csdn.net/zhengyikuangge/ar ...
- xcode7--iOS开发---将app打包发布至app store
时隔3个月再次接触应用打包,又是一顿折腾 说说这次的感受吧: 变得是打包时间减少到4小时(其中大部分时间还是xcode7或者是iOS9的原因),不变的是还是一如既往的坑!! 好了,废话不多说,下面讲讲 ...
- Lucene 工作原理
Lucene 简介 Lucene 是一个基于 Java 的全文信息检索工具包,它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能.Lucene 目前是 Apache Jakarta 家 ...
- 【模板】素数测试(Miller-Rabin测试)
基础素数测试模板 对于大数的素性判断,目前Miller-Rabin算法应用最广泛.一般底数仍然是随机选取,但当待测数不太大时,选择测试底数就有一些技巧了.比如,如果 被测数小于4759123141,那 ...
- centos7编译安装Apache
一.安装 安装之前先将服务器的防火墙关掉. systemctl stop firewalld systemctl disable firewall 第一步: 安装apr 下载: wget -c ...