2300 years ago, Moriya Suwako was defeated by Yasaka Kanako in the great Suwa War. As the goddess of mountains in Gensokyo, she is 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 the most one shrine. If She builds a shrine i in the-th village, she can get the Fi faith points.
Because of geological differences and the Quantum entanglement theory, each village have one and only one "entangled" villa GE Pi (It is a kind of one-way relationship). If Suwakobuilds shrines both i Pi in the-th village and the-th village, the faith she get from the- i t H Village would changes by Gi points (it can result to a negative value of faith points). If She is builds a shrine in the Pi -th village, but is not in i the-th village, the faith she get would not changes.
Now, you can get Suwako to find out the maximal faith points she.
Input
There is multiple test cases. For each test case:
The first line contains a integer N (2 <= N <= 100000) indicates the number of villages in Gensokyo. Then followed N by lines, each line contains three integers Fi (0 <= Fi <= 100000) Gi ( -100000 <= <= 100000) (1 <= <= and would not point to the Pi Pi N Pi i -th village itself).
Output
For each test case, the output of the maximal faith points that Suwako can get.
Sample Input
23-1 22-2 143-2 24-3 32-1 15-2 2
Sample Output
39
Test instructions: In N villages choose to build holy sites, if in pi only built, will not lose satisfaction, if the son node in pi
Also build will lose g satisfaction, ask you to choose the maximum satisfaction degree
Analysis: I see with ordinary tree DP no difference, set dp[i][1]: For the first point of the maximum satisfaction, dp[i][0]
The maximum satisfaction is not built at the point I, but there is a ring, we have to analyze the characteristics of this ring, each village has only one "entangled" village
That is, when the edge (the direction) of the time each point has only one edge, if there is a ring, it is possible to start from the point on the ring to the point outside the ring
And there is no double ring. So we select a point on the ring, sorting and do not choose to do two times the tree DP, take the maximum value.
The direct topological ordering of points not on the ring is excluded.
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string > #include <iostream> #include <queue> #include <cmath> #include <map> #include <stack> #include <set>using namespace std; #define REPF (I, A, b) for (int i = A; I <= B; + + i) #define REP (i, N) for (int i = 0; i < n; + + i) #define CLEAR (A, X) memset (A, x, sizeof a) typedef long LONG ll;const LL INF = ( 1LL<<50); typedef pair<int,int>pil;const INT maxn=1e5+100;struct node{int to,next; int Val;} E[maxn];int N,tot; LL dp[maxn][2];int head[maxn],in[maxn];int vis[maxn],a[maxn];void addedge (int u,int v,int W) {e[tot].to=v;e[tot].next= Head[u]; e[tot].val=w;head[u]=tot++;} void Dfs (int u,int fa) {dp[u][0]=0; Dp[u][1]=a[u]; for (int i=head[u];i!=-1;i=e[i].next) {int to=e[i].to; if (TO!=FA) DFS (TO,FA); Dp[u][0]+=max (dp[to][0],dp[to][1]); Dp[u][1]+=max (dp[to][0],dp[to][1]+e[i].val); }}LL Solve (int u) {LL ans1=0,ans2=0; Dp[u][0]=0;dp[u][1]=-inf; for (int i=head[u];i!=-1;i=e[i].next) {int v=e[i].to; DFS (V,U); Ans1+=max (dp[v][0],dp[v][1]); } Dp[u][0]=-inf;dp[u][1]=a[u]; for (int i=head[u];i!=-1;i=e[i].next) {int v=e[i].to; DFS (V,U); Ans2+=max (Dp[v][0],dp[v][1]+e[i].val); } return Max (ANS1,ANS2);} bool OK (int u,int fa) {vis[u]=1; for (int i=head[u];i!=-1;i=e[i].next) {int to=e[i].to; if (TO==FA) return true; if (OK (TO,FA)) return true; } return false;} void Topsort () {queue<int>q; for (int i=1;i<=n;i++) if (!in[i]) Q.push (i); while (!q.empty ()) {int X=q.front (); Q.pop (); Vis[x]=1; for (int i=head[x];i!=-1;i=e[i].next) {int to=e[i].to; in[to]--; if (!in[to]) Q.push (to); }}}int Main () {int x,y,w; while (~scanf("%d", &n)) {CLEAR (head,-1); tot=0; CLEAR (vis,0); CLEAR (in,0); for (int i=1;i<=n;i++) {scanf ("%d%d%d", &a[i],&w,&y); Addedge (y,i,w); in[i]++; } topsort (); LL ans=0; for (int i=1;i<=n;i++) {if (!vis[i]&&ok (i,i)) ans+=solve (i); } printf ("%lld\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ZOJ 3527 shinryaku! Kero Musume (tree dp)