Bzoj 1040 Knight

Source: Internet
Author: User

Title Link: http://61.187.179.132/JudgeOnline/problem.php?id=1040

Test instructions: Give a graph with only one ring. Each point has a weighted value. Select some points 22 is not adjacent, so that the weight of the largest?

There is only one ring, then we first build, then there is only one non-tree edge, non-tree edge on the two points we set as node and root, then we DP on both sides, first let node can not select, and then let the root can not be selected, DP two times can, and the ring edge can not go.

1#include <algorithm>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <iostream>6 #definell Long Long7 inttot,go[2000005],next[2000005],first[1000005],b[2000005];8 intvis[1000005],root,tmp,node,flag,n;9ll f[1000005][2],w[1000005];Ten voidInsertintXintYintID) { Onetot++; Ago[tot]=y; -next[tot]=First[x]; -first[x]=tot; theb[tot]=ID; - } - voidAddintXintYintID) { - Insert (x,y,id); + Insert (y,x,id); - } + voidDfsintXintFA) { Avis[x]=1; at      for(intI=first[x];i;i=Next[i]) { -         intPur=Go[i]; -         if(PUR==FA)Continue; -         if(Vis[pur]) { -root=pur; -Node=x; intmp=B[i]; -             Continue; to         } + DFS (pur,x); -     } the } * voiddpintXintFA) { $f[x][0]=0; f[x][1]=0;Panax Notoginseng      for(intI=first[x];i;i=Next[i]) { -         intPur=Go[i]; the         if(b[i]==tmp| | PUR==FA)Continue; + DP (PUR,X); Af[x][0]+=std::max (f[pur][0],f[pur][1]); thef[x][1]+=f[pur][0]; +     } -f[x][1]+=W[x]; $     if(flag==0&&x==root) f[x][1]=0; $     if(flag==1&&x==node) f[x][1]=0; - } - intMain () { thescanf"%d",&n); -     intId=0;Wuyi      for(intI=1; i<=n;i++){ the         intx; -scanf"%lld%d",&w[i],&x); Wuid++; - Add (i,x,id); About     } $ll ans=0; -ll ans=0; -      for(intI=1; i<=n;i++) -      if(!Vis[i]) { Anode=tmp=0; +DFS (I,0); theflag=1; -DP (Root,0); $Ans=std::max (f[root][0],f[root][1]); theflag=0; theDP (Root,0); theAns=std::max (Ans,std::max (f[root][0],f[root][1])); theans+=Ans; -     } inprintf"%lld", ans); the}

Bzoj 1040 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.