Cell Phone Network
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 6781 |
|
Accepted: 2429 |
Description
Farmer John had decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on he N (1≤ n ≤10,000) pastures (conveniently Numbered 1.. N) so they can all communicate.
Exactly N-1 pairs of pastures is adjacent, and for any of the pastures a and B (1≤ a ≤ N; 1 ≤ B ≤ N; a ≠ B) There is a sequence of adjacent pastures such that a was the first pasture in the Sequenc E and B are the last. Farmer John can only place cell phone towers in the pastures with each tower have enough range to provide service to the PA Sture it is in and all pastures adjacent to the pasture with the cell tower.
Help him determine the minimum number of towers he must install to provide cell phone service to each pasture.
Input
* Line 1: A single integer: N
* Lines 2. N: Each line specifies a pair of adjacent pastures with the space-separated integers: a and b
Output
* Line 1: A single integer indicating the minimum number of towers to install
Sample Input
51 35 24) 33 5
Sample Output
2
Source
Usaco January Gold "Analysis" give you a tree, when you dye a node, his neighboring nodes will be dyed color, ask the least choice of how many nodes staining, can be the whole tree infected. This is the tree's minimum dominating set template problem, greedy to do.
#include <iostream>#include<cstring>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<time.h>#include<string>#include<map>#include<stack>#include<vector>#include<Set>#include<queue>#defineINF 10000000#defineMoD 10000typedefLong Longll;using namespacestd;Const intn=2e4+Ten;Const intm=50000;int inch[n],vis[n];intn,m,k,tim=0;intNewpos[n],p[n];vector<int>Vec[n],edg[n];BOOLS[n],se[n];voidDfsintUintFA) {Newpos[tim++]=u; //printf ("%d%d\n", tim-1,newpos[tim-1]); for(intI=0; I<edg[u].size (); i++){ intv=Edg[u][i]; if(v!=FA) {P[v]=u; DFS (V,U); } }}intgreedy () {intans=0; inti; for(i=n-1; i>=0; i--) { intt=Newpos[i]; //printf ("%d%d%d\n", t,p[t],p[p[t]]); if(!S[t]) { if(!Se[p[t]]) {Se[p[t]]=true; Ans++; } S[t]=true; S[p[t]]=true; S[P[P[T] ]=true; } } returnans;}intMain () {intu,v,ans=0;; scanf ("%d",&N); for(intI=1; i<n; i++) {scanf ("%d%d",&u,&v); Edg[u].push_back (v); Edg[v].push_back (U); } DFS (1,0); printf ("%d\n", greedy ()); return 0;}
POJ 3659 Cell Phone Network (minimum dominating set of trees) (greedy)