【HDOJ6228】Tree(树)
2024-09-05 22:12:05
题意:有一棵n个点的树,在树上的点涂色,每个点涂一种颜色,一共可以涂k种颜色,
然后把同一种颜色(比如说x)的点用最优方案连起来,在连线的边涂上x颜色,问涂k次的边最多有几条
k<=500
sigma n<=200000
思路:类似边分治的思路
考虑每一条边,如果以它为中心将树分割出的两部分点数都不小于k的话这条边必定对答案有贡献
这场都是队友写的,划水真爽
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
#include <limits.h>
#include <string>
#include <iostream>
#include <queue>
#include <math.h>
#include <stack>
#include <map>
#define left (now<<1)
#define right ((now<<1)+1)
#define mid ((l+r)>>1)
using namespace std;
typedef long long int lint; const int MAXN = 2e5 + ;
const int MOD = 1e9 + ;
const int EPS = 1e-; int n,t,s[MAXN];
int ans1,ans2,ans,k;
vector<int> g[MAXN]; int dfs(int fa,int now){
s[now] = ;
if(g[now].size() == && now != ){ return s[now];}
for(int i = ; i < g[now].size(); ++i){
if(g[now][i] != fa){
s[now] += dfs(now,g[now][i]);
}
}
return s[now];
} int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k); memset(s,,sizeof(s)); ans = ;
for(int i = ; i <= n; ++i){g[i].clear();}
for(int i = ; i < n; ++i){
int u,v; scanf("%d%d",&u,&v);
g[u].push_back(v); g[v].push_back(u);
}
dfs(-,);
// for(int i = 1; i <= n; ++i){
// i != n ? printf("%d ",s[i]) : printf("%d\n",s[i]);
// }
for(int i = ; i <= n; ++i){
int t1 = s[i]; int t2 = n - t1;
if(t1 >= k && t2 >= k) ++ans;
}
printf("%d\n",ans);
}
return ;
}
最新文章
- tomcat发布脚本
- iOS与Html5和JS之间的交互---学习笔记五
- Ubuntu14.04下解压rar压缩包
- poj 1556 The Doors
- linux系统终端命令提示符设置(PS1)记录
- 弄清const与指针、引用之间的关系
- jq 判断输入数字
- .net mvc mssql easyui treegrid 及时 编辑 ,支持拖拽
- Oracle-orclEXORIM
- bzoj4035 [HAOI2015]数组游戏
- CDRAF之Service mesh
- 美人鱼 hdu 5784
- python使用tkinter写带界面的工具
- 使用GIT SUBTREE集成项目到子目录(转)
- elasticsearch 请求体查询方式整理
- map reduce相关程序
- 分享6款国内、外开源PHP轻论坛CMS程序
- ubuntu 18 设置语言环境
- Zookeeper -- 本地\完全分布式 搭建
- MYSQL复习笔记5-select-from-where子句