Example 1.15 Network uvalive 3902

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Problem-Solving ideas: the need to place as few servers as possible, so that all clients to the nearest server distance is not more than K. Since a server has been placed, it can be used as a root node, first to convert the root tree into a root tree, and then we consider the deepest leaves, then it is not difficult to prove that the leaf node of the optimal server placement is its K-level ancestor. This algorithm is not difficult to come up with: from the deepest leaves to enumerate, and in its K-level ancestor place a server, while marking the server can cover all the nodes, so that when all the leaves are covered, the number of servers is the least. Only the leaves that are enumerated to the K+1 layer can be stopped, because leaves that are less than or equal to K are already covered by the root node.

Here's a tip: to make it easy to find all the leaves with a depth of k, we add the leaf nodes to the nodes array in the process of conversion, so that nodes[k] represents all the leaves of the K-layer.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 1000+10vector<int>gr[n ], Nodes[n];int N, S, K, Fa[n];bool covered[n];void dfs (int u, int f, int d)//No root tree goes to root tree, calculates FA array, inserts leaf nodes into nodes table according to depth {fa[u] = f; int NC = Gr[u].size (), if (NC = = 1 && d > k) nodes[d].push_back (U),//nc==1 description is leaf node for (int i = 0; i < NC; i++) { int v = gr[u][i];if (v! = f) Dfs (V, u, D + 1);}}  void dfs2 (int u, int f, int d) {Covered[u] = true;int NC = gr[u].size (); for (int i = 0; i < NC; i++) {int v = gr[u][i];if (V! = F&&d < K) DFS2 (V, u, D + 1);}} int solve () {int ans = 0;memset (covered, 0, sizeof (covered)); for (int d = n-1; d>k; d--)//maximum depthThe degree is n-1, from the maximum depth of the leaves consider for (int i = 0; i < nodes[d].size (); i++) {int u = nodes[d][i];if (Covered[u]) Continue;int v = u;for (int j = 0; J < K; J + +) v = fa[v];//K-Class ancestor Vdfs2 (V,-1, 0);//Place the server at node V and overwrite the corresponding node ans++;} return ans;} int main () {//freopen ("T.txt", "R", stdin), int t;scanf ("%d", &t), while (t--) {scanf ("%d%d%d", &n, &s, &k) ; for (int i = 1; I <= n; i++) {gr[i].clear (); Nodes[i].clear ();} for (int i = 0; i < n-1; i++) {int A, b;scanf ("%d%d", &a, &b); Gr[a].push_back (b); Gr[b].push_back (a);} DFS (S,-1, 0);p rintf ("%d\n", Solve ());} return 0;}


Example 1.15 Network uvalive 3902

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.