UVA 1267 Network (DFS)

Source: Internet
Author: User

UVA 1267 Network

Consider a tree network withNnodes where the internal nodes correspond to servers and the terminal nodes correspond to clients. The nodes is numbered from 1 toN. Among the servers, there is an original serverSwhich provides VOD (Video on Demand) service. To ensure the quality of service for the clients, the distance from each client to the VOD serverSshould not exceed a certain valuek. The distance from a nodeuto a nodevIn the tree was defined to being the number of edges on the path fromuTov. If there is a nonempty subsetCof clients such that the distance from eachuInchCToSis greater thank, then replicas of the VOD system has to being placed in some servers so and the distance from each client to the nearest V OD Server (the original VOD system or its replica) iskor less.

Given a tree network, a server S which has VOD system, and a positive integerK , find the minimum number of replicas necessary so, each client are within distanceK from the nearest server which have 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, ten, one, and}, the set of servers is {2, 3, 4, 5, +, +), and the Original VOD server is located at node 12.

For k = 2 , the quality of service was not guaranteed with one VOD server at node because the clients In {6, 7, 8, 9, ten} is away from VOD server at distance> k . Therefore, we need one or more replicas. When one replica are placed at node 4, the distance from each client to the nearest server of {4} are less than or equal to 2. The minimum number of the needed replicas is a for this example.

Input

Your program was 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 all test case contains an integer n (3 n 1) which is the number of nodes of the tree network. The next line contains Integers s (1 s n ) and k ( k 1) where s is the VOD server and K was the distance value for ensuring the Qualit Y 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 a 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



Main topic: give N,s,k, that is, there are n client and server, and then give N-1 edge, connect n host, now on the server serial number is a resource, in order to facilitate all users browse (access path length does not exceed K), to copy resources on fewer servers, Ask that you copy resources at least on several servers .

problem-solving ideas: First of all, the root map is converted to a tree, the resource is located on the server s as the root, and then DFS build, build the process will all the leaf nodes in depth records. The second is to enumerate the servers that need to replicate resources, from the node depth of the largest leaf node, look up the Father node K, that is, the most current policy, and then the server is not more than K terminal all marked .



#include <stdio.h> #include <vector> #include <string.h>using namespace Std;int N, S, K, fa[1005], vis[ 1005];vector<int> gra[1005], node[1005];void Tra (int u, int f, int d) {//Convert to tree fa[u] = f;int n = gra[u].size (); if (n = = 1 && d > k) node[d].push_back (U); for (int i = 0; i < n; i++) {int v = gra[u][i];if (v! = f) Tra (V, u, D + 1);}} void DFS (int u, int f, int d) {Vis[u] = 1;int n = gra[u].size (); for (int i = 0; i < n; i++) {int v = gra[u][i];if (v!) = f && D < K) DFS (V, u, D + 1); Distance not exceeding K}}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 j = 0; J < K; J + +) v = fa[v];//v is a K-class ancestor of U, so the server at V is optimal DFS (V,-1, 0); ans++;}} return ans;} int main () {intt;scanf ("%d", &t), while (t--) {scanf ("%d\n%d%d", &n, &s, &k); for (int i = 1; I <= N; i+ +) {gra[i].clear (); Node[i].clear ();} int A, b;for (inti = 0; i < n-1; i++) {scanf ("%d%d", &a, &b); Gra[a].push_back (b); Gra[b].push_back (a);} Tra (S,-1, 0);p rintf ("%d\n", Solve ());} return 0;}


UVA 1267 Network (DFS)

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.