BZOJ1596[USACO2008 Jan] Telephone network
Test instructions
Select the smallest point in a tree to build a tower, so that each point has a tower or adjacent point. n≤10000.
Exercises
Greedy. DFS builds a tower at its father, for each current point, after Dfs finishes all its child nodes if it as well as its son and its father does not have a tower.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 #defineMAXN 100107 using namespacestd;8 9InlineintRead () {Ten CharCh=getchar ();intf=1, x=0; One while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} A while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); - returnf*x; - } the structe{intT,n;} es[2*MAXN];intESS,G[MAXN]; - voidPeintFintt) { -Es[++ess]= (e) {t,g[f]}; g[f]=ess; Es[++ess]= (e) {f,g[t]}; g[t]=ess; - } + intN,ans;BOOLCOV[MAXN]; - voidDfsintXintFA) { + BOOLf=0; A for(intI=G[X];I;I=ES[I].N)if(es[i].t!=FA) { atDFS (ES[I].T,X);if(cov[es[i].t]) f=1; - } - if(!f&&!cov[x]&&!cov[fa]) ans++,cov[fa]=1; - } - intMain () { -N=read (); Inc (I,1, N-1){intX=read (), Y=read (); PE (x, y);} Dfs1,0); printf"%d", ans);return 0; in}
20161108
BZOJ1596[USACO2008 Jan] Telephone network *