LA3902 Networlk
Network
Consider a tree network with n nodes where the internal nodes correspond to servers and the terminal nodes correspond to clients. The nodes are numbered from 1 to n. Among the servers, there is an original server S which provides VOD (Video On Demand) service. To ensure the quality of service for the clients, the distance from each client to the VOD server S should not exceed a certain value k. The distance from a node u to a node v in the tree is defined to be the number of edges on the path from u to v. If there is a nonempty subset C of clients such that the distance from each u in C to S is greater than k , then replicas of the VOD system have to be placed in some servers so that the distance from each client to the nearest VOD server (the original VOD system or its replica) is k or less. Given a tree network, a server S which has VOD system, and a positive integer k, find the minimum number of replicas necessary so that each client is within distance k from the nearest server which has the original VOD system or its replica. For example, consider the following tree network. In the above tree, the set of clients is {1, 6, 7, 8, 9, 10, 11, 13}, the set of servers is {2, 3, 4, 5, 12, 14}, and the original VOD server is located at node 12. For k = 2, the quality of service is not guaranteed with one VOD server at node 12 because the clients in {6, 7, 8, 9, 10} are away from VOD server at distance > k. Therefore, we need one or more replicas. When one replica is placed at node 4, the distance from each client to the nearest server of {12, 4} is less than or equal to 2. The minimum number of the needed replicas is one for this example. Input Your program is to read the input from standard input. The input consists of T test cases. The number of test cases (T) is given in the first line of the input. The first line of each test case contains an integer n (3 ≤ n ≤ 1, 000) which is the number of nodes of the tree network. The next line contains two integers s (1 ≤ s ≤ n) and k (k ≥ 1) where s is the VOD server and k is the distance value for ensuring the quality of service. In the following n − 1 lines, each line contains a pair of nodes which represent an edge of the tree network. Output Your program is to write to standard output. Print exactly one line for each test case. The line should contain an integer that is the minimum number of the needed replicas. Sample Input 2 14 12 2 1 2 2 3 3 4 4 5 5 6 7 5 8 5 4 9 10 3 2 12 12 14 13 14 14 11 14 3 4 1 2 2 3 3 4 4 5 5 6 7 5 8 5 4 9 10 3 2 12 12 14 13 14 14 11 Sample Output 1 0
坑点:只需覆盖叶子节点即可,不用覆盖中间节点
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #define min(a, b) ((a) < (b) ? (a) : (b)) 9 #define max(a, b) ((a) > (b) ? (a) : (b)) 10 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a)) 11 inline void swap(int &a, int &b) 12 { 13 int tmp = a;a = b;b = tmp; 14 } 15 inline void read(int &x) 16 { 17 x = 0;char ch = getchar(), c = ch; 18 while(ch < '0' || ch > '9') c = ch, ch = getchar(); 19 while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar(); 20 if(c == '-') x = -x; 21 } 22 23 const int INF = 0x3f3f3f3f; 24 const int MAXN = 2000 + 10; 25 26 struct Edge 27 { 28 int u,v,nxt; 29 Edge(int _u, int _v, int _nxt){u = _u;v = _v;nxt = _nxt;} 30 Edge(){} 31 }edge[MAXN << 1]; 32 int head[MAXN], cnt, fa[MAXN], q[MAXN], deep[MAXN], n, t, s, k, b[MAXN], ans; 33 inline void insert(int a, int b) 34 { 35 edge[++cnt] = Edge(a,b,head[a]);head[a] = cnt; 36 } 37 38 void bfs(int u) 39 { 40 int he = 1, ta = 2; 41 q[he] = s;deep[s] = 0;fa[s] = 0; 42 while(he < ta) 43 { 44 int now = q[he ++]; 45 for(register int pos = head[now];pos;pos = edge[pos].nxt) 46 { 47 int v = edge[pos].v; 48 if(v == fa[now]) continue; 49 fa[v] = now;deep[v] = deep[now] + 1; 50 q[ta ++] = v; 51 } 52 } 53 } 54 55 void dfs(int u, int pre, int step) 56 { 57 if(u == 0)return; 58 if(step > k) return; 59 for(register int pos = head[u];pos;pos = edge[pos].nxt) 60 { 61 int v = edge[pos].v; 62 if(v == pre)continue; 63 b[v] = 1; 64 dfs(v, u, step + 1); 65 } 66 } 67 68 int main() 69 { 70 read(t); 71 for(;t;--t) 72 { 73 memset(head, 0, sizeof(head));cnt = 0;ans = 0; 74 memset(edge, 0, sizeof(edge));memset(b, 0, sizeof(b)); 75 memset(deep, 0, sizeof(deep));memset(fa, 0, sizeof(fa)); 76 memset(q, 0, sizeof(q)); 77 read(n), read(s), read(k); 78 for(register int i = 1;i < n;++ i) 79 { 80 int tmp1,tmp2; 81 read(tmp1), read(tmp2); 82 insert(tmp1, tmp2); 83 insert(tmp2, tmp1); 84 } 85 bfs(s); 86 for(register int j = n;deep[q[j]] > k;-- j) 87 if(!b[q[j]]) 88 { 89 //是否是叶子 90 int tmp = 0; 91 for(register int pos = head[q[j]];pos;pos = edge[pos].nxt) 92 ++ tmp; 93 if(tmp > 1) continue; 94 int tmp1 = k, tmp2 = q[j]; 95 while(fa[tmp2] && tmp1) 96 { 97 -- tmp1; 98 tmp2 = fa[tmp2]; 99 } 100 b[tmp2] = 1; 101 dfs(tmp2, -1, 1); 102 ++ ans; 103 } 104 printf("%d\n", ans); 105 } 106 return 0; 107 }