。。。

st#include<cstdio>

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6+;
typedef long long ll;
struct node {
int to,next;
} e[*maxn]; //以这种方式建图,就不需要建树。
int head[maxn]; //可以随意取任何一个节点作为根节点,因为本来就是无向图
int ans = ,cut,root,_root;
int n;
int w[maxn],vis[maxn];
ll dp[maxn][];
//这种建图方式很好
void add(int u,int v) { //无向图,两边都要加
e[ans].to = v; //记录当前第ans根线的to
e[ans].next=head[u]; //指向head[u] 之前指向的那根线。
head[u]=ans++; //指向当前线
}
void dfs(int u,int fa){ //寻找环 ,顺便,标记vis
vis[u] = ;
for(int i=head[u];i!=-;i=e[i].next){
int v = e[i].to;
if(v == fa) continue;
if(!vis[v]) dfs(v,u);
else {root = v;_root=u;cut=i;} //标记要待会作为根节点的root和
} //_root 以及拆掉的线i
} void tree_dp(int u,int fa){
dp[u][] = w[u];
dp[u][] = ;
for(int i=head[u];i!=-;i=e[i].next){
int temp = e[i].to;
if(i == cut || i == (cut^) || temp == fa) continue; //优先级的问题 如果当前线等于拆掉的线return
tree_dp(temp,u);
dp[u][] += dp[temp][];
dp[u][] += max(dp[temp][],dp[temp][]);
}
} int main() {
while(~scanf("%d",&n)) {
ans = ;
memset(w,,sizeof(w));
memset(vis,,sizeof(vis));
memset(dp,,sizeof(dp));
memset(head,-,sizeof(head)); for(int i=; i<=n; i++) {
int u;
scanf("%d %d",&w[i],&u);
add(i,u);
add(u,i);
} ll sum =;
for(int i=;i<=n;i++){
if(vis[i]) continue;
dfs(i,-);
// printf("%d %d %d\n",root,_root,cut);
tree_dp(root,-);//????(将-1改为_root就会wa)
ll temp = dp[root][];
tree_dp(_root,-);///???为什么-1就ac,0就wa
sum+=max(temp,dp[_root][]);
}
printf("%lld\n",sum);
}
return ;
}

wa:

#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
const int maxn=1e6+;
inline LL max(LL a,LL b){return (a<b?b:a);}
int first[maxn],next[maxn*],to[maxn*];
int edge_count=-;//!!!!重点错误::因为每次两个边是连在一起的所以应该(0,1),(2,3)而不是(1,2),(2,3)【此处指的是边编号】
inline void add(int x,int y){
edge_count++;
to[edge_count]=y;
next[edge_count]=first[x];
first[x]=edge_count;
}
int n,a[maxn];
inline void read(int &a){
a=;int b=;char x=getchar();
while(x<'' || ''<x){
if(x=='-')b=-;
x=getchar();
}
while(''<=x && x<=''){
a=(a<<)+(a<<)+x-'';
x=getchar();
}
a*=b;
}
bool vis[maxn];
int x[maxn],y[maxn],cnt[maxn],p;
void dfs(int root,int fa){ vis[root]=; for(int i=first[root];i!=-;i=next[i]){
if(to[i]==fa)continue;
if(vis [ to[i] ]){
x[p]=root;
y[p]=to[i];
cnt[p]=i;
}
else dfs(to[i],root);
}
}
LL f[maxn][],ans;
//1->选了 0->没选
void search(int root,int fa,int ban){ // printf("%d %d %d\n",root,fa,ban);
f[root][]=(LL)a[root];
f[root][]=0ll; for(int i=first[root];i!=-;i=next[i]){
if(to[i]==fa || i==ban || i==(ban^) )continue;
search(to[i],root,ban);
f[root][]+=f[ to[i] ][];
f[root][]+=max(f[ to[i] ][],f[ to[i] ][]);
} }
int main()
{
//freopen("knight.in","r",stdin);
read(n);
memset(first,-,sizeof(first));
for(int i=,en;i<=n;i++){
read(a[i]);read(en);
add(i,en);
add(en,i);
}
for(int i=;i<=n;i++){
if(!vis[i]){
dfs(i,-);
// printf("%d %d\n",x[p],y[p]);
p++;
}
}
//memset(vis,0,sizeof(vis)); for(int i=;i<p;i++){
LL temp=0ll; //memset(vis,0,sizeof(vis));
search(x[i],0,cnt[i]);
//(之前写的是:search(x[i],y[i],cnt[i]));也是wa
temp=f[ x[i] ][]; // memset(vis,0,sizeof(vis));
search(y[i],0,cnt[i]);
//(只有写成search(y[i],-1,cnt[i]);才能对)
ans+=max(temp,f[ y[i] ][]);
} printf("%lld",ans);
return ;
}

