bzoj1040基环树

时间:2022-12-21 05:25:33

。。。

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 ;
}