UVa 1220 Party at Hali-bula (tree DP, max standalone set)

Source: Internet
Author: User

Test instructions: The company has n people to form a tree structure, in addition to the boss have only a direct boss, ask to choose as many as possible, but not at the same time select a person and his direct supervisor, ask the maximum number of people can choose, and is not the only solution.

Analysis: This question is almost the biggest independent set problem of the tree, but one more judgment uniqueness. Use two arrays, one to record the number of people, one to judge uniqueness.

D[u][0], in a subtree that is the root of U, the maximum number of people can be obtained without selecting u points, then d[u][1] is the maximum number of points that can be selected.

F[u][0] is similar, in a subtree that is rooted in u, whether the u point is unique or not, then f[u][1] is the choice of whether the U point is unique.

For d[u][1] calculations, because you chose U, then the child nodes of U are not selectable, so it is d[u][1] = SUM (d[v][0], V is a child node), when F[v][0] is not unique, f[u][1] is not unique.

For d[u][0] calculation, because no U is selected, then its sub-nodes can be selected or not selected, that is, the largest choice, that is, d[u][0] = SUM (max (d[v][0), d[v][1]), then the uniqueness of how to judge it? Almost like the top,

One more, if d[v][0] = = D[v][1], then this is not the only, the other and the same as above. The rest is simple, with DFS.

The code is as follows:

#include <iostream> #include <cstdio> #include <map> #include <cstring> #include <vector>  using namespace Std;const int maxn = + 5;vector<int> g[maxn];bool f[maxn][2];int d[maxn][2], N, cnt;//d[u][0] not selected    , d[u][0] Select map<string, int> id;void init () {//Initialize for (int i = 1; I <= n; ++i) g[i].clear ();    Memset (f, false, sizeof (f));    memset (d, 0, sizeof (d));   CNT = 0; Id.clear ();}    int GetID (const string &s) {//Get ID if (Id.count (s)) return id[s]; return id[s] = ++cnt;} void Dfs (int u) {if (!        G[u].size ()) {//bottom d[u][0] = 0;        D[U][1] = 1;    return;        } for (int i = 0; i < g[u].size (); ++i) {int son = g[u][i];        DFS (son); D[U][1] + = d[son][0];//d[u][1] calculation if (f[son][0]) f[u][1] = true;//judgment uniqueness if (D[son][0] > D[son][1]) {//d[u]            [0] Calculation d[u][0] + = d[son][0];        if (f[son][0]) f[u][0] = true; } else if (d[son][0] = = D[son][1]) {//equal, that is not unique d[u][0] + = d[son][0];        F[u][0] = true;            } else {d[u][0] + = d[son][1];        if (f[son][1]) f[u][0] = true;        }} ++d[u][1];//don't forget to add 1}int main () {while (scanf ("%d", &n) = = 1 && N) {init ();        string s1, S2;  CIN >> S1;        GetID (S1);            for (int i = 0; i < n-1; ++i) {cin >> s1 >> S2;        G[getid (S2)].push_back (GetID (S1));        } dfs (1); if (d[1][0] = = d[1][1]) printf ("%d no\n", d[1][0]);//judge who is in the number greater else if (d[1][0] > D[1][1]) printf ("%d%s\n", d[1][ 0], f[1][0]?        "No": "Yes"); else printf ("%d%s\n", d[1][1], f[1][1]?    "No": "Yes"); } return 0;}

UVa 1220 Party at Hali-bula (tree DP, max standalone set)

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.