This is a forest, each tree has a ring ... Each tree is handled separately, find the ring on either side of the disconnection, limit the end of this side of the situation, and then you can tree DP.
------------------------------------------------------------------------
#include <cstdio>#include <algorithm>#include <cstring>#include <cctype>using namespace std;int Read () {char C = getchar ();int ret = 0;For (;!isdigit (c); c = GetChar ());For (; IsDigit (c); c = GetChar ()) ret = RET * + C-' 0 ';return ret;}typedef long Long ll; const int MAXN = 1000009;struct Edge {int to;edge* Next;} E[MAXN << 1], *pt = E, *head[maxn], *a, *b;void Add (int u, int v) {pt->to = v; pt->next = head[u]; Head[u] = pt++;}void Addedge (int u, int v) {Add (U, v); Add (v, u);}edge* Rev (edge* e) {return E + ((e-e) ^ 1);}ll dp[maxn][2];int N, W[MAXN], x, y;bool VIS[MAXN];void Dfs (int x, edge* r) {if (Vis[x]) {a = r, B = Rev (r);return;}vis[x] = true;For (edge* e = head[x]; e; e = e->next)if (E! = r) Dfs (E->to, rev (e));}void Dp (int x, edge* r) {dp[x][0] = 0; dp[x][1] = w[x];For (edge* e = head[x]; e; e = e->next) if (E! = R && E! = a && E! = b) {Dp (E->to, rev (e));dp[x][0] + = max (dp[e->to][0], dp[e->to][1]);dp[x][1] + = dp[e->to][0];}}ll work (int x) {dfs (x, NULL);Dp (A->to, NULL);LL ret = dp[a->to][0];Dp (B->to, NULL);return Max (ret, dp[b->to][0]);}int main () {N = read ();for (int i = 0; i < N; i++) {W[i] = read ();Addedge (Read ()-1, i);}memset (Vis, 0, sizeof vis);ll ans = 0;for (int i = 0; i < N; i++)if (!vis[i]) ans + = Work (i);printf ("%lld\n", ans);return 0;}
-----------------------------------------------------------------------
1040: [ZJOI2008] Knight Time Limit:Ten Sec Memory Limit:162 MB
Submit:2693 Solved:1029
[Submit] [Status] [Discuss] Description
The Knights of Z are a powerful organization that brings together elites from all over the world. They maxi and punish evil and Good, and are praised by all sectors of the community. A terrible thing has happened recently, and the evil Y countries have launched a war of aggression against Z-states. The war stretches for 500 miles, and the Z-nation, who has been comfortably in peace for hundreds of years, can withstand the forces of the nations of Y. So people pinned all their hopes on the Knights, as if expecting the birth of a true dragon, leading Justice to defeat evil. The Knights were certainly capable of defeating the forces of evil, but the Knights often had some contradictions with each other. Every knight has and only one of his own most hated Knight (not himself), he is absolutely not with his most dislike of the people to the expedition together. War stretches, the people misery, organized a Knight Corps to join the Battle of urgency! The king has given you a formidable task, electing a Knight Corps from all the knights, so that there are no contradictory members of the Legion (there is no knight in the case of the Knights with whom he hates the most), and that the Knight Corps is the most capable of fighting. In order to describe the combat effectiveness, we numbered the Knights according to 1 to N, giving each knight an estimate of the combat effectiveness of a legion that is the sum of the fighting power of all knights.
Input
The first line contains a positive integer n, which describes the number of knights. The next n lines, two positive integers per line, describe in order the combat effectiveness of each knight and the knight he hates most.
Output
Should contain a line containing an integer representing the combat effectiveness of your chosen Knight Corps.
Sample Input 3
10 2
20 3
1Sample Output -HINT
For 100% of the test data, meet N≤1 000 000, each knight's combat effectiveness is not greater than 1 000 000 positive integer.
Source
Bzoj 1040: [ZJOI2008] Knight (tree-shaped DP)