LA 3902 UVA 1267 - Network
2013-08-16 14:21
561 查看
1267 - Network
Time limit: 3.000 secondsConsider 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.![](http://uva.onlinejudge.org/external/12/p3902.png)
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![](http://uva.onlinejudge.org/external/12/3902img2.png)
n
![](http://uva.onlinejudge.org/external/12/3902img2.png)
1, 000) which is the number of nodes of the tree network. The next line contains two integers s (1
![](http://uva.onlinejudge.org/external/12/3902img2.png)
s
![](http://uva.onlinejudge.org/external/12/3902img2.png)
n) and k (k
![](http://uva.onlinejudge.org/external/12/3902img3.png)
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 题目大意:给一树型网络,初始一个点s放置一台服务器,给一个k。先要求放置最少的服务器,使得所有叶子节点距离服务器距离<=k。输出k。 先转换成s为根的树,根据观察即可发现策略:从最深的叶子节点开始考虑,每次放在距离最远的没有覆盖的叶子节点k的节点,每放置一个服务器进行一次dfs。 这里用一个深度表node来保存深度:vector<int> node[maxn]; node[d] 就是深度为d的叶子节点。
1 #include <cstdio> 2 #include <vector> 3 #include <algorithm> 4 #include <cstring> 5 #include <map> 6 #include <queue> 7 #include <set> 8 9 using namespace std; 10 11 #define SI(a) scanf("%d", &(a)) 12 #define SS(a) scanf("%s", (a)) 13 #define SC(a) scanf("%c", &(a)); 14 #define Max(a, b) (a > b ? a : b) 15 #define Min(a, b) (a < b ? a : b) 16 17 const int maxn = 1000 + 50; 18 19 vector<int> G[maxn]; 20 vector<int> node[maxn]; 21 int covd[maxn]; 22 int pa[maxn]; 23 int n, k; 24 25 void dfs(int u, int fa, int d) { 26 pa[u] = fa; 27 int nc = G[u].size(); 28 if(nc == 1 && d > k) node[d].push_back(u); 29 for(int i=0; i<nc; i++) if(G[u][i] != fa) { 30 dfs(G[u][i], u, d+1); 31 } 32 } 33 34 void dfs2(int u, int fa, int d) { 35 covd[u] = 1; 36 if(d < k) 37 for(int i=0; i<G[u].size(); i++) if(fa != G[u][i]) 38 dfs2(G[u][i], u, d+1); 39 40 } 41 42 43 int main() { 44 int T; 45 46 SI(T); 47 while(T--) { 48 int s; 49 50 SI(n); 51 SI(s); SI(k); 52 memset(covd, 0, sizeof(covd)); 53 for(int i=0; i<=n; i++) {G[i].clear(); node[i].clear(); } 54 covd[s] = 1; 55 for(int i=0; i<n-1; i++) { 56 int u, v; 57 SI(u); SI(v); 58 G[u].push_back(v); 59 G[v].push_back(u); 60 } 61 62 int ans = 0; 63 64 dfs(s, -1, 0); 65 dfs2(s, -1, 0); 66 for(int d=n-1; d>k; d--) { 67 for(int i=0; i<node[d].size(); i++) { 68 if(covd[node[d][i]]) continue; 69 int v = node[d][i]; 70 for(int j=0; j<k; j++) v = pa[v]; 71 dfs2(v, -1, 0); 72 ans++; 73 } 74 } 75 printf("%d\n", ans); 76 77 } 78 79 return 0; 80 }
相关文章推荐
- LA 3902 UVA 1267 - Network
- UVA 1267 && LA 3902 Network (思路--树上的DFS)
- UVaLive(LA) 3902 Network,Seoul 2007
- Uva-Live Archive3902-Network
- Uva 3902 Network
- UVa 1267 Network (DFS&贪心)
- uva 1267 - Network
- UVA 1267 Network(dfs)
- 3902 - Network (LA)
- UVA 1267 - Network
- uva 1267 Network
- LA 3902 Network
- uva 1267 Network(DFS)
- UVaLive 3902 Network (无根树转有根树,贪心)
- 算法竞赛入门经典训练指南 例题1-15 网络 Network UVALive 3902
- uva1267 Network
- UVA - 1267 Network
- uva 1267 - Network(dfs)
- NetWork(LA 3902)深度优先搜索
- uva1267 - Network