博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ 1023: [SHOI2008]cactus仙人掌图
阅读量:5337 次
发布时间:2019-06-15

本文共 2154 字,大约阅读时间需要 7 分钟。

思路:先求出非环上的最长链,然后环上用单调队列优化dp。
代码:

#pragma GCC optimize(2)#pragma GCC optimize(3)#pragma GCC optimize(4)#include
using namespace std;#define y1 y11#define fi first#define se second#define pi acos(-1.0)#define LL long long//#define mp make_pair#define pb push_back#define ls rt<<1, l, m#define rs rt<<1|1, m+1, r#define ULL unsigned LL#define pll pair
#define pli pair
#define pii pair
#define piii pair
#define pdd pair
#define mem(a, b) memset(a, b, sizeof(a))#define debug(x) cerr << #x << " = " << x << "\n";#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//headconst int N = 5e4 + 5;vector
g[N];int fa[N], deep[N], dp[N], a[N*2], n, m, t, ans;int dfn[N], low[N], cnt;inline void DP(int u, int v) { int t = 0; for (int i = v; i != u; i = fa[i]) a[++t] = i; a[++t] = u; for (int i = 1; i <= t; ++i) a[i+t] = a[i]; deque
q; for (int i = 1; i <= 2*t; ++i) { while(!q.empty() && i-q.front() > t/2) q.pop_front(); if(!q.empty()) ans = max(ans, dp[a[i]]+dp[a[q.front()]]+i-q.front()); while(!q.empty() && dp[a[q.back()]]-q.back() <= dp[a[i]]-i) q.pop_back(); q.push_back(i); } for (int i = v; i != u; i = fa[i]) { dp[u] = max(dp[u], dp[i]+min(deep[i]-deep[u], deep[v]-deep[i]+1)); }}inline void tarjan(int u, int o) { fa[u] = o; deep[u] = deep[o]+1; dfn[u] = low[u] = ++cnt; for (int i = 0; i < g[u].size(); ++i) { int v = g[u][i]; if(v == o) continue; if(!dfn[v]) { tarjan(v, u); low[u] = min(low[u], low[v]); } else low[u] = min(low[u], dfn[v]); if(low[v] > dfn[u]) { ans = max(ans, dp[u]+dp[v]+1); dp[u] = max(dp[u], dp[v]+1); } } for (int i = 0; i < g[u].size(); ++i) { int v = g[u][i]; if(fa[v] != u && dfn[v] > dfn[u]) { DP(u, v); } }}int main() { while(~scanf("%d %d", &n, &m)) { cnt = ans = 0; for (int i = 1; i <= m; ++i) { scanf("%d", &t); for (int i = 1; i <= t; ++i) scanf("%d", &a[i]); for (int i = 2; i <= t; ++i) g[a[i-1]].pb(a[i]), g[a[i]].pb(a[i-1]); } tarjan(1, 0); printf("%d\n", ans); for (int i = 1; i <= n; ++i) g[i].clear(), low[i] = dfn[i] = dp[i] = 0; } return 0;}

转载于:https://www.cnblogs.com/widsom/p/11461334.html

你可能感兴趣的文章
SpringMVC 拦截器简单配置
查看>>
第一次作业小结
查看>>
erlang http post 发送数据请求
查看>>
Unresolved external CheckAutoResult
查看>>
[收藏转贴]struct探索·extern "C"含义探索 ·C++与C的混合编程·C 语言高效编程的几招...
查看>>
tinkcmf视频上传大小限制
查看>>
解决“并非来自 Chrome 网上应用店。”
查看>>
《第1章:统计学习方法概论》
查看>>
JS定时器时间日期钟表
查看>>
partial(C# 参考)
查看>>
Supervisor介绍、安装及配置
查看>>
openshift 添加cron定时任务
查看>>
sublime text3在指定浏览器上本地服务器(localhost)运行文件(php)
查看>>
【ABAP系列】SAP ABAP基础-录制BDC的MODE定义解析
查看>>
C++编写DLL的方法
查看>>
自适应布局1
查看>>
docker不稳定 short running containers with -rm failed to destroy
查看>>
poj 3071 Football (概率DP水题)
查看>>
NEFU 506&&ZOJ 3353 Chess Board (四种构造的高斯消元)
查看>>
JS正则表达式验证数字
查看>>