bzoj千题计划114:bzoj1791: [Ioi2008]Island 岛屿
http://www.lydsy.com/JudgeOnline/problem.php?id=1791
就是求所有基环树的直径之和
加手工栈
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm> using namespace std; #define N 1000001 typedef long long LL; int tot=,front[N],to[N<<],nxt[N<<],l[N<<]; bool vis[N];
int fa[N],dep[N]; int cir[N<<],val[N],cnt; bool inc[N]; LL dp[N],len[N<<],maxn,ans; int h,t,q[N<<]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void add(int u,int v,int w)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; l[tot]=w;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; l[tot]=w;
} int lev2; int st_i2[N],st_x2[N],st_y2[N],st_t2[N]; #define i st_i2[lev2]
#define y st_y2[lev2]
#define x st_x2[lev2]
#define t st_t2[lev2] void dfs(int s,int sf)
{
lev2=;
st_x2[]=s;
start:;
for(i=front[x];i;i=nxt[i])
{
t=to[i];
if(t==y || inc[t]) continue;
st_x2[lev2+]=t;
st_y2[lev2+]=x;
lev2++;
goto start;
end:;
maxn=max(maxn,dp[x]+dp[t]+l[i]);
dp[x]=max(dp[x],dp[t]+l[i]);
}
lev2--;
if(lev2) goto end;
} #undef i
#undef y
#undef t
#undef x int lev; int st_i[N],st_x[N],st_y[N],st_t[N]; #define i st_i[lev]
#define y st_y[lev]
#define x st_x[lev]
#define t st_t[lev] void findcircle(int s)
{
lev=;
st_x[lev]=s;
dep[s]=;
start:;
vis[x]=true;
for(i=front[x];i;i=nxt[i])
{
t=to[i];
if(!vis[t])
{
fa[t]=x;
val[x]=l[i];
dep[t]=dep[x]+;
st_x[lev+]=t;
st_y[lev+]=i;
lev++;
goto start;
end:;
}
else if(i!=(y^))
{
cnt=dep[x]-dep[t]+;
int now=x;
val[x]=l[i];
while(dep[now]>=dep[t])
{
inc[now]=true;
len[cnt]=val[now];
cir[cnt--]=now;
now=fa[now];
}
cnt=dep[x]-dep[t]+;
int nn=cnt;
for(int j=;j<=cnt;++j) cir[++nn]=cir[j],len[nn]=len[j];
for(int j=;j<=nn;++j) len[j]+=len[j-];
for(int j=;j<=cnt;++j) dfs(cir[j],);
h=t=;
for(int j=;j<=nn;++j)
{
while(h<t && q[h]<=j-cnt) h++;
if(h<t) maxn=max(maxn,dp[cir[q[h]]]+dp[cir[j]]+len[j-]-len[q[h]-]);
while(h<t && dp[cir[j]]-len[j-]>dp[cir[q[t-]]]-len[q[t-]-]) t--;
q[t++]=j;
}
} }
lev--;
if(lev) goto end;
} #undef i
#undef y
#undef t
#undef x int main()
{
// freopen("isl.in","r",stdin);
// freopen("isl.out","w",stdout);
int n;
read(n);
int x,y;
for(int i=;i<=n;++i)
{
read(x); read(y);
add(i,x,y);
}
for(int i=;i<=n;++i)
if(!vis[i])
{
maxn=;
findcircle(i);
ans+=maxn;
}
cout<<ans;
}
1791: [Ioi2008]Island 岛屿
Time Limit: 20 Sec Memory Limit: 162 MB
Submit: 1795 Solved: 400
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
3 8
7 2
4 2
1 4
1 9
3 4
2 3
Sample Output
HINT

