[ZJOI2008] Knight

Description

The Knights of Country Z is a powerful organization that gathers elites from all over the world. They rob the rich to help the poor, punish evil and promote good, and are praised by all walks of life.

A terrible thing happened recently, the evil country Y launched a war of aggression against country Z. The war lasted for 500 miles, and how could country Z, which had been at ease in a peaceful environment for hundreds of years, resist the army of country Y. So people put all their hopes on the knights, just like expecting the birth of a real dragon and emperor to lead justice to defeat evil.

The knights certainly have the ability to defeat evil forces, but the knights often have some contradictions with each other. Every knight has one and only one knight he hates the most (of course not himself), and he will never go out with the person he hates the most.

The flames of war are lingering, and the people's lives are ruined. It is urgent to organize a knight army to join the battle! The king has given you a difficult task to select a knight corps from all the knights, so that there are no conflicting two people in the corps (there is no situation where a knight is elected to the knight corps with the person he hates the most), And, making this knight army the most combat-effective.

To describe the combat power, we number the knights from 1 to N, and give each knight an estimate of the combat power. The combat power of a legion is the sum of the combat power of all the knights.

Input

The first line contains a positive integer N, describing the number of knights.

The next N lines, each with two positive integers, describe each knight's combat power and the knight he hates the most in order.

Output

Should contain a line containing an integer representing the combat power of the knight army you selected.

Range

For 30% of the test data, N ≤ 10;

For 60% of the test data, N ≤ 100;

For 80% of the test data, N ≤ 10 000 is satisfied.

For 100% of the test data, satisfying N ≤ 1 000 000, the combat power of each knight is a positive integer not greater than 1 000 000.

Solution

It is observed that a base ring tree forest is formed after connecting the edges according to the meaning of the question.

For this type of base ring tree problem, the general solution is as follows:

Find a unique ring within a connected block

Randomly delete an edge on the ring and record the two endpoints $first_root$ $second_root$

Take the two endpoints as the root to make a tree DP

take the maximum value

Code

 

#include<cstdio>
#include<cstring>
#include<iostream>
#define N 1000005
#define int long long

int val[N];
int n,cnt=1;
bool vis[N];
int head[N];
int f[N],g[N];
int first_root,second_root,deleedge;

struct Edge{
  int to,nxt;
}edge[N<<1];

void add(int x,int y){
  edge[++cnt].to=y;
  edge[cnt].nxt=head[x];
  head[x]=cnt;
}

void dfs(int now,int in_edge){
  vis[now]=1;
  for(int i=head[now];i;i=edge[i].nxt){
    int to=edge[i].to;
    if(!vis[to]) dfs(to,i);
    else if((i^1)!=in_edge){
      first_root=now;
      second_root=to;
      deleedge=i;
    }
  }
}

void dp(int now,int in_edge){
  f[now]=val[now];
  g[now]=0;
  for(int i=head[now];i;i=edge[i].nxt){
    int to=edge[i].to;
    if(i==deleedge||(i^1)==deleedge) continue;
    if(i==in_edge||(i^1)==in_edge) continue;
    dp(to,i);
    g[now]+=std::max(g[to],f[to]);
    f[now]+=g[to];
    //printf("now=%lld,to=%lld,g=%lld,f=%lld\n",now,to,g[now],f[now]);
  }
}

signed main(){
  scanf("%lld",&n);
  for(int x,i=1;i<=n;i++){
    scanf("%lld%lld",&val[i],&x);
    add(x,i); add(i,x);
  }
  int ans=0;
  for(int i=1;i<=n;i++){
    if(vis[i]) continue;
    dfs(i,0);
    /*memset(f,0,sizeof f);
    memset(g,0,sizeof g);*/
    dp(first_root,0);
    int maxn=-12345678901234567;
    maxn=std::max(maxn,g[first_root]);
    /*memset(f,0,sizeof f);
    memset(g,0,sizeof g);*/
    dp(second_root,0);
    //printf("frist=%lld,second=%lld\n",first_root,second_root); 
    maxn=std::max(maxn,g[second_root]);
    years += maxn;
  }
  printf("%lld\n",ans);
  return 0;
}

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325076580&siteId=291194637