[UVa 1267]Network

题解

我们将图中给出的$VOD$为根建立有根树,每次取出未覆盖的最深的节点第$k$级祖先,将其建为$VOD$,遍历一遍,将能够盖到的点标记。重复,直到盖完。贪心正确性可以证明。

  1 //It is made by Awson on 2017.9.16
  2 #include <map>
  3 #include <set>
  4 #include <cmath>
  5 #include <ctime>
  6 #include <queue>
  7 #include <stack>
  8 #include <cstdio>
  9 #include <string>
 10 #include <vector>
 11 #include <cstdlib>
 12 #include <cstring>
 13 #include <iostream>
 14 #include <algorithm>
 15 #define LL long long
 16 #define Max(a, b) ((a) > (b) ? (a) : (b))
 17 #define Min(a, b) ((a) < (b) ? (a) : (b))
 18 #define Abs(a) ((a) < 0 ? (-(a)) : (a))
 19 using namespace std;
 20 const int N = 1000;
 21
 22 int n, s, k, u, v;
 23 struct tt {
 24     int to, next;
 25 }edge[N*2+5];
 26 int path[N+5], top;
 27
 28 int dep[N+5], fa[N+5];
 29 bool isleave[N+5];
 30
 31 struct node {
 32     int u, dist;
 33     node () {
 34     }
 35     node (int _u,int _dist) {
 36         u = _u; dist = _dist;
 37     }
 38     bool operator < (const node & b) const{
 39         return dist < b.dist;
 40     }
 41 };
 42 priority_queue<node>Q;
 43
 44 int get_father(int u, int kth) {
 45     if (!kth) return u;
 46     return get_father(fa[u], kth-1);
 47 }
 48 void dfs2(int u, int kth) {
 49     isleave[u] = false;
 50     if (!kth) return;
 51     for (int i = path[u]; i; i = edge[i].next)
 52         dfs2(edge[i].to, kth-1);
 53 }
 54 void dfs(int u, int depth) {
 55     dep[u] = depth;
 56     for (int i = path[u]; i; i = edge[i].next)
 57         if (dep[edge[i].to] == -1) {
 58             dfs(edge[i].to, depth+1);
 59             fa[edge[i].to] = u;
 60             isleave[u] = false;
 61         }
 62 }
 63 void add(int u, int v) {
 64     edge[++top].to = v;
 65     edge[top].next = path[u];
 66     path[u] = top;
 67 }
 68 void work() {
 69     memset(path, 0, sizeof(path)); top = 0;
 70     memset(dep, -1, sizeof(dep));
 71     memset(isleave, 1, sizeof(isleave));
 72     memset(fa, 0, sizeof(fa));
 73     scanf("%d%d%d", &n, &s, &k);
 74     for (int i = 1; i < n; i++) {
 75         scanf("%d%d", &u, &v);
 76         add(u, v); add(v, u);
 77     }
 78     dfs(s, 0);
 79     for (int i = 1; i <= n; i++)
 80         if (isleave[i] && dep[i] > k) {
 81             Q.push(node(i,dep[i]));
 82         }
 83     int ans = 0;
 84     while (!Q.empty()) {
 85         while (!isleave[Q.top().u]) {
 86             Q.pop();
 87             if (Q.empty()) break;
 88         }
 89         if (Q.empty()) break;
 90         int father = get_father(Q.top().u, k);
 91         dfs2(father, k);
 92         ans++;
 93     }
 94     printf("%d\n", ans);
 95 }
 96 int main() {
 97     int t;
 98     scanf("%d", &t);
 99     while (t--)
100         work();
101     return 0;
102 }
时间: 2024-11-23 01:33:00

[UVa 1267]Network的相关文章

uva 1267 Network(DFS)

uva 1267 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 VO

UVA 1267 - Network(贪心DFS)

题目链接:点击打开链接 题意:一开始只有一个结点上有一个服务器, 为了让所有叶子结点距离服务器的距离不超过k, 我们在非叶子结点上添加服务器, 问最少添加多少服务器. 思路:贪心. 将第一个服务器所在结点作为根结点, 向下拓展, 记录父子关系, 将叶子结点的深度排序, 从最深的结点开始向上找k个距离的父节点, 安装服务器, 并进行一次DFS, 将所有距离它不超过k的结点标记. 细节参见代码: #include<cstdio> #include<cstring> #include&l

uva 315 Network(连通图求割点)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251  Network  A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers

UVA - 315 Network 和 UVA - 10199 (求割顶)

链接 :  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20837 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21278 求割顶的裸题. UVA - 315 #include <algorithm> #include <iostream> #include <sstream> #include <cstrin

UVA 315 Network(无向图求割点)

题目大意 A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together two pl

UVA 318 Network(无向图求割点数)

Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect togethe

UVA 315 Network

Description: A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect togethe

POJ 1144 &amp; Uva 315 Network 【求割点数目】

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10855   Accepted: 5020 链接:http://poj.org/problem?id=1144 Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places

Uva 网络(Network,Seoul 2007,LA 3902)

1 #include<iostream> 2 #include<cstring> 3 #include<vector> 4 using namespace std; 5 6 const int maxn=1000+10; 7 int n,s,k; 8 vector<int> tree[maxn],nodes[maxn]; 9 int fa[maxn]; 10 bool covered[maxn]; 11 12 void dfs(int u,int f,int