Source
bzoj千题计划114:bzoj1791: [Ioi2008]Island 岛屿的更多相关文章
- [bzoj1791][ioi2008]Island 岛屿(基环树、树的直径)
[bzoj1791][ioi2008]Island 岛屿(基环树.树的直径) bzoj luogu 题意可能会很绕 一句话:基环树的直径. 求直径: 对于环上每一个点记录其向它的子树最长路径为$dp_ ...
- BZOJ1791: [Ioi2008]Island 岛屿
BZOJ1791: [Ioi2008]Island 岛屿 Description 你将要游览一个有N个岛屿的公园. 从每一个岛i出发,只建造一座桥. 桥的长度以Li表示. 公园内总共有N座桥. 尽管每 ...
- bzoj1791: [Ioi2008]Island 岛屿 单调队列优化dp
1791: [Ioi2008]Island 岛屿 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1826 Solved: 405[Submit][S ...
- bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块
http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...
- bzoj千题计划196:bzoj4826: [Hnoi2017]影魔
http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...
- bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪
http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...
- bzoj千题计划177:bzoj1858: [Scoi2010]序列操作
http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...
- bzoj千题计划317:bzoj4650: [Noi2016]优秀的拆分(后缀数组+差分)
https://www.lydsy.com/JudgeOnline/problem.php?id=4650 如果能够预处理出 suf[i] 以i结尾的形式为AA的子串个数 pre[i] 以i开头的形式 ...
- bzoj千题计划304:bzoj3676: [Apio2014]回文串(回文自动机)
https://www.lydsy.com/JudgeOnline/problem.php?id=3676 回文自动机模板题 4年前的APIO如今竟沦为模板,,,╮(╯▽╰)╭,唉 #include& ...
随机推荐
- 软工1816 · Beta冲刺(5/7)
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 推进后端完成安卓端接口的开发 在测试中发现返回地图接口存在错误(待修复) 推进 ...
- 在visual studio中查看源代码
地址:https://docs.microsoft.com/zh-cn/visualstudio/ide/go-to-and-peek-definition?view=vs-2017 在 Visual ...
- #1490 : Tree Restoration
微软 2017春招真题 题目 There is a tree of N nodes which are numbered from 1 to N. Unfortunately, its edges a ...
- 未能加载文件或程序集“Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件
发布的打包项目在本机测试好使,部署到客户服务器上报错 分析器错误消息: 未能加载文件或程序集“Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Cu ...
- syntax error:unexpected end of file
将window上编辑的xxy1.sh脚本上传到linux上,并执行的时候提示 xxy1.sh: line 17: syntax error: unexpected end of file 但是通过ca ...
- IE下Userdata本地化存储
这两天看了下Discuz x2发帖的实时保存机制,涉及到本地化存储,所以上网查了下,Firefox等支持HTML5的浏览器使用window.localStorage或window.sessionSto ...
- STM32标准外设库、 HAL库、LL库
工作以来一直使用ST的STM32系列芯片,ST为开发者提供了非常方便的开发库.到目前为止,有标准外设库(STD库).HAL库.LL库 三种.前两者都是常用的库,后面的LL库是ST最近才添加,目前支持的 ...
- UFLDL学习笔记 ---- 主成分分析与白化
主成分分析(PCA)是用来提升无监督特征学习速度的数据降维算法.看过下文大致可以知道,PCA本质是对角化协方差矩阵,目的是让维度之间的相关性最小(降噪),保留下来的维度能量最大(去冗余),PCA在图像 ...
- 基本数据结构 —— 堆以及堆排序(C++实现)
目录 什么是堆 堆的存储 堆的操作 结构体定义 判断是否为空 往堆中插入元素 从堆中删除元素 取出堆中最大的元素 堆排序 测试代码 例题 参考资料 什么是堆 堆(英语:heap)是计算机科学中一类特殊 ...
- MySQL事务及隔离级别(读书小结)
标签: MySQL事务 隔离 0.什么是事务? 事务是指MySQL的一些操作看做是一个不可分割的执行单元.事务的特点是要么所有操作都执行成功,要么一个都不执行.也就是如果一个事务有操作执行失败,那么就 ...