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

思路:

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

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<string>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<algorithm>
  7. #include<map>
  8. #include<queue>
  9. #include<vector>
  10. #include<ctime>
  11. using namespace std;
  12. typedef long long ll;
  13. typedef unsigned int uint;
  14. typedef unsigned long long ull;
  15. typedef pair<int,int> PII;
  16. typedef vector<int> VI;
  17. #define fi first
  18. #define se second
  19. #define MP make_pair
  20. #define mem0(a) memset(a,0,sizeof(a))
  21. #define N 210000
  22. #define M 130
  23. #define MOD 1000000007
  24. #define eps 1e-8
  25. #define pi acos(-1)
  26. #define oo 1000000000
  27.  
  28. int flag[N],f[N],a[N],n,mx;
  29. vector<int>c[N];
  30.  
  31. void dfs(int u)
  32. {
  33. flag[u]=;
  34. if(a[u])
  35. {
  36. int t=;
  37. for(int i=;i<=(int)c[u].size()-;i++)
  38. {
  39. int v=c[u][i];
  40. if(!flag[v])
  41. {
  42. dfs(v);
  43. if(f[v]<) t-=f[v];
  44. }
  45. }
  46. f[u]=t+;
  47. }
  48. else
  49. {
  50. int t=,s=oo;
  51. for(int i=;i<=(int)c[u].size()-;i++)
  52. {
  53. int v=c[u][i];
  54. if(!flag[v])
  55. {
  56. dfs(v);
  57. if(f[v]<) t-=f[v];
  58. else s=min(s,f[v]);
  59. }
  60. }
  61. if(mx-s>=t+) f[u]=s+t+;
  62. else f[u]=-t-;
  63. }
  64. }
  65.  
  66. int isok(int m)
  67. {
  68. mem0(flag);
  69. mem0(f);
  70. mx=m;
  71. dfs();
  72. int flag=;
  73. if(f[]<) return ;
  74. for(int i=;i<=n;i++)
  75. if(abs(f[i])>m) return ;
  76. return ;
  77. }
  78.  
  79. int main()
  80. {
  81. int k;
  82. scanf("%d%d",&n,&k);
  83. for(int i=;i<=n-;i++)
  84. {
  85. int x,y;
  86. scanf("%d%d",&x,&y);
  87. c[x].push_back(y);
  88. c[y].push_back(x);
  89. }
  90. for(int i=;i<=k;i++)
  91. {
  92. int x;
  93. scanf("%d",&x);
  94. a[x]=;
  95. }
  96. int l=,r=n,last=n;
  97. while(l<=r)
  98. {
  99. int mid=(l+r)>>;
  100. if(isok(mid)){last=mid; r=mid-;}
  101. else l=mid+;
  102. }
  103. printf("%d\n",last);
  104. return ;
  105. }

【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. guacamole实现上传下载

    目录 1. 源码解读 2. 上传下载的核心代码 分析的入手点,查看websocket连接的frame 看到首先服务端向客户端发送了filesystem请求,紧接着浏览器向服务端发送了get请求,并且后 ...

  2. 引用外部静态库(.a文件)时或打包.a时,Category方法无法调用。崩溃

    我的这个是MJRefresh,学习打.a包Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ...

  3. BZOJ 4029 HEOI2015 定价 数位贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4029 题意概述:对于一个数字的荒谬程度定义如下:删除其所有的后缀0,然后得到的数字长度为a ...

  4. URAL 1736 Chinese Hockey(网络最大流)

    Description Sergey and Denis closely followed the Chinese Football Championship, which has just come ...

  5. C++STL——队列

    一.相关定义 原理:queue 队列也是一个线性存储表,元素数据的插入在表的一端进行,在另一端删除,从而构成了一个先进先出FIFO(First In First Out)表. 队头&队尾:插入 ...

  6. Week1 Team Homework #1 from Z.XML-对于学长项目《shield star》的思考和看法

    试用了一下学长黄杨等人开发的<shield star>游戏.                      其实作为一个学弟,我对cocos2d-x引擎还算是比较了解,所以对于这样一款很“典型 ...

  7. 【转】C++操作符的优先级 及其记忆方法

    优先级 操作符 描述 例子 结合性 1 ()[]->.::++-- 调节优先级的括号操作符数组下标访问操作符通过指向对象的指针访问成员的操作符通过对象本身访问成员的操作符作用域操作符后置自增操作 ...

  8. 解决Mysql错误Too many connections的方法

    MySQL数据库 Too many connections出现这种错误明显就是 mysql_connect 之后忘记 mysql_close:当大量的connect之后,就会出现Too many co ...

  9. elementUI的导航栏怎么根据路由默认选中相关项

    1. <el-menu :default-active="this.$route.path.substr(1)" class="left-nav"> ...

  10. [C/C++] C++常见面试题

    参考:http://blog.csdn.net/shihui512/article/details/9092439 1.new.delete.malloc.free之间的关系 malloc和free都 ...