bzoj1040基环树的更多相关文章

  1. bzoj1040 基环树森林dp

    https://www.lydsy.com/JudgeOnline/problem.php?id=1040 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社 ...

  2. [bzoj1040][ZJOI2008]骑士_树形dp_基环树_并查集

    骑士 bzoj-1040 ZJOI-2008 题目大意:n个骑士,每个骑士有权值val和一个讨厌的骑士.如果一个骑士讨厌另一个骑士那么他们将不会一起出战.问出战的骑士最大atk是多少. 注释:$1\l ...

  3. bzoj1040(ZJOI2008)骑士——基环树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1040 基环树的模板. 套路就是把环断开,先把一端作为根节点,强制不选:再把另一端作为根节点, ...

  4. 【bzoj1040】[ZJOI2008]骑士 并查集+基环树dp

    题目描述 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里,在 ...

  5. BZOJ1040:骑士(基环树DP)

    Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里,在和平环境中 ...

  6. [BZOJ1040][ZJOI2008]骑士 基环树DP

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1040 题目给出了$n$个点和$n$条无向边,即一棵基环树或者基环树森林. 如果题目给的关系 ...

  7. 基环树DP

    基环树DP Page1:问题 啥是基环树?就是在一棵树上增加一条边. Page2:基环树的几种情况 无向 有向:基环外向树,基环内向树. Page3:处理问题的基本方式 1.断环成树 2.分别处理树和 ...

  8. 【BZOJ1791】【IOI2008】【基环树】island(status第一速度)

      1791: [Ioi2008]Island 岛屿  Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 908  Solved: 159 [Su ...

  9. 『Island 基环树直径』

    Island(IOI 2008) Description 你准备浏览一个公园,该公园由 N 个岛屿组成,当地管理部门从每个岛屿 i 出发向另外一个岛屿建了一座长度为 L_i 的桥,不过桥是可以双向行走 ...

随机推荐

  1. bis和bic命令实现或和异或运算

    从20世纪70年代末到80年代末,Digital Equipment的VAX计算机是一种非常流行的机型.它没有布尔运算AND和OR指令,只有bis(位设置)和bic(位清除)这两种指令.两种指令的输入 ...

  2. ML.NET is an open source and cross-platform machine learning framework

    https://www.microsoft.com/net/learn/apps/machine-learning-and-ai/ml-dotnet Machine Learning made for ...

  3. P1742 最小圆覆盖(计算几何)

    体验过\(O(n^3)\)过\(10^5\)吗?快来体验一波当\(wys\)的快感吧\(QAQ\) 前置芝士1:二元一次方程组求解 设 \[\begin{cases}a1 * x + b1*y=c1\ ...

  4. selenium家族发展史

    什么是Selenium? Selenium 是专门为Web应用程序编写的一个验收测试工具.Selenium测试直接运行在浏览器中,支持的浏览器包括IE(7.8.9).Mozilla Firefox.M ...

  5. Qt调用自己编译的libglog.a出现问题

    我确定依据正确导入库后,依旧出现未定义的引用. undefined reference to _imp___ZN6google17InitGoogleLoggingEPKc 尝试过重新编译,调整编译参 ...

  6. 美化博客CSS

    title: 美化博客CSS date: 2019/01/19 14:28:59 --- 美化博客CSS 可以去这里看下好看的样式 修改下文档的css,博客园是在页面定制CSS代码,我这里修改了下标题 ...

  7. 目前的.NET(C#)世界里,主流的ORM框架

    推荐一些常用的asp.net ORM框架 SqlSugar (国内) Dos.ORM (国内) Chloe (国内) StackExchange/Dapper (国外) Entity Framewor ...

  8. [物理学与PDEs]第1章第9节 Darwin 模型 9.3 Darwin 模型

    1. $\Omega$ 中 ${\bf A}={\bf A}_T+{\bf A}_L$, 其中 $\Div{\bf A}_T=0$, $\rot{\bf A}_L={\bf 0}$. 若 $$\bex ...

  9. CemtOS7更改yum网易 阿里云的yum源。

    一,鉴于用国外的Yum源,速度比较慢,所以想到将国外的yum源,改为国内的Yum源,著名的有网易 阿里云源.如何更改呢? 二,更改yum源为网易的. 首先备份/etc/yum.repos.d/Cent ...

  10. webpack配置使用gif动图

    1. 下载npm包: npm install url-loader --save-dev 2. webpack.config.js中module  -> rules里添加: { test: /\ ...