呵呵呵呵呵呵,自己画图,大概半个小时,觉的连上边会成环(是不是该交仙人掌了??)然后求环不重合部分最大就好了,

结果写了一坨DP,最后写不下去了,再次扒了题解。

发现我真的是个sb。

k==1,直接是直径

k==2,搞出直径然后把直径删掉(把权值赋为-1,再找直径)(有点像我一开始想的每次找个最长链去贪心,然而,,总觉得,这种题贪心这么可能对)

  1. /*#include <bits/stdc++.h>
  2. #define LL long long
  3. #define lowbit(x) x&(-x)
  4. #define inf 0x3f3f3f3f
  5. #define N 100005
  6. using namespace std;
  7. inline int ra()
  8. {
  9. int x=0,f=1; char ch=getchar();
  10. while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
  11. while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
  12. return x*f;
  13. }
  14. int head[N],cnt,n,f[N][3][3],k;
  15. struct edge{int next,to;}e[N<<1];
  16. void insert(int x, int y){e[++cnt].next=head[x]; e[cnt].to=y; head[x]=cnt;}
  17. void dfs(int x, int fa)
  18. {
  19. int mx1=0,mx2=0,mx3=0,mx4=0;
  20. for (int i=head[x];i;i=e[i].next)
  21. {
  22. if (e[i].to==fa) continue;
  23. dfs(e[i].to,x);
  24. int orz=f[e[i].to][0][1],flag=1,hehe=1;
  25. if (orz<=mx1 && orz<=mx2 && orz<=mx3 && orz>mx4) mx4=orz;
  26. if (orz<=mx1 && orz<=mx2 && orz>mx3) mx4=mx3,mx3=orz;
  27. if (orz<=mx1 && orz>mx2) mx4=mx3,mx3=mx2,mx2=orz,hehe=0;
  28. if (orz>mx1) mx4=mx3,mx3=mx2,mx2=mx1,mx1=orz,flag=0;
  29. if (f[x][1][1] && k)
  30. {
  31. if (!flag)
  32. f[x][2][0]=max(f[x][2][0],f[x][1][1]+mx1+1);
  33. else if (!hehe) f[x][2][0]=max(f[x][2][0],f[x][1][1]+mx2+1);
  34. }
  35. cout<<f[x][1][1]<<endl;
  36. if (f[x][1][0] && k) f[x][1][1]=max(f[x][1][1],f[x][1][0]+mx1+1);
  37. cout<<f[x][1][1]<<endl;
  38. if (f[e[i].to][1][0] && mx2 && k) f[x][1][1]=max(f[x][1][1],1+f[e[i].to][1][0]+flag?mx1:mx2);
  39. if (f[e[i].to][1][1] && k) f[x][1][1]=max(f[x][1][1],f[e[i].to][1][1]+1);
  40. if (f[e[i].to][1][1] && mx2 && k) f[x][2][0]=max(f[x][2][0],1+f[e[i].to][1][1]+flag?mx1:mx2);
  41. if (f[e[i].to][2][0] && k) f[x][2][0]=max(f[x][2][0],f[e[i].to][2][0]);
  42. if (f[x][1][0] && f[e[i].to][1][0] && k) f[x][2][0]=max(f[x][2][0],f[x][1][0]+f[e[i].to][1][0]);
  43. f[x][1][0]=max(f[x][1][0],max(mx1+mx2+1,f[e[i].to][1][0]));
  44. f[x][0][1]=mx1+1;
  45. if (f[3][1][1]==3) {cout<<x<<" "<<e[i].to; while (1);}
  46. }
  47. printf("%d %d %d %d %d\n",x,f[x][0][1],f[x][1][0],f[x][1][1],f[x][2][0]); system("pause");
  48. }
  49. int main(int argc, char const *argv[])
  50. {
  51. n=ra(); k=ra(); k--;
  52. for (int i=1; i<n; i++)
  53. {
  54. int x=ra(),y=ra();
  55. insert(x,y); insert(y,x);
  56. }
  57. dfs(1,0);
  58. printf("%d\n",n*2-f[1][k+1][0]);
  59. return 0;
  60. }*/
  61. #include <bits/stdc++.h>
  62. #define LL long long
  63. #define lowbit(x) x&(-x)
  64. #define inf 0x3f3f3f3f
  65. #define N 100005
  66. using namespace std;
  67. inline int ra()
  68. {
  69. int x=,f=; char ch=getchar();
  70. while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
  71. while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
  72. return x*f;
  73. }
  74. int mx,n,k,tot,cnt=,len;
  75. int head[N];
  76. int s1[N],s2[N];
  77. struct edge{
  78. int to,next,v;
  79. }e[N<<];
  80. void insert(int x, int y){
  81. e[++cnt].next=head[x]; e[cnt].to=y; e[cnt].v=; head[x]=cnt;
  82. }
  83. int dfs(int x, int fa)
  84. {
  85. int mx1=,mx2=;
  86. for (int i=head[x];i;i=e[i].next)
  87. {
  88. if (e[i].to==fa) continue;
  89. int v=e[i].v+dfs(e[i].to,x);
  90. if (v>mx1) mx2=mx1,mx1=v,s2[x]=s1[x],s1[x]=i;
  91. else if (v>mx2) mx2=v,s2[x]=i;
  92. }
  93. if (mx1+mx2>len) len=mx1+mx2,mx=x;
  94. return mx1;
  95. }
  96. int main(int argc, char const *argv[])
  97. {
  98. n=ra(); k=ra(); tot=*(n-);
  99. for (int i=; i<n; i++)
  100. {
  101. int x=ra(),y=ra();
  102. insert(x,y);
  103. insert(y,x);
  104. }
  105. dfs(,); tot-=len-;
  106. if (k==)
  107. {
  108. len=;
  109. for (int i=s1[mx];i;i=s1[e[i].to]) e[i].v=e[i^].v=-;
  110. for (int i=s2[mx];i;i=s1[e[i].to]) e[i].v=e[i^].v=-;
  111. dfs(,); tot-=len-;
  112. }
  113. cout<<tot;
  114. return ;
  115. }

