Hihocoder 1343:stable members "topological sort"

Source: Internet
Author: User

Hihocoder #1343: Topics

Explain:
A study group, a total of n students, a supervisor. Each student has his or her own mentor (one or more), and the mentor can be the other student or supervisor.
Every week, students submit their study reports and reports to their mentors, a team that is well-designed, without loops (the delivered reports will not go back to their own hands), and all reports will eventually be delivered to the supervisor.
But some of the students in this team will not be able to submit the report to the supervisor because some other student is not working, and we call this student unreliable. It is the reliable learner who is not affected by a student's non-work and the report is submitted.
The problem is finding the number of reliable students.

Input:
The first row of numbers is n, the total number of learners. The next line corresponds to the number and number of tutors for the 1 to n learners, such as the second line (1 0), 1 for the trainee 1, and the supervisor (0 for the supervisor); line Four (2 1 2), which represents 3 of the trainees, two and 1 respectively.

Output:
Number of reliable trainees

Test instructions: A direction-free graph, with only one upstream point. If you delete a point, some subsequent points cannot be connected to the most upstream point, and these follow-up points are unstable. To find out all unstable points, then output the number of remaining stable points.

Solution One: BFS topology

For each vertex V, traverse its subsequent vertices to find all the unstable points. The method of dyeing is as follows:
For a vertex V, a topological sort method is used to traverse its subsequent vertices and dye in turn. The subsequent vertex enters the queue condition that all its parent vertices have been dyed to the vertex v color. Because of this situation, when the deletion of 1 o'clock, although 4 in the side of 2, but also unstable.

#include <iostream> #include <cstdio> #include <cstdlib> #include <vector> #include <queue    > #include <cstring>using namespace std;struct member{int color = 0; vector<int> s, p;//son,parent}mbs[100001];bool is[100001];//unstableint n;bool all_colored (int v, int color) {Boo    L res = true;    for (int i = 0; res && i < mbs[v].p.size (); i++) Res &= (Mbs[mbs[v].p[i]].color = = color); return res;}    void topo (int v) {if (Mbs[v].color! = 0) return;    int color = v;    Mbs[v].color = v;    Queue<int> que;    Que.push (v);        while (!que.empty ()) {int u = que.front ();        Que.pop ();            for (int i = 0; i < mbs[u].s.size (); i++) {int s = mbs[u].s[i];                if (all_colored (s, color)) {Mbs[s].color = color;                Que.push (s);            Is[s] = true;    }}}}int Main () {cin >> N; for (int i = 1; I <= N; i++) {int K;        Cin >> K;            for (int j = 0; J < K; J + +) {int p;            CIN >> P;            Mbs[p].s.push_back (i);        Mbs[i].p.push_back (P);    }} memset (is, 0, sizeof (IS));    for (int i = 1; I <= N; i++) topo (i);    int res = 0;    for (int i = 1; I <= N; i++) res + = Is[i];    cout << n-res << Endl; return 0;}

Solution Two: Memory Search DFS "find the aggregation point for each student"

Think about it, for unstable learners, his mentor path if there are more than one must be at a certain time to converge to the same student, and the stable student convergence point is certainly self.

explanation : first for the direct mentor has the supervisor, then the convergence point is itself, because itself is stable. Second, for a mentor with only one but not the supervisor, iterate to find the closest point of convergence to the supervisor. Finally, for multiple tutors, it is necessary to iterate to find the focal point of their mentor, and if one of the two mentors is different, then he is stable and his point of convergence is himself. If all are the same, then the convergence point is the focal point of their mentors.
As in the sample input:
1, 2 of the mentors are 0, so the convergence point is their own 1 and 2.
3 of the mentors have two 1 and 2, their aggregation points are 1, 2, different, then 3 of the convergence point is 3.
4 of the Mentor is the 3,3 of the convergence point is 3, then 4 of the convergence point is also 3.
5 of the Mentors 4 and 3, the convergence point is 3, so 5 of the convergence point is also 3.

