博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LA3902 Networlk
阅读量:4358 次
发布时间:2019-06-07

本文共 5093 字,大约阅读时间需要 16 分钟。

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 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
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 }
LA3902

 

转载于:https://www.cnblogs.com/huibixiaoxing/p/8286686.html

你可能感兴趣的文章
【转】Android中自定义控件的步骤
查看>>
软件测试工作中的沟通问题
查看>>
format 的用法,9*9乘法表
查看>>
mysql--5
查看>>
uva11214 Guarding the Chessboard
查看>>
CentOS6.4下Git服务器Gitosis安装配置
查看>>
007 斐波那契数列
查看>>
《Docker入门实战》笔记(一)
查看>>
hdu 3635 Dragon Balls (并查集)
查看>>
文件操作
查看>>
7.java集合,泛型简单总结,IO流
查看>>
杭电2007 平方和与立方和
查看>>
JS邮箱验证-正则验证
查看>>
关于SQL查询效率,100w数据,查询只要1秒
查看>>
Quartz 2D绘图
查看>>
JS Fetch
查看>>
EJB 笔记
查看>>
【delete】Android自定义控件(四) 自定义ImageView动态设置ImageView的高度
查看>>
HDUOJ------(1230)火星A+B
查看>>
Servlet
查看>>