【赛后补题】(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, 问前面最多有多少个 ...
随机推荐
- sublime3插件安装方法
sublime3插件安装方法:http://blog.csdn.net/u011627980/article/details/52171886
- JSON、JSONP、XML的区别
这两天看jquery手册的时候看到了jsonp.发现手册把jsonp与json放在一起讲解了,所以想写篇文章 梳理一下这三者的关系. jsonp: jsonp是json的一种“使用模式”,可以让网页从 ...
- Python程序的执行原理(转)
1. 过程概述 Python先把代码(.py文件)编译成字节码,交给字节码虚拟机,然后虚拟机一条一条执行字节码指令,从而完成程序的执行. 2. 字节码 字节码在Python虚拟机程序里对应的是PyCo ...
- o'Reill的SVG精髓(第二版)学习笔记——第十一章
第十一章:滤镜 11.1滤镜的工作原理 当SVG阅读器程序处理一个图形对象时,它会将对象呈现在位图输出设备上:在某一时刻,阅读器程序会把对象的描述信息转换为一组对应的像素,然后呈现在输出设备上.例如我 ...
- js标准对象——Date
在JavaScript中,Date对象用来表示日期和时间. 要获取系统当前的时间: var now = new Date(); alert(now); now;//Mon Oct 23 2017 11 ...
- centos 安装配置 rabbitmq 以及nginx转发
安装erlang cd /tmp wget http://erlang.org/download/otp_src_18.3.tar.gz . cd /opt/otp_src_18. yum -y in ...
- JetBrains 授权服务器(License Server):
JetBrains 授权服务器(License Server): https://www.imsxm.com/jetbrains-license-server.html
- Mybaties保存后自动获取主键ID
<!-- 插入记录 --> <insert id="saveTvTypeBatch" useGeneratedKeys="true" keyP ...
- BZOJ 1193--马步距离
1193: [HNOI2006]马步距离 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2267 Solved: 1026[Submit][Stat ...
- 替换html里面的\r\n及解决记事本中的每个段落只有一行的情形
1. 在用python爬取小说的时候, 发现在内容里每次换行都有\r\n(即回车, 换行)出现. 此时可以采用 s.replace('\\r\\n','') , 其中s为字符串类型. 2. 在爬取完 ...