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& ...
随机推荐
- 【CS231N】2、多类SVM
一.疑问 1. assignments1 linear_svm.py文件的函数 svm_loss_naive中,使用循环的方式实现梯度计算 linear_svm.py文件的函数 svm_loss_ve ...
- mvc 路由配置-学习
MapRoute(RouteCollection, String, String) 映射指定的URL路由. 'Declaration <ExtensionAttribute> _ Publ ...
- 前端切图相关ps技术
标签(空格分隔): 前端切图 复制图层到一个新的ps文件 对于单个图层 1.选中图层 2.CTRL+A全选 3.CTRL+C 4.CTRL+N新建文件,文件大小默认就可以(背景透明也在这个面板设置), ...
- 正确理解 SqlConnection 的连接池机制[转]
作者: eaglet 转载请注明出处 .net 中通过 SqlConnection 连接 sql server,我们会发现第一次连接时总是很耗时,但后面连接就很快,这个其实和SqlConnection ...
- 通过loadrunner将http返回response写入文本txt中
脚本如下 Action() { int myFile;//lr不支持FILE类型,所以定义一个int类型的file web_reg_save_param("goods_price" ...
- mysql数据优化--数据库结构的优化
1,比如存时间类型的就使用int类型 其中mysql的两个函数可以拿来使用 unix_timestamp 将时间日期转化为时间戳
- 微信小程序 功能函数 替换字符串内的指定字符
var str = 'abcadeacf'; var str1 = str.replace('a', 'o'); alert(str1); // 打印结果: obcadeacf var st ...
- JS 随机整数
<script> function GetRandomNum(Min,Max){ var Range = Max - Min; var Rand = Math.random() ...
- POJ1430
这个题目初看上去是一个排列组合题,而实际上……也是一个排列组合题. 题目描述是: Description The Stirling number of the second kind S(n, m) ...
- java使用Cookie判断用户登录情况
1.判断是否登录 public boolean isLogin() { Set<Cookie> cookies = this.browser.getCookies(); String JS ...