"Bzoj 1040" [ZJOI2008] Knight

Source: Internet
Author: User

1040: [ZJOI2008] Knight time limit: ten Sec Memory Limit: 162 MB
Submit: 1976 Solved: 772
[Submit] [Status] 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.


DP of the base ring tree forest.


Because the graph of the problem has n points, n edges, so think of the base ring tree, but the problem is not said to be connected, so is a base ring tree forest.


Then each time you find the connecting block, then the DP can be.


The DP should break off a chain on the ring, and then compute the root of the disconnected two endpoint root and _root respectively.


Root is rooted, the root must not take, _root may not take. _root is the same as root.


DP equation: f[i][1] Table I take the time with I as the root of the subtree maximum combat effectiveness; f[i][0] means I do not take.

F[i][1]=a[i]+sigma (F[j][0])

F[i][0]=sigma (Max (f[j][0],f[j][1]))

(J for son of I)


#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < cstdlib> #include <cmath> #define LL long long#define M 1000005using namespace std; LL f[m][2],a[m];int n,v[m],h[m],root,_root,tot=1,cant;struct edge{int Y,ne;} E[m*2];void addedge (int x,int y) {tot++;e[tot].y=y;e[tot].ne=h[x];h[x]=tot;} void Dfs (int x,int fa) {v[x]=1;for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (Y==FA) continue;if (!v[y]) DFS (y,x); else{ cant=i;root=x;_root=y;}}} LL Maxx (ll A,ll b) {if (a>b) return A;return B;} void dp (int x,int FA) {f[x][0]=0,f[x][1]=a[x];for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (y==fa| | i==cant| | i== (cant^1)) CONTINUE;DP (y,x); F[x][0]+=maxx (f[y][0],f[y][1]); f[x][1]+=f[y][0];}} int main () {scanf ("%d", &n), for (int i=1;i<=n;i++) {int x;scanf ("%lld%d", &a[i],&x); Addedge (X,i); Addedge (i,x);} LL ans=0;for (int i=1;i<=n;i++) if (!v[i]) {DFS (i,-1);dp(root,-1); LL temp=f[root][0];dp(_root,-1); Temp=maxx (temp,f[_root][0]); ans+=temp;} COut<<ans<<endl;return 0;} 



Sentiment:

1. Because this problem and disconnect which side is irrelevant, so there is no need to calculate the ring on each point of the subtree, and then to calculate the ring

The diameter of the base ring tree needs to be enumerated to break which edge to seek the diameter, the time complexity is O (n^2), will time out

"Bzoj 1040" [ZJOI2008] Knight

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.