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冲刺(3/7)
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 协助后端完成历史记录接口.美食排行榜接口 完成食堂平面图的绘制 确定web端业 ...
- 个人作业2--APP案例分析
产品 选择产品:酷狗音乐播放器 版本:Android版 选择理由:是我高中就开始用的音乐播放软件,在平时使用频率比较高,平时喜欢在累的时候听音乐放松. 调研 第一次上手体验 第一次使用的时候,感觉整个 ...
- TCP系列53—拥塞控制—16、Destination Metrics和Congestion Manager
一.概述 我们之前介绍过rtt.ssthresh等变量,这些变量一般在TCP连接建立的时候有个初始值,然后随着TCP的数据交互逐渐调整到适应对应的网络状态的值.但是如果每次TCP建立连接都依靠默认初始 ...
- yum与rmp
清理一切缓存[root@geust02 ~]# yum clean all 重建元数据[root@geust02 ~]# yum makecache 查询vim相关的软件包[root@geu ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 微服务架构之思维三部曲:What、Why、How
本文转自:http://www.servicemesh.cn/?/article/49 What:什么是微服务? 某百科对微服务架构的定义和阐述:微服务可以在“自己的程序”中运行,并通过“轻量级设备与 ...
- windows下python3.6安装pycryto or crypto or pycryptodome与使用
pycrypto,pycrytodome和crypto是一个东西,在很久以前,crypto在python上面的名字是pycrypto它是一个第三方库,但是已经停止更新三年了,所以不建议安装这个库: w ...
- 虚拟机centos 安装 redis 环境 linux 使用 java 远程连接 redis
redis官网地址:http://www.redis.io/ 最新版本:2.8.3 在Linux下安装Redis非常简单,具体步骤如下(官网有说明): 1.下载源码,解压缩后编译源码. $ wget ...
- mappers标签引入映射器的四种方式
第一种方式:mapper标签,通过resource属性引入classpath路径的相对资源 <!-- Using classpath relative resources --> < ...
- Zookeeper的基础
认识Zookeeper zookeeper是什么 分布式数据的一致性解决方案. Zookeeper 能做什么 数据发布和订阅(配置中心,config,disconf,diamond,appollo) ...