2300 years ago, Moriya Suwako was defeated by Yasaka Kanako in the Great Suwa War. As the goddess of mountains in Gensokyo, she was planning to make a comeback and regain faith among humans.
To achieve her great plan, she decides to build shrines in human villages. Of course, each village can bulid at most one shrine. If she builds a shrine in the i-th village, she can get Fifaith points.
Because of geological differences and the Quantum Entanglement theory, each village has one and only one "entangled" village Pi (it is a kind of one-way relationship). If Suwakobuilds shrines both in the i-th village and the Pi-th village, the faith she get from the i-th village will changes by Gi points (it can result to a negative value of faith points). If she only builds a shrine in the Pi-th village, but not in the i-th village, the faith she get will not changes.
Now, please help Suwako to find out the maximal faith points she can get.
Input
There are multiple test cases. For each test case:
The first line contains an integer N (2 <= N <= 100000) indicates the number of villages in Gensokyo. Then followed by N lines, each line contains three integers Fi (0 <= Fi <= 100000) Gi (-100000 <= Gi <= 100000) Pi (1 <= Pi <= N and Pi will not point to the i-th village itself).
Output
For each test case, output the maximal faith points that Suwako can get.
Sample Input
2 3 -1 2 2 -2 1 4 3 -2 2 4 -3 3 2 -1 1 5 -2 2
Sample Output
3 9
Author: JIANG, Kai
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=100000+100;
int n,m,edge_cnt;
int a[MAXN],g[MAXN],in[MAXN],head[MAXN],vis[MAXN];
ll dp[MAXN][2],dp1[MAXN][2];
struct Edge
{
int v;
int next;
}edge[MAXN];
void init()
{
edge_cnt=0;
memset(in,0,sizeof(in));
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
edge[edge_cnt].v=v;
edge[edge_cnt].next=head[u];
head[u]=edge_cnt++;
}
ll dfs(int st,int cur,ll dp[][2],int flag)
{
int u=cur,v=edge[head[u]].v;
while(v!=st)
{
dp[v][0]+=max(dp[u][0],dp[u][1]);
dp[v][1]+=max(dp[u][0],dp[u][1]+g[u]);
u=v;
v=edge[head[u]].v;
}
if(flag)
dp[u][1]+=g[u];
return max(dp[u][0],dp[u][1]);
}
ll get_ans(int u)
{
int v=edge[head[u]].v;
dp1[v][0]+=dp[u][0];
dp1[v][1]+=dp[u][0];// u不取
ll ans1=dfs(u,v,dp1,0);
dp[v][0]+=dp[u][1];
dp[v][1]+=dp[u][1]+g[u];
ll ans2=dfs(u,v,dp,1);
return max(ans1,ans2);
}
int main()
{
//freopen("text.txt","r",stdin);
while(~scanf("%d",&n))
{
init();
for(int i=1;i<=n;i++)
{
int j;
scanf("%d%d%d",&a[i],&g[i],&j);
addedge(i,j); in[j]++;
}
queue<int>Q;
for(int i=1;i<=n;i++)
{
dp[i][1]=a[i];
if(!in[i])
Q.push(i);
}
while(!Q.empty())
{
int u=Q.front(); Q.pop();
vis[u]=1;
int v=edge[head[u]].v;
dp[v][0]+=max(dp[u][0],dp[u][1]);
dp[v][1]+=max(dp[u][1]+g[u],dp[u][0]);
if(!(--in[v]))
Q.push(v);
}
memcpy(dp1,dp,sizeof(dp));
ll sum=0;
for(int i=1;i<=n;i++)
if(!vis[i])
{
sum+=get_ans(i);
vis[i]=1;
for(int j=edge[head[i]].v;j!=i;j=edge[head[j]].v)
vis[j]=1;
}
printf("%lld\n",sum);
}
return 0;
}