(06-08补)UVALive 3902 Network 网络
来源:互联网 发布:cms排行 编辑:程序博客网 时间:2024/06/12 00:04
题目大意:n台机器连成一个树状网络,其中叶节点是客户端,其他节点是服务器。现在有一台服务器在节点s,服务器能传播的信号的距离为k,因为有的用户距离服务器的距离大于k,所以必须添加服务器。问最少要添加几个服务器,才能使每个客户端都收到信号
分析:首先以S为根节点建立一颗有根树,并dfs建树时记录点的深度,是否为叶子。
把叶子按深度从大到小排序,每次贪心选一个深度最大的叶子,然后找到他的k级祖先,以k级祖先出发dfs标记K层以内的叶子,如此反复直到叶子都被染色。
复杂度O (N)
本代码存在部分bug:实在未能发现,望指导
#include <cstdio>#include <cstring>#include <vector>#include <iostream>#include <algorithm>using namespace std;const int maxn = 1005;vector <int> node[maxn], mapp[maxn];int fa[maxn], n, s, k;bool vis[maxn];void dfs(int u, int f, int d){ //寻找叶子结点 fa[u] = f; if (mapp[u].size() == 1 && d>k) node[d].push_back(u); //避开按深度排序 for (int i = 0; i<mapp[u].size(); i++){ int v = mapp[u][i]; if (v != f) dfs(v, u, d+1); }}void dfs2(int u, int f, int d){ vis[u] = true; for (int i = 0; i<mapp[u].size(); i++){ int v = mapp[u][i]; if (v != f && d<k) dfs(v, u, d+1); }}int solve(){ int ans = 0; memset(vis, 0, sizeof(vis)); for (int d = n-1; d>k; d++) for (int i = 0; i<node[d].size(); i++){ //从深度最大出发,贪心 int u = node[d][i]; if (vis[u]) continue; int v = u; for (int i = 0; i<k; i++) v = fa[v]; //寻找k级祖先 dfs2(v, -1, 0); ans++; } return ans;}int main(){ int T; scanf("%d", &T); while (T--){ scanf("%d%d%d", &n, &s, &k); for (int i = 1; i<=n; i++){ mapp[i].clear(); node[i].clear(); } for (int i = 0; i<n; i++){ int a, b; scanf("%d%d", &a, &b); mapp[a].push_back(b); mapp[b].push_back(a); } dfs(s, -1, 0); printf("%d\n", solve()); } return 0;}
1 0
- (06-08补)UVALive 3902 Network 网络
- UVaLive(LA) 3902 Network,Seoul 2007
- UVALive - 3902 Network
- UVALive 3902 Network
- UVALive - 3902 Network
- 算法竞赛入门经典训练指南 例题1-15 网络 Network UVALive 3902
- UvaLive 6435 Network Packet Ordering(dp)
- UVALive 4960Sensor network(MST+LCA)
- UVALive 4960 Sensor network(MST+LCA)
- 例题1.15 网络 UVALive 3902
- 网络(Network,Seoul 2007,LA 3902)
- UVALive 3027 Corporative Network
- UVALive 3027 Corporative Network
- UVALive 4731 Cellular Network
- UVALive 4731 Cellular Network
- UVALive 3027 Corporative Network
- UVALive 3027 Corporative Network
- UVALive 3027 Corporative Network
- Android 屏幕保持常亮
- ubuntu14.04初见
- 如何在vs2010中使用SSE指令集
- JAVA 实用类库及其应用
- 手机开发实战127——AT介绍1
- (06-08补)UVALive 3902 Network 网络
- 手机开发实战128——AT介绍2
- 创建对象--构造函数
- ubuntu 重装后的一系列问题
- Android shape属性
- 手机开发实战129——AT介绍3
- Struts2启动报错
- 成为Java大牛的25个要素!!!
- html表单应用的简单例子