Cell Phone NetworkTime limit:1000msMemory limit:65536kTotal submissions:5735accepted:2053Description
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's the first pasture in the sequence 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 a space-separated integers:a and B
Output
* Line 1: A single integer indicating the minimum number of towers to install
Sample Input
5
1 3
5 2
4 3
3 5
Sample Output
2
Source
Usaco January Gold
John wants all his cows to use their phones to communicate with each other (and get drunk ...). ), he needed to build
Several signal towers are in the N-Block Meadow. The grassland known to be adjacent to the signal tower receives a signal. N-1 a lawn for you (A, B)
Neighboring relationship, Q: The minimum number of towers to be built can be a signal to all grasslands.
Idea: To investigate the minimum dominating set of trees. Minimum dominating set: A value that takes as few points as possible from all vertices to form a set
So that all the remaining points are connected to the points taken out. The dominating set with the smallest number of vertices is called the minimum branch
A set. The greedy method is used here to beg.
1. Search the entire tree at the depth of point 1th to find out the number of each point in DFS and the Father node number for each point.
2. Check the reverse sequence of DFS, if the current point is neither a dominating set nor a point in the dominant set, and it
Father also does not belong to the domination set, the Father point to join the dominant set, the number of dominating sets plus 1.
3. Mark the parent node of the current node, the parent (which belongs to the dominant set), and the parent node of the current node (with the dominant set
Connected to the dots).
Reference: ACM-ICPC Programming Series--Graph theory and application p66~p69
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXN = 20020;struct edgenode{int to; int next;} Edges[maxn];int Head[maxn],father[maxn],newpos[maxn];bool vis[maxn];//newpos[] Indicates which point is the first point of the depth-first traversal sequence// Now indicates how many points are already in the current depth-first traversal sequence,//vis[] for the depth-first traversal of the//father[] represents the Father node number of point I int n,m,now;void DFS (int x) {newpos[now++] = x; for (int k = head[x]; K! =-1; k = edges[k].next) {if (!vis[edges[k].to]) {vis[edges[k].to] = True Father[edges[k].to] = x; DFS (edges[k].to); }}}//s[i] is true, indicating that the point I is covered by//set[i] means that points I belong to the required point set bool S[maxn],set[maxn];int greedy ()//greedy for minimum domination set {memset (s,0,sizeof (S)); memset (set,0,sizeof (Set)); int ans = 0; for (int i = N-1; I >= 1; i--)//reverse sequence check {int t = newpos[i]; if (! S[T])//The current point is not covered, that is, the current point does not belong to the dominant set, the night does not dominate the focus of the point connected {if (! SET[FATHER[T])//The Father node of the current point does not belong to the dominating set, {set[father[t]] = true; WillParent node joins the dominant set ans++; Domination set number plus 1} s[t] = true; S[father[t]] = true; S[father[father[t]] = true; Marks the parent node of the current point, the parent node of the current node, and the parent of the current node}} return ans; int main () {int u,v; while (~SCANF ("%d", &n)) {memset (edges,0,sizeof (Edges)); memset (head,-1,sizeof (Head)); memset (father,0,sizeof (father)); memset (vis,false,sizeof (VIS)); memset (newpos,0,sizeof (Newpos)); int id = 0; for (int i = 0; i < N-1; ++i) {scanf ("%d%d", &u,&v); Edges[id].to = v; Edges[id].next = Head[u]; Head[u] = id++; edges[id].to = u; Edges[id].next = Head[v]; HEAD[V] = id++; } now = 0; Vis[1] = true; FATHER[1] = 1; DFS (1); printf ("%d\n", greedy ()); } return 0;}
POJ3659 Cell Phone Network "minimum domination set" "Greedy"