【赛后补题】(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, 问前面最多有多少个 ...
随机推荐
- react中修改antd的默认样式
最近在做react+antd项目.不可避免的遇到了修改antd默认样式的问题. 比如,table组件的表头背景色设置,如果直接使用元素样式,会修改整个项目的table.这里我用的方法是,给table添 ...
- POJ 1830 开关问题 【01矩阵 高斯消元】
任意门:http://poj.org/problem?id=1830 开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...
- POJ 2976 Dropping tests 【01分数规划+二分】
题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total S ...
- ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(六)之 好友申请、同意、拒绝
不知道距离上一篇多久没有写了,可能是因为忙(lan)的关系吧.废话不多说,今天要介绍的不算什么新知识,主要是逻辑上的一些东西.什么逻辑呢,加好友,发送好友申请,对方审批通过,拒绝.(很遗憾,对方审批通 ...
- 【洛谷P1118】数字三角形
数字三角形 题目链接 4 16 3 1 2 4 3 1 2 4 (3+1) (1+2) (2+4)(3+1+1+2) (1+2+2+4) (3+1+1+1+2+2+2+4)16=1*3+3*1+3*2 ...
- 【luogu P3901 数列找不同】 题解
对于区间查询的问题,提供一种思路: 莫队. 莫队是处理区间问题的乱搞神器,尤其是对于离线查询问题,当然也可以做在线查询,比如带修莫队. 对于有的题,莫队是乱搞骗分,而在某些地方,莫队是正解. 这道题来 ...
- 【luogu P2385 青铜莲花池】 题解
题目链接:https://www.luogu.org/problemnew/show/P2385 莲花池什么的最漂亮啦! 最近刷了两天搜索= =我搜索一直是弱菜 直接套bfs #include < ...
- struts2 实现文件下载方法汇总
http://pengranxiang.iteye.com/blog/259401 一.通过struts2提供的下载机制下载文件: 项目名为 struts2hello ,所使用的开发环境是MyEcli ...
- 在文件中的AngularJS模块
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 课时11.HTML基本机构详解(掌握)
通过观察我们发现,HTML基本结构中所有标签都是成对出现的.这些成对出现的标签中有一个带/有一个不带/,那么这些不带/的标签我们称之为开始标签,这些带/的标签,我们称之为结束标签. html标签 作用 ...