bzoj 1912: [Apio2010]patrol 巡逻
呵呵呵呵呵呵,自己画图,大概半个小时,觉的连上边会成环(是不是该交仙人掌了??)然后求环不重合部分最大就好了,
结果写了一坨DP,最后写不下去了,再次扒了题解。
发现我真的是个sb。
k==1,直接是直径
k==2,搞出直径然后把直径删掉(把权值赋为-1,再找直径)(有点像我一开始想的每次找个最长链去贪心,然而,,总觉得,这种题贪心这么可能对)
- /*#include <bits/stdc++.h>
- #define LL long long
- #define lowbit(x) x&(-x)
- #define inf 0x3f3f3f3f
- #define N 100005
- using namespace std;
- inline int ra()
- {
- int x=0,f=1; char ch=getchar();
- while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
- while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
- return x*f;
- }
- int head[N],cnt,n,f[N][3][3],k;
- struct edge{int next,to;}e[N<<1];
- void insert(int x, int y){e[++cnt].next=head[x]; e[cnt].to=y; head[x]=cnt;}
- void dfs(int x, int fa)
- {
- int mx1=0,mx2=0,mx3=0,mx4=0;
- for (int i=head[x];i;i=e[i].next)
- {
- if (e[i].to==fa) continue;
- dfs(e[i].to,x);
- int orz=f[e[i].to][0][1],flag=1,hehe=1;
- if (orz<=mx1 && orz<=mx2 && orz<=mx3 && orz>mx4) mx4=orz;
- if (orz<=mx1 && orz<=mx2 && orz>mx3) mx4=mx3,mx3=orz;
- if (orz<=mx1 && orz>mx2) mx4=mx3,mx3=mx2,mx2=orz,hehe=0;
- if (orz>mx1) mx4=mx3,mx3=mx2,mx2=mx1,mx1=orz,flag=0;
- if (f[x][1][1] && k)
- {
- if (!flag)
- f[x][2][0]=max(f[x][2][0],f[x][1][1]+mx1+1);
- else if (!hehe) f[x][2][0]=max(f[x][2][0],f[x][1][1]+mx2+1);
- }
- cout<<f[x][1][1]<<endl;
- if (f[x][1][0] && k) f[x][1][1]=max(f[x][1][1],f[x][1][0]+mx1+1);
- cout<<f[x][1][1]<<endl;
- 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);
- if (f[e[i].to][1][1] && k) f[x][1][1]=max(f[x][1][1],f[e[i].to][1][1]+1);
- 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);
- if (f[e[i].to][2][0] && k) f[x][2][0]=max(f[x][2][0],f[e[i].to][2][0]);
- 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]);
- f[x][1][0]=max(f[x][1][0],max(mx1+mx2+1,f[e[i].to][1][0]));
- f[x][0][1]=mx1+1;
- if (f[3][1][1]==3) {cout<<x<<" "<<e[i].to; while (1);}
- }
- 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");
- }
- int main(int argc, char const *argv[])
- {
- n=ra(); k=ra(); k--;
- for (int i=1; i<n; i++)
- {
- int x=ra(),y=ra();
- insert(x,y); insert(y,x);
- }
- dfs(1,0);
- printf("%d\n",n*2-f[1][k+1][0]);
- return 0;
- }*/
- #include <bits/stdc++.h>
- #define LL long long
- #define lowbit(x) x&(-x)
- #define inf 0x3f3f3f3f
- #define N 100005
- using namespace std;
- inline int ra()
- {
- int x=,f=; char ch=getchar();
- while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
- while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
- return x*f;
- }
- int mx,n,k,tot,cnt=,len;
- int head[N];
- int s1[N],s2[N];
- struct edge{
- int to,next,v;
- }e[N<<];
- void insert(int x, int y){
- e[++cnt].next=head[x]; e[cnt].to=y; e[cnt].v=; head[x]=cnt;
- }
- int dfs(int x, int fa)
- {
- int mx1=,mx2=;
- for (int i=head[x];i;i=e[i].next)
- {
- if (e[i].to==fa) continue;
- int v=e[i].v+dfs(e[i].to,x);
- if (v>mx1) mx2=mx1,mx1=v,s2[x]=s1[x],s1[x]=i;
- else if (v>mx2) mx2=v,s2[x]=i;
- }
- if (mx1+mx2>len) len=mx1+mx2,mx=x;
- return mx1;
- }
- int main(int argc, char const *argv[])
- {
- n=ra(); k=ra(); tot=*(n-);
- for (int i=; i<n; i++)
- {
- int x=ra(),y=ra();
- insert(x,y);
- insert(y,x);
- }
- dfs(,); tot-=len-;
- if (k==)
- {
- len=;
- for (int i=s1[mx];i;i=s1[e[i].to]) e[i].v=e[i^].v=-;
- for (int i=s2[mx];i;i=s1[e[i].to]) e[i].v=e[i^].v=-;
- dfs(,); tot-=len-;
- }
- cout<<tot;
- return ;
- }
bzoj 1912: [Apio2010]patrol 巡逻的更多相关文章
- BZOJ 1912: [Apio2010]patrol 巡逻 (树的直径)(详解)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1912 题解: 首先,显然当不加边的时候,遍历一棵树每条边都要经过两次.那么现在考虑k==1 ...
- bzoj 1912 : [Apio2010]patrol 巡逻 树的直径
题目链接 如果k==1, 显然就是直径. k==2的时候, 把直径的边权变为-1, 然后在求一次直径. 变为-1是因为如果在走一次这条边, 答案会增加1. 学到了新的求直径的方法... #includ ...
- bzoj 1912: [Apio2010]patrol 巡逻【不是dp是枚举+堆】
我是智障系列.用了及其麻烦的方法= =其实树形sp就能解决 设直径长度+1为len(环长) 首先k=1,直接连直径两端就好,答案是2*n-len 然后对于k=2,正常人的做法是树形dp:先求直径,然后 ...
- 【BZOJ】1912: [Apio2010]patrol 巡逻(树的直径)
题目 传送门:QWQ 分析 $ k=1 $ 时显然就是树的直径 $ k=2 $ 时怎么做呢? 做法是把一开始树的直径上的边的边权改成$ -1 $,那么当我们第二次用这些边做环时就抵消了一开始的贡献. ...
- BZOJ 1912:[Apio2010]patrol 巡逻(树直径)
1912: [Apio2010]patrol 巡逻 Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ ...
- [Apio2010]patrol 巡逻
1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 2541 Solved: 1288[Submit][S ...
- 【BZOJ1912】[Apio2010]patrol 巡逻 树形DP
[BZOJ1912][Apio2010]patrol 巡逻 Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示 ...
- 【bzoj1912】 Apio2010—patrol 巡逻
http://www.lydsy.com/JudgeOnline/problem.php?id=1912 (题目链接) 题意 给出一棵树,要求在树上添加K(1 or 2)条边,添加的边必须经过一次,使 ...
- BZOJ1912 [Apio2010]patrol 巡逻
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
随机推荐
- 重识线段树——Let's start with the start.
声明 本文为 Clouder 原创,在未经许可情况下请不要随意转载.原文链接 前言 一般地,这篇文章是给学习过线段树却仍不透彻者撰写的,因此在某些简单的操作上可能会一笔带过. 当然了,入门线段树后也可 ...
- 4 ehcache 配置
拷贝ehcache.xml文件到工程的resources目录下面 <?xml version="1.0" encoding="UTF-8"?> &l ...
- Batch批量替换hosts
hosts文件替换 工作需要,要修改很多计算机的hosts文件,采用bat批量完成 解决的问题: 1.pc工作在非管理员权限,右键管理员权限太麻烦,因此采用执行中申请管理员权限的方式 2.hosts和 ...
- PaperReading20200222
CanChen ggchen@mail.ustc.edu.cn VS-GAE Motivation: With the publication of NAS101, researchers can ...
- 十五、web中处理乱码问题总结
一.jsp变成之道---中文乱码 jsp在转换为Servlet的过程经过三次编码转化: 转自 http://www.cnblogs.com/chenssy/p/4235191.html 二.java ...
- [Codeforces #615 div3]1294E Obtain a Permutation
Before the Beginniing 本文为 Clouder 原创文章,原文链接为Click,转载时请将本段放在文章开头显眼处.如进行了二次创作,请明确标明. 由本人转载于博客园. 题意分析 C ...
- [Codeforces #608 div2]1271C Shawarma Tent
Description The map of the capital of Berland can be viewed on the infinite coordinate plane. Each p ...
- 前端学习笔记系列一:12 js中获取时间new date()的用法
获取时间: 1 var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.get ...
- NO29 用户提权sudo配置文件详解实践--志行为审计
用户提权sudo配置文件详解实践: 放到visudo里: 验证权限:
- Elasticsearch的快速使用——Spring Boot使用Elastcisearch, 并且使用Logstash同步mysql和Elasticsearch的数据
我主要是给出一些方向,很多地方没有详细说明.当时我学习的时候一直不知道怎么着手,花时间找入口点上比较多,你们可以直接顺着方向去找资源学习. 如果不是Spring Boot项目,那么根据Elastics ...