#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 ;
} /*
*/

最新文章

  1. 一键生成APP官网
  2. sql2008“备份集中的数据库备份与现有的xxx数据库不同”解决方法
  3. 7Z命令行详解
  4. 第六天 做的app不会改变什么
  5. 每日一九度之 题目1076:N的阶乘
  6. Drupal常用开发工具(一)——Devel模块
  7. hdu 5720 BestCoder 2nd Anniversary Wool 推理+一维区间的并
  8. [原]生产环境下的nginx.conf配置文件(多虚拟主机)
  9. 关于http客户端常见错误&quot;警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec&quot;
  10. csu 10月 月赛 D 题 CX and girls
  11. hdoj 2085 核反应堆【水】
  12. Flocker 做为后端存储代理 docker volume-driver 支持
  13. 如何调节Eclipse下console输出字体的大小??
  14. Linux(CentOS7)下如何配置多个Tomcat容器
  15. pdf.js插件使用记录,在线打开pdf
  16. browser-sync events.js:85 throw er; // Unhandled &#39;error&#39; event
  17. VKD224B触摸芯片调试笔记
  18. Linux下,如何查看磁盘是否包含数据
  19. win8 下 TortoiseSVN 不显示图标
  20. 如何使用Android Studio把自己的Android library分享到jCenter和Maven Central

热门文章

  1. UVA 1213 Sum of Different Primes
  2. Codeforces 713C Sonya and Problem Wihtout a Legend DP
  3. python实现备份gitlab版本库并更改文件名
  4. Mantis 从Windows 迁移到Linux上
  5. Windows下端口占用查看
  6. MyBatis 系列五 之 关联映射
  7. 双关键字LIS
  8. python模块(requests,logging)
  9. python自动开发之第二十四天(Django)
  10. nvidia tk1使用记录--基本环境搭建