UVA 1267-network (Greedy dfs)

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions: At first there is only one node on the server, in order to let all the leaf node distance from the server is not more than K, we add the server on the non-leaf node, ask the least how many servers to add.

Idea: Greed. The first server node as the root node, the downward expansion, record the parent-child relationship, the depth of the leaf node to sort, starting from the deepest node to find the K-Distance parent node, install the server, and a DFS, will all the distance it does not exceed the node mark of K.

See the code for details:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long long ll;typedef long double ld;const ld EPS = 1e-9, PI = 3.14159265358979323846264 33832795;const int mod = 1000000000 + 7;const int INF = int (1e9); Const LL INF64 = LL (1e18); const int MAXN = + + 10;int    T,n,m,s,k,root,p[maxn];bool vis[maxn];struct Node {int u, D;    Node (int u=0, int d=0): U (u), D (d) {} BOOL operator < (const node& RHS) Const {return d < RHS.D;    }};vector<int> g[maxn];vector<node> d;void Pre (int u, int fa, int cur) {int cnt = g[u].size ();    P[u] = FA; if (cnt = = 1) {D.push_back (Node (u, cur)); return ;        } for (int i=0;i<cnt;i++) {int v = g[u][i];        if (v! = FA) {Pre (V, U, cur+1);    }}}void dfs (int u, int fa, int cur) {if (cur > k) return;    int len = G[u].size ();    Vis[u] = true;        for (int i=0;i<len;i++) {int v = g[u][i];        if (v! = FA) {dfs (V, u, cur+1);    }}}int U, v;int Main () {scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n,&s,&k);        for (int i=1;i<=n;i++) g[i].clear ();        D.clear ();            for (int i=1;i<n;i++) {scanf ("%d%d", &u,&v);            G[u].push_back (v);        G[v].push_back (U);        } pre (S, s, 0);        Sort (D.begin (), D.end ());        Memset (Vis, false, sizeof (VIS));        DFS (s, s, 0);        int len = D.size ();        int ans = 0;            for (int i=len-1;i>=0;i--) {node u = d[i];            if (vis[u.u]) continue;            int v = u.u;           for (int j=0;j<k;j++) v = p[v]; DFS (V, v, 0);        ++ans;    } printf ("%d\n", ans); } return 0;}


UVA 1267-network (Greedy 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.