#include <iostream> #include <vector> #include <algorithm> #include <map>using namespace std; Vector<vector<int>> Members;map<int, Int>stablemap;//map stores a learner's aggregation point int stablenum (int num) {if (    Stablemap.find (num)! = Stablemap.end ()) {return stablemap[num];    } vector<int> mentors = members[num-1];        if (Find (Mentors.begin (), Mentors.end (), 0)! = Mentors.end ()) {Stablemap.insert (Make_pair (num, num));    return num;        } else if (mentors.size () = = 1) {int stable = Stablenum (mentors[0]);        Stablemap.insert (Make_pair (mentors[0], stable));    return stable;        } else {int stable = Stablenum (mentors[0]);        Stablemap.insert (Make_pair (mentors[0], stable));            for (int i = 1; i < mentors.size (); i++) {int temp = Stablenum (mentors[i]);            Stablemap.insert (Make_pair (mentors[i], temp)); if (Stable! = temp) {Stablemap.insert (Make_pair (num, num));            return num;        }} stablemap.insert (Make_pair (num, stable));    return stable;    }}void Numofstablem () {int sum = 0;        for (int i = 0; i < members.size (); i++) {vector<int> mentors = members[i];        if (Find (Mentors.begin (), Mentors.end (), 0)! = Mentors.end ()) {sum++;            } else if (Mentors.size () > 1) {if (Stablenum (i+1) = = i+1) {sum++; }}} Cout<<sum<<endl;}    int main () {int N;    scanf ("%d", &n);        while (n--) {int K;        scanf ("%d", &k);        Vector<int> mentors;            while (k--) {int mentor;            scanf ("%d", &mentor);        Mentors.push_back (mentor);    } members.push_back (mentors);    } Numofstablem (); return 0;}

Solution three: Dominate the tree "must through the node, LCA"

Here the master is denoted by the number of a. At this point, the definition of "unstable member" is: if there is a point that has a number not 0, so that all paths from point 0 to that point must pass through this point, then the point represents the member is "unstable member". , all paths from point 0 to 4 must pass through point 3, so member 4 is an unstable member.
Domination Tree:
Expand the definition of "unstable member" above, remove the "non-0 point" limit, you can get the definition of " domination point ", that is: for a certain target point, if there is a point, so that all the paths from the starting point to the target point in the graph must pass through that point, then the point is the "dominant point" of the target Medium: Points 1, 2, 3, 4 dominate points are: 0, 0, 0, 0 and 3, respectively. Obviously, if all the points in the graph can be reached from the starting point, then the starting point is a bit of a "dominating point" in the graph.
One point of "dominance" is not necessarily the only one. For example, if for a point, there are only 1 paths from the starting point to that point, then all points on that path are the point of domination. For situations where there are multiple domination points, we can find a dominant point, which is closest to the target point, which we become the " Direct domination point ". For a given graph, a point may have multiple dominating points, but its direct dominance point must be unique. At this point, outside the starting point, all the points have their own unique "direct control point."
The dominant tree is a multi-fork tree: The points in the graph correspond to the nodes of the tree, and the parent node of each node is its direct dominant point. The tree in the chart above consists of the following trees:

