题意:有一棵n个点的树,其中有k个是关键点,将树分割成若干部分,每部分至少包含一个关键点,求最大分割大小的最小值

思路:

最后特判一下f[1]的值

 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define mem0(a) memset(a,0,sizeof(a))
#define N 210000
#define M 130
#define MOD 1000000007
#define eps 1e-8
#define pi acos(-1)
#define oo 1000000000 int flag[N],f[N],a[N],n,mx;
vector<int>c[N]; void dfs(int u)
{
flag[u]=;
if(a[u])
{
int t=;
for(int i=;i<=(int)c[u].size()-;i++)
{
int v=c[u][i];
if(!flag[v])
{
dfs(v);
if(f[v]<) t-=f[v];
}
}
f[u]=t+;
}
else
{
int t=,s=oo;
for(int i=;i<=(int)c[u].size()-;i++)
{
int v=c[u][i];
if(!flag[v])
{
dfs(v);
if(f[v]<) t-=f[v];
else s=min(s,f[v]);
}
}
if(mx-s>=t+) f[u]=s+t+;
else f[u]=-t-;
}
} int isok(int m)
{
mem0(flag);
mem0(f);
mx=m;
dfs();
int flag=;
if(f[]<) return ;
for(int i=;i<=n;i++)
if(abs(f[i])>m) return ;
return ;
} int main()
{
int k;
scanf("%d%d",&n,&k);
for(int i=;i<=n-;i++)
{
int x,y;
scanf("%d%d",&x,&y);
c[x].push_back(y);
c[y].push_back(x);
}
for(int i=;i<=k;i++)
{
int x;
scanf("%d",&x);
a[x]=;
}
int l=,r=n,last=n;
while(l<=r)
{
int mid=(l+r)>>;
if(isok(mid)){last=mid; r=mid-;}
else l=mid+;
}
printf("%d\n",last);
return ;
}

【EOJ3654】管理孩子(贪心,二分)的更多相关文章

  1. poj 2782 Bin Packing (贪心+二分)

    F - 贪心+ 二分 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description ...

  2. Card Game Cheater(贪心+二分匹配)

    Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分

    Heap Partition Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A sequence S = { ...

  4. 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

    题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...

  5. Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找

    The link to problem:Problem - D - Codeforces   D. Range and Partition  time limit per test: 2 second ...

  6. zoj-3963 Heap Partition(贪心+二分+树状数组)

    题目链接: Heap Partition Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A sequence ...

  7. LightOj1383 - Underwater Snipers(贪心 + 二分)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1383 题意:在平面图中,有一条河,用直线y=k表示,河上面(y>k)的都是敌方区 ...

  8. Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分

    C. GukiZ hates Boxes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...

  9. UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)

      Copying Books  Before the invention of book-printing, it was very hard to make a copy of a book. A ...

随机推荐

  1. Hystrix入门指南

    Introduction 1.Where does the name come from? hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与 ...

  2. FreeRTOS软件定时器的使用

    先贴上一个创建的代码,先声明一个句柄 TimerHandle_t pump_wakeup_timer_handle = NULL; 创建定时器和启动定时器,第三个参数,pdFALSE是只定时一次,pd ...

  3. OpenCV入门:(一:安装与配置)

    看到的不是自己的,只有写下来的才是自己的,上次接触OpenCV实在三个月前,亢奋的看完了OpenCV自带的入门文档,觉得对图形处理有了一点点了解,现在三个月过去了,由于学习需要,想深入了解OpenCV ...

  4. Qt 贪吃蛇小游戏

    简单的实现了走和变大的样子,剩下的还在完善 贴代码 #include "mainwindow.h" #include "ui_mainwindow.h" #in ...

  5. MySQL☞length函数

    length(字符串/列名):求出该字符串/列名中字符的个数 格式: select  length(列名)  from 表名 如下图:

  6. kafka java动态获取topic并动态创建消费者

    1.获取所有topic package com.example.demo; import java.io.IOException; import java.util.List; import org. ...

  7. Daily Scrum02 12.05

    deadline果然是第一生产力...这学期一下子4~5个大的Project.然后截止日期都在近期.所有的组员都很辛苦!大家加油~ 这个scrum是当天过后一天补上的.因为当前负责的同学正在忙于编译大 ...

  8. 【转】cocos2dx 3.x 集成protobuf

    http://www.cnblogs.com/chevin/p/6001872.html vs2013+cocos2dx 3.13.1 这篇博文是集成Lua版本的protobuf,集成C++版本的过程 ...

  9. 【Linux】——搭建nexus

    1.安装 前提条件: JDK已经安装,运行java -version查看. 将本地下载好的nexus存放到linux上,存放路径为 /usr/local/software.可使用winscp直接拷贝. ...

  10. 从零开始配置Jenkins(二)——常见问题及排错思路

    [前言] 一年多以前就听说Jenkins了,那时知道是它可以完成自动构建,感觉蛮强大的.后来,很多人都说它很恶心.最近,公司需要搭建新的服务器,小编就负责从头开始配置并且发布部署成功每一条线每一个项目 ...