bzoj 1912: [Apio2010]patrol 巡逻的更多相关文章

  1. BZOJ 1912: [Apio2010]patrol 巡逻 (树的直径)(详解)

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1912 题解: 首先,显然当不加边的时候,遍历一棵树每条边都要经过两次.那么现在考虑k==1 ...

  2. bzoj 1912 : [Apio2010]patrol 巡逻 树的直径

    题目链接 如果k==1, 显然就是直径. k==2的时候, 把直径的边权变为-1, 然后在求一次直径. 变为-1是因为如果在走一次这条边, 答案会增加1. 学到了新的求直径的方法... #includ ...

  3. bzoj 1912: [Apio2010]patrol 巡逻【不是dp是枚举+堆】

    我是智障系列.用了及其麻烦的方法= =其实树形sp就能解决 设直径长度+1为len(环长) 首先k=1,直接连直径两端就好,答案是2*n-len 然后对于k=2,正常人的做法是树形dp:先求直径,然后 ...

  4. 【BZOJ】1912: [Apio2010]patrol 巡逻(树的直径)

    题目 传送门:QWQ 分析 $ k=1 $ 时显然就是树的直径 $ k=2 $ 时怎么做呢? 做法是把一开始树的直径上的边的边权改成$ -1 $,那么当我们第二次用这些边做环时就抵消了一开始的贡献. ...

  5. BZOJ 1912:[Apio2010]patrol 巡逻(树直径)

    1912: [Apio2010]patrol 巡逻 Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ ...

  6. [Apio2010]patrol 巡逻

    1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 2541  Solved: 1288[Submit][S ...

  7. 【BZOJ1912】[Apio2010]patrol 巡逻 树形DP

    [BZOJ1912][Apio2010]patrol 巡逻 Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示 ...

  8. 【bzoj1912】 Apio2010—patrol 巡逻

    http://www.lydsy.com/JudgeOnline/problem.php?id=1912 (题目链接) 题意 给出一棵树,要求在树上添加K(1 or 2)条边,添加的边必须经过一次,使 ...

  9. BZOJ1912 [Apio2010]patrol 巡逻

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

随机推荐

  1. 重识线段树——Let's start with the start.

    声明 本文为 Clouder 原创,在未经许可情况下请不要随意转载.原文链接 前言 一般地,这篇文章是给学习过线段树却仍不透彻者撰写的,因此在某些简单的操作上可能会一笔带过. 当然了,入门线段树后也可 ...

  2. 4 ehcache 配置

    拷贝ehcache.xml文件到工程的resources目录下面 <?xml version="1.0" encoding="UTF-8"?> &l ...

  3. Batch批量替换hosts

    hosts文件替换 工作需要,要修改很多计算机的hosts文件,采用bat批量完成 解决的问题: 1.pc工作在非管理员权限,右键管理员权限太麻烦,因此采用执行中申请管理员权限的方式 2.hosts和 ...

  4. PaperReading20200222

    CanChen ggchen@mail.ustc.edu.cn   VS-GAE Motivation: With the publication of NAS101, researchers can ...

  5. 十五、web中处理乱码问题总结

    一.jsp变成之道---中文乱码 jsp在转换为Servlet的过程经过三次编码转化: 转自  http://www.cnblogs.com/chenssy/p/4235191.html 二.java ...

  6. [Codeforces #615 div3]1294E Obtain a Permutation

    Before the Beginniing 本文为 Clouder 原创文章,原文链接为Click,转载时请将本段放在文章开头显眼处.如进行了二次创作,请明确标明. 由本人转载于博客园. 题意分析 C ...

  7. [Codeforces #608 div2]1271C Shawarma Tent

    Description The map of the capital of Berland can be viewed on the infinite coordinate plane. Each p ...

  8. 前端学习笔记系列一:12 js中获取时间new date()的用法

    获取时间: 1  var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.get ...

  9. NO29 用户提权sudo配置文件详解实践--志行为审计

     用户提权sudo配置文件详解实践: 放到visudo里:  验证权限:

  10. Elasticsearch的快速使用——Spring Boot使用Elastcisearch, 并且使用Logstash同步mysql和Elasticsearch的数据

    我主要是给出一些方向,很多地方没有详细说明.当时我学习的时候一直不知道怎么着手,花时间找入口点上比较多,你们可以直接顺着方向去找资源学习. 如果不是Spring Boot项目,那么根据Elastics ...