Obviously, when the tree is built, the parent node of each point is its direct point of domination, and all ancestor nodes of that point are its dominant point. At this point, according to test instructions, the "stable member" We are looking for is the direct domination point being the member of Point No. 0 (Master), which is the child who dominates the root node in the tree.
·Achievements:
In order to establish a dominant tree, it is necessary to know the direct domination point of each point. Consider the "precursor" of each point in the original, considering the mentor of each member. If a member has only one mentor, then it is clear that the path from master to that member will pass through his mentor, so mentor is the direct point of view of the member; for abstract graphs, if a point has only one precursor point, then the precursor is the direct point of the current point. If a member has more than one mentor, then for a mentor, from master to that member will not necessarily pass it, so when a member has more than mentor, his mentor is not his direct point of view, for the abstract diagram, if a point has multiple precursors , then these precursors are not its direct point of domination, we need to consider the dominant point of these precursor nodes, when these precursor nodes have a common point of control, it is stated that all the paths from the starting point to these precursor points will pass through this common point of control, that is to say, All paths from the starting point to the target point pass through this common point of domination, so this common dominating point is the direct point of control of the target point. This conclusion is also used for cases where there is only one precursor node
According to the definition of the dominant tree, the dominant point shared by multiple nodes is clear, that is their public ancestor, and we are looking for the nearest public ancestor. This conclusion is also used in cases where there is only one precursor node, because if there is only one point, then its nearest public ancestor is itself.
Thus, the process of establishing a dominant tree is to first add the starting point to the tree as the root of the entire dominant tree, and then for each node, find the nearest public ancestor of all its precursor nodes in the dominant tree, the ancestor node that is the parent node of the current node.
·topology Ordering:
One condition of the above build-up process is to ensure that when a point is added to the tree, it must be ensured that all its precursors are already in the tree so that the nearest public ancestor of those points can be found. Therefore, the order in which nodes are added into the tree is important. We can find the proper order by topological sort, and the result of topological sort is the order of node join tree. This ensures that the precursor node must be joined to the tree before the current node.
·Recent public Ancestors:
The process of achievement was designed to the nearest common ancestor of multiple nodes. We can use an O (LGN) complexity algorithm to solve it. Considering the height of each node in the tree, move the node with a small height up the parent node pointer, and then move up the parent node while all nodes are the same height, when all nodes reach the same node, the endpoint is the nearest public ancestor of those nodes.

The above is the solution of the idea, the completion of the establishment of the ruling tree, statistics on the root node how many children , is the answer.

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector    > Using namespace std;  const int MAXN = 100005;  struct Edge {int to,next;  }EDGE[MAXN*10]; int N,CNT,ANS,HEAD[MAXN],DEG[MAXN]; int dep[maxn],parent[maxn],tmp[15];    Tmp[i] indicates that the first direct precursor to the node vector<int> G[MAXN];      void Addedge (int u,int v) {edge[cnt].to = v;      Edge[cnt].next = Head[u];  Head[u] = cnt++;      } int LCA (int u) {int MIN_DEP =-1;          for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];          Tmp[i] = v;      if (MIN_DEP = =-1 | | min_dep > DEP[V]) MIN_DEP = Dep[v]; } for (int i = 0; i < g[u].size (), i++) {while (Dep[tmp[i]] > MIN_DEP) tmp[i] = Paren      T[tmp[i]];          } while (true) {int i;          for (i = 1; i < g[u].size (); i++) if (tmp[i]! = tmp[0]) break; if (I >= g[u].size ()) break;      for (int i = 0; i < g[u].size (); i++) tmp[i] = parent[tmp[i]];  } return tmp[0];      } void BFs () {queue<int> q;      for (int i = 0; I <= n; i++) if (deg[i] = = 0) Q.push (i);          while (!q.empty ()) {int u = q.front ();          Q.pop ();              for (int i = head[u]; i =-1; i = edge[i].next) {int v = edge[i].to;              deg[v]--;                  if (deg[v] = = 0) {Parent[v] = LCA (v);                  DEP[V] = Dep[parent[v]] + 1;                  if (parent[v] = = 0) ans++;              Q.push (v);          }}}} int main () {while (scanf ("%d", &n)!=eof) {memset (head,-1,sizeof (head));          memset (deg,0,sizeof (deg));          for (int i = 0; I <= N; i++) g[i].clear ();              for (int i = 1; I <= n; i++) {int k;              scanf ("%d", &k); For (inT j = 1; J <= K;                  J + +) {int u;                  scanf ("%d", &u);  Addedge (U,i); Child g[i].push_back (U);  Parent deg[i]++;          In degree}} ans = 0;          BFS ();      printf ("%d\n", ans);  } return 0;  }

Hihocoder 1343:stable members "topology sort"

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.