Test instructions: Given a forest. A light can be installed on each node to cover all the edges connected to that node. Select the fewest number of nodes num overwrites all the edges. Where num is the smallest, is it reasonable to place num lights so that the two lights cover the most edges?
Idea: F[i][0] representative did not put the lights, F[i][1] representative put the lights, G[i] similar.
1#include <algorithm>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <iostream>6 intf[1005][2],g[1005][2];7 inttot,go[200005],first[200005],next[200005];8 intn,m;9 intRead () {Ten CharCh=getchar ();intt=0, f=1; One while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} A while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} - returnt*F; - } the voidInsertintXinty) { -tot++; -go[tot]=y; -next[tot]=First[x]; +first[x]=tot; - } + voidAddintXinty) { A Insert (x, y); insert (y,x); at } - voidDfsintXintFA) { -f[x][0]=0; g[x][0]=0; -f[x][1]=1; g[x][1]=0; - for(intI=first[x];i;i=Next[i]) { - intPur=Go[i]; in if(PUR==FA)Continue; - DFS (pur,x); to if(f[pur][0]<f[pur][1]|| (f[pur][0]==f[pur][1]&&g[pur][0]>g[pur][1]+1)){ +f[x][1]+=f[pur][0]; -g[x][1]+=g[pur][0]; the}Else{ *f[x][1]+=f[pur][1]; $g[x][1]+=g[pur][1]+1;Panax Notoginseng } -f[x][0]+=f[pur][1]; theg[x][0]+=g[pur][1]; + } A } the intMain () { + intT; -scanf"%d",&T); $ for(intTcase=1; tcase<=t; tcase++){ $N=read (); m=read (); -tot=0; - for(intI=1; i<=n;i++) first[i]=g[i][0]=g[i][1]=0, f[i][0]=f[i][1]=-1; the for(intI=1; i<=m;i++){ - intX=read (), y=read ();Wuyix++;y++; the Add (x, y); - } Wu intans1=0, ans2=0; - for(intI=1; i<=n;i++) About if(f[i][0]==-1&&f[i][1]==-1){ $DFS (I,0); - if(f[i][0]<f[i][1]|| (f[i][0]==f[i][1]&&g[i][0]>g[i][1])){ -ans1+=f[i][0]; -ans2+=g[i][0]; A}Else{ +ans1+=f[i][1]; theans2+=g[i][1]; - } $ } theprintf"Case %d:%d%d%d\n", tcase,ans1,ans2,m-ans2); the } the}
Lightoj 1230 Placing lampposts (tree-shaped DP)