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