【BZOJ 1023】【SHOI 2008】cactus仙人掌图
良心的题解↓
http://z55250825.blog.163.com/blog/static/150230809201412793151890/
tarjan的时候如果是树边则做树形DP(遇到环就无视),最后在tarjan回溯前扫一遍当前点为“最高点”的环,进行环上DP,这个环上DP是$O(n^2)$的,但如果我们用单调队列优化则是$O(n)$的
总复杂度$O(n)$真是无限仰膜OTZ
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 500003;
void read(int &k) {
k = 0; int fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 1) + (k << 3) + c - '0';
k = k * fh;
} struct node {int nxt, to;} E[N << 1];
int ans = 0, point[N], n, m, cnt = 0, l, r;
int deep[N], F[N], fa[N], DFN[N], low[N], Q[N << 1], tb[N << 1]; void ins(int x, int y) {E[++cnt] = (node) {point[x], y}; point[x] = cnt;} void __(int rt, int x) {
int tot = deep[x] - deep[rt] + 1, num = tot << 1, top = tot >> 1;
for(int tmp = x; tmp != rt; tmp = fa[tmp]) tb[tot--] = F[tmp];
tb[1] = F[rt];
tot = deep[x] - deep[rt] + 1;
for(int i = 1; i <= tot; ++i) tb[tot + i] = tb[i];
l = r = 1; Q[1] = 1;
for(int i = 2; i <= num; ++i) {
while (l <= r && i - Q[l] > top) ++l;
ans = max(ans, tb[i] + i + tb[Q[l]] - Q[l]);
while (l <= r && tb[Q[r]] - Q[r] <= tb[i] - i) --r;
Q[++r] = i;
}
for(int i = 2; i <= tot; ++i)
F[rt] = max(F[rt], tb[i] + min(i - 1, tot - i + 1));
} void _(int x) {
DFN[x] = low[x] = ++cnt;
for(int tmp = point[x]; tmp; tmp = E[tmp].nxt) {
int v = E[tmp].to;
if (v == fa[x]) continue;
if (!DFN[v]) {
fa[v] = x; deep[v] = deep[x] + 1;
_(v); low[x] = min(low[x], low[v]);
} else
low[x] = min(low[x], DFN[v]);
if (DFN[x] < low[v]) {
ans = max(ans, F[x] + F[v] + 1);
F[x] = max(F[x], F[v] + 1);
}
}
for(int tmp = point[x]; tmp; tmp = E[tmp].nxt) {
int v = E[tmp].to;
if (x == fa[v] || DFN[x] > DFN[v]) continue;
__(x, v);
}
} int main() {
read(n); read(m); int u, v, k;
for(int i = 1; i <= m; ++i) {
read(k); read(u);
for(--k; k; --k) {read(v); ins(u, v); ins(v, u); u = v;}
} cnt = 0;
_(1);
printf("%d\n", ans);
return 0;
}
仙人掌虽然偏,但是不知道也不可以,这个代码是我磕了一晚上题解的成果QAQ
【BZOJ 1023】【SHOI 2008】cactus仙人掌图的更多相关文章
- 【BZOJ 1023】[SHOI2008]cactus仙人掌图
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1023 [题意] [题解] 如果不考虑有环的情况; 那么有一个经典的求树的直径的方法; ...
- 【BZOJ】【1023】【SHOI2008】cactus仙人掌图
DP+单调队列/仙人掌 题解:http://hzwer.com/4645.html->http://z55250825.blog.163.com/blog/static/150230809201 ...
- bzoj 1023: [SHOI2008]cactus仙人掌图 tarjan缩环&&环上单调队列
1023: [SHOI2008]cactus仙人掌图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1141 Solved: 435[Submit][ ...
- bzoj千题计划113:bzoj1023: [SHOI2008]cactus仙人掌图
http://www.lydsy.com/JudgeOnline/problem.php?id=1023 dp[x] 表示以x为端点的最长链 子节点与x不在同一个环上,那就是两条最长半链长度 子节点与 ...
- SHOI2008 cactus仙人掌图 和 UOJ87 mx的仙人掌
cactus仙人掌图 题目描述 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus).所谓简单回路就是指在图上不重复经过任何一 ...
- [BZOJ]1023 cactus仙人掌图(SHOI2008)
NOIP后的第一次更新嗯. Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus).所谓简单回路就是指在 ...
- 1023: [SHOI2008]cactus仙人掌图 - BZOJ
Description如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人图(cactus).所谓简单回路就是指在图上不重复经过任何一个顶点的回路 ...
- 【刷题】BZOJ 1023 [SHOI2008]cactus仙人掌图
Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus).所谓简单回路就是指在图上不重复经过任何一个顶点的 ...
- 【BZOJ】1023: [SHOI2008]cactus仙人掌图 静态仙人掌(DFS树)
[题意]给定仙人掌图(每条边至多在一个简单环上),求直径(最长的点对最短路径).n<=50000,m<=10^7. [算法]DFS树处理仙人掌 [题解]参考:仙人掌相关问题的处理方法(未完 ...
- 1023: [SHOI2008]cactus仙人掌图(DP+单调队列优化)
这道题吗= =首先解决了我多年以来对仙人掌图的疑问,原来这种高大上的东西原来是这个啊= = 然后,看到这种题,首先必须的就是缩点= = 缩点完之后呢,变成在树上找最长路了= =直接树形dp了 那么那些 ...
随机推荐
- BZOJ2429[HAOI2006]聪明的猴子[最小生成树 kruskal]
2429: [HAOI2006]聪明的猴子 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 896 Solved: 575[Submit][Statu ...
- java内存设置
在上边红色框内加入java内存设置命令: -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
- matrix(No.1)operations
- 《JavaScript基础教程》
第五章.窗口与框架 5.2 设置目标 源代码: //主页面:Captain.html <!DOCTYPE html> <html lang="en" xmlns= ...
- css3和原生js时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 重写setTimeout扩展参数
//判断函数行参长度来决定是否需要重写setTimeout,ie8以下为undefined if(window.setTimeout.length == undefined){ var __sto = ...
- filestream read方法 循环读取固定文件
1.循环读取啊,byte[]可以定义为1024或者2049等等,不要超过int的maxvalue就可以.然后取出来操作完再去取. FileStream stream = new FileStream( ...
- ssh生成key不交互
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" 首次执行不交互 第二次再次执行会让输入y
- mac上远程连接windows
Microsoft 适用于 Mac 的远程桌面连接客户端 2.1.1 http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=1814 ...
- Android -- Apk安装简诉
安装涉及到如下几个目录 system/app 系统自带的应用程序,无法删除 data/app 用户程序安装的目录,有删除权限. 安装时把apk文件复制到此目录 data/data 存放 ...