HYSBZ 1040 Knight (base ring outward tree DP)

Source: Internet
Author: User

The Knights of Z are a powerful organization that brings together elites from all over the world. They maxi, punish evil and raise good, and are
The praise of the world. A terrible thing has happened recently, and the evil Y countries have launched a war of aggression against Z-states. 500 miles of war, in a peaceful environment
How can the hundreds of-year-old Z-nation withstand the armies of the nations of y? So people pinned all their hopes on the Knights, as if expecting a
The birth of a true dragon, leading Justice to defeat evil. The Knights are certainly capable of defeating the forces of evil, but the Knights often have a
Some contradictions. Every knight has and only one of his own most hated Knight (of course not himself), he is absolutely not with his most dislike of the people together
The. War stretches, the people misery, organized a Knight Corps to join the Battle of urgency! The king gave you a Herculean task, from all
Knight of the Knights, so that there are no contradictions within the regiment (there is no knight and his most hated to be elected to the Knight Corps
situation), and make the Knight Corps the most combat-fighting force. To describe the combat effectiveness, we numbered the Knights by 1 to N, giving each knight a battle
The battle force estimates that a legion's combat effectiveness is the sum of all knights ' combat effectiveness.

Input

The first line contains a positive integer n, which describes the number of knights. Next n rows, two positive integers per line, in order to describe the combat effectiveness of each knight
And the knight he hated the most.

Output

Should contain a line containing an integer representing the combat effectiveness of your chosen Knight Corps.

Sample Input310 1

Sample Outputof Hint

N≤1 000 000, each knight's combat capacity is not greater than 1 000 000 positive integers.

Main topic:

Give you the Battle of N Knights and the knight he hates (with and without himself). To organize a legion that makes the Legion the strongest, and that no one in the Legion is hated by another. The Battle of the Legion is extremely effective.

See the topic, think of the previous to do pick apples (without the boss of the party). Apples of neighboring levels cannot be picked at the same time.

The DP expression is: Dp[i][0]=∑max (dp[son][0],dp[son][1]); dp[i][1]=∑dp[son][0];

But the problem is that there may be rings (at least 3 elements on the ring).

If there is a ring, delete one of the edges in the ring, make a DP from two points on the side, and the final answer is Max (Dp[st][0],dp[en][0]), which means that two points cannot be taken at the same time.

The problem may have multiple connected components, and not all connected components must have a ring (there is a knight that hates each other) (which causes an undirected edge to be stored two times, to delete one), which should be noted in the code.

The answer to LOL, chain forward to open the double array of stars!

#include <cstdio>#include<queue>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=1000000;intfight[maxn+5];intto[maxn*2+5];intnex[maxn*2+5];inthead[maxn+5];intCNT;voidAddedge (intUintv) {to[cnt]=v; NEX[CNT]=Head[u]; Head[u]=cnt++;}BOOLIsedge (intUintv) {     for(inti=head[u];i!=-1; i=Nex[i]) {        if(to[i]==v)return true; }    return false;}intvis1[maxn+5];intSt,en;voidDFS1 (intXintFA) {Vis1[x]=1;  for(inti=head[x];i!=-1; i=Nex[i]) {        if(to[i]!=FA) {            if(!Vis1[to[i]) DFS1 (to[i],x); Else{St=x; En=To[i]; }        }    }}Long Longdp[maxn+5][2];intvis2[maxn+5];voidDFS2 (intx) {Vis2[x]=1; BOOLflag=false;  for(inti=head[x];i!=-1; i=Nex[i]) {        if(!vis2[to[i]]&&! ((x==st&&to[i]==en) | | (x==en&&to[i]==St)))            {DFS2 (to[i]); Flag=true; dp[x][1]+=dp[to[i]][0]; dp[x][0]+=max (dp[to[i]][0],dp[to[i]][1]); }} dp[x][1]+=1ll*Fight[x]; if(!flag) dp[x][1]=1ll*fight[x];}intMain () {intN;  while(SCANF ("%d", &n)! =EOF) {memset (head,-1,sizeof(head)); CNT=0;  for(intI=1, a,b;i<=n;i++) {scanf ("%d%d",&a,&b); Fight[i]=A; if(!Isedge (i,b))                {Addedge (i,b);            Addedge (B,i); }        }        Long Longans=0; memset (Vis1,0,sizeof(VIS1));  for(intI=1; i<=n;i++)        {            if(!Vis1[i]) {St=-1, en=-1; DFS1 (i,-1); //printf ("%d%d\n", st,en);                if(st==-1&&en==-1) {memset (Vis2,0,sizeof(VIS2)); Memset (DP,0,sizeof(DP));                    DFS2 (i); Ans+=max (dp[i][0],dp[i][1]); }                Else{memset (Vis2,0,sizeof(VIS2)); Memset (DP,0,sizeof(DP));                    DFS2 (ST); Long Longtemp=dp[st][0]; memset (Vis2,0,sizeof(VIS2)); Memset (DP,0,sizeof(DP));                    DFS2 (en); //printf ("%d%d\n", temp,dp[en][0]);Ans+=max (temp,dp[en][0]); }}} printf ("%lld\n", ans); }    return 0;}
View Code

HYSBZ 1040 Knight (base ring outward tree DP)

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.