bzoj 1040 基向内环树dp
2024-10-21 06:46:11
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg using namespace std; const int N = 1e6 + ;
const int M = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +; int n, tot, a[N], fa[N], b[N], head[N];
bool vis[N];
LL f[N][]; struct Edge {
int to, nx;
} edge[N << ]; void add(int u, int v) {
edge[tot].to = v;
edge[tot].nx = head[u];
head[u] = tot++;
} int getRoot(int x) {
return fa[x] == x ? x : fa[x] = getRoot(fa[x]);
} void dp(int u, int fa, int ban) {
f[u][] = , f[u][] = a[u];
LL tmp[] = {}; for(int i = head[u]; ~i; i = edge[i].nx) {
int v = edge[i].to;
if(v == fa || i == ban || (i ^ ) == ban) continue;
dp(v, u, ban); tmp[] = f[u][], tmp[] = f[u][]; tmp[] = max(tmp[], f[u][] + f[v][]);
tmp[] = max(tmp[], f[u][] + f[v][]);
tmp[] = max(tmp[], f[u][] + f[v][]); f[u][] = tmp[], f[u][] = tmp[];
}
} int main() {
memset(head, -, sizeof(head)); scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d%d", &a[i], &b[i]);
add(i, b[i]); add(b[i], i);
fa[i] = i;
} LL ans = ;
for(int i = ; i <= n; i++) {
int x = getRoot(i);
int y = getRoot(b[i]);
if(x != y) {
fa[x] = y;
} else {
LL ret = ;
dp(i, , i * - );
ret = max(ret, f[i][]);
dp(b[i], , i * - );
ret = max(ret, f[b[i]][]);
ans += ret;
}
} printf("%lld\n", ans);
return ;
} /*
*/
最新文章
- 一键生成APP官网
- sql2008“备份集中的数据库备份与现有的xxx数据库不同”解决方法
- 7Z命令行详解
- 第六天 做的app不会改变什么
- 每日一九度之 题目1076:N的阶乘
- Drupal常用开发工具(一)——Devel模块
- hdu 5720 BestCoder 2nd Anniversary Wool 推理+一维区间的并
- [原]生产环境下的nginx.conf配置文件(多虚拟主机)
- 关于http客户端常见错误";警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec";
- csu 10月 月赛 D 题 CX and girls
- hdoj 2085 核反应堆【水】
- Flocker 做为后端存储代理 docker volume-driver 支持
- 如何调节Eclipse下console输出字体的大小??
- Linux(CentOS7)下如何配置多个Tomcat容器
- pdf.js插件使用记录,在线打开pdf
- browser-sync events.js:85 throw er; // Unhandled &#39;error&#39; event
- VKD224B触摸芯片调试笔记
- Linux下,如何查看磁盘是否包含数据
- win8 下 TortoiseSVN 不显示图标
- 如何使用Android Studio把自己的Android library分享到jCenter和Maven Central