Topic links
1040: [ZJOI2008] Knight time limit:10 Sec Memory limit:162 MB
submit:3054 solved:1162
[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 Input3
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.
According to the topic can be known, each of the Unicom block has and only one ring, so we find this ring and then disconnect it from the two endpoints U1, U2, respectively Dfs. Set dp[u][0] to not select U, dp[u][1] to select U, then the answer is Max (dp[u1][0], dp[u2][0]). Notice there are many unicom block, at first did not think of Mad WA.
#include <iostream>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<Set>#include<string>#include<queue>#include<stack>#include<bitset>using namespacestd;#definePB (x) push_back (x)#definell Long Long#defineMK (x, y) make_pair (x, y)#defineLson L, M, rt<<1#defineMem (a) memset (a, 0, sizeof (a))#defineRson m+1, R, rt<<1|1#defineMem1 (a) memset (a,-1, sizeof (a))#defineMEM2 (a) memset (a, 0x3f, sizeof (a))#defineRep (i, N, a) for (int i = A; i<n; i++)#defineFi first#defineSe Secondtypedef pair<int,int>PLL;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-8;Const intMoD = 1e9+7;Const intINF =1061109567;Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} };Const intMAXN = 1e6+5;inthead[maxn*2], num, U1, U2, A[maxn], VIS[MAXN], e;ll dp[maxn][2];structnode{intto, Nextt;} E[MAXN*2];voidAddintUintv) {e[num].to= V, e[num].nextt = Head[u], head[u] = num++;}voidinit () {num=0; Mem1 (head);}voidGet_circle (intUint from) {Vis[u]=1; for(inti = Head[u]; ~i; i =e[i].nextt) { intv =e[i].to; if((i^1) == from) Continue; if(Vis[v]) {u1= u, U2 =v; E=i; Continue ; } get_circle (V, i); }}voidDfsintUint from) {dp[u][1] =A[u]; dp[u][0] =0; for(inti = Head[u]; ~i; i =e[i].nextt) { intv =e[i].to; if((i^1) == from) Continue; if(i = = E | | (i^1)==E)Continue; DFS (v, i); dp[u][1] + = dp[v][0]; dp[u][0] + = max (dp[v][1], dp[v][0]); }}intMain () {intN, x; CIN>>N; Init (); for(inti =1; i<=n; i++) {scanf ("%d%d", &a[i], &x); Add (i, x); Add (x, I); } ll ans=0; for(inti =1; i<=n; i++) { if(Vis[i])Continue; Get_circle (i,-1); DFS (U1,-1); LL TMP= dp[u1][0]; DFS (U2,-1); TMP= MAX (tmp, dp[u2][0]); Ans+=tmp; } cout<<ans<<Endl; return 0;}
Bzoj 1040: [ZJOI2008] Knight tree DP