HDU 5326 Company Personnel Management tree problem (multi-school)-Simple DP

Source: Internet
Author: User

Test instructions: Give a company's personnel management tree, for direct and indirect subordinate total K people have how many.

Analysis: Just start a look at the tree is frightened, do not dare to do, then bite the tooth a think, not difficult ah, G elder brother Said is DP, I used I do not know the method, too. My method is to use Vis[i][j] to record I and J Direct affiliation, and then through Vis[i][j] and vis[j][k] and can find vis[i][k] indirect affiliation, A[I]+=A[J],A[I]+=A[K] can. Believe in yourself.

This is the G-Brother code:

Recursive Dafa must learn to use it.

#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt  #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long long using namespace std; const int MAXN = + 5;//const int INF = 0x3f3f3f3f;int d[105];vector<int> g[105];int N, k;int son[105];int DP (    int i) {if (d[i]! =-1) return d[i];    D[i] = 0;    for (int j = 0; J < G[i].size (); j + +) {D[i] + = DP (G[i][j]) +1; } return d[i];}    int main () {//Freopen ("Input.txt", "R", stdin);        while (scanf ("%d%d", &n, &k) = = 2) {memset (son, 0, sizeof (son));        memset (d,-1, sizeof (d));        for (int i = 1; I <= n; i++) g[i].clear ();            for (int i = 0; i < n-1; i++) {int u, v;            cin >> u >> v;        G[u].push_back (v); } inT-ans = 0;        for (int i = 1; I <= n; i++) {if (DP (i) = = k) ans++;    } cout << ans << endl; } return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5326 Company Personnel Management tree problem (multi-school)-Simple DP

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.