Hihocoder1343:stable members "BFS topology sequencing"

Source: Internet
Author: User

title Link: https://hihocoder.com/problemset/problem/1343 #1343: Stable Members time limit:10000ms single point time limit:1000ms memory limit:256MB Description

Recently Little Hi joined an algorithm learning group. The group consists of one algorithm master and members N . The members is numbered from 1 to N . Each member have one or more and the as his mentors. Some members ' mentor are the master himself.

Every week each member sends a report of he own learning progress and the reports collected from he pupils (if there is Any) to his mentors. The group is so well designed this there is no loop in the reporting chain so no one receives he own report from his Pupi L. And finally the master gets every one ' s report (maybe more than once).

Little Hi notices that for some members their reporting routes to the master can is easily cut off by a single member ' s (o Ther than the master and himself) absence from the reporting duty. They is called unstable members while the others is stable members. Given the reporting network of the group, can you find what is many members is stable?

Assume there is 4 members of the group. Member 1 and 2 both has the master as their only mentor. Member 3 has 2 Mentors:member 1 and Member 2. Member 4 has 1 Mentor:member 3. Then member 4 was the only unstable member in the group because if member 3 was absent his learning report would be unable to be sent to the master.

input

The first line contains a integer N , the number of members.

The-th line of the following lines describe the mentors of the-th i N i member. The first integer Ki is, the number of mentors of the i -th member. Then follows Ki integers ... A1 AN , which is his mentors ' numbers. Number 0 indicates the master is one of the mentor.

For 40% of the data, 1≤ N ≤1000.

For 100% of the data, 1≤ N ≤100000.

For 100% of the data, 1≤ Ki ≤10, Ki < N, 0≤ Ai ≤ N .

Output

Output the number of stable members.

Sample input
Sample output
3
Test instructions: to a undirected graph, define a point for unstable when and only if a point is deleted (not for itself or point 0), it can not be connected with point 0, the other point is stable, the figure has several stable points. Sample Explanation: as shown in the diagram on the left, delete point 3, then 4 and 5 can not connect to 0 that is master, so 4 and 5 are unstable points, 1, 2 and 3 are stable, so the final answer is 3 stable points. The following : The topological ordering of Point V, traversing its subsequent points, and if all the parent nodes of the subsequent points are dyed V, they are also dyed V, merged into the queue, marked as unstable points.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intN =100001;4 structNode {5     intcolor =0;6vector<int>s, p;//child node, parent node7 }a[n];8 BOOLUnstable[n];9 BOOLAll_colored (intVintcolor) {Ten     intnum =a[v].p.size (); One     BOOLFlag =true; A      for(inti =0; Flag && i < num; ++i) -Flag &= (A[a[v].p[i]].color = =color); -     returnFlag; the } - voidTopointv) { -     if(Unstable[v])return; -queue<int>Q; + Q.push (v); -A[v].color =v; +      while(!Q.empty ()) { A         intU =Q.front (); Q.pop (); at         intnum =a[u].s.size (); -          for(inti =0; i < num; ++i) { -             intson =A[u].s[i]; -             if(all_colored (son, v)) { -A[son].color =v; -Unstable[son] =true; in Q.push (son); -             } to         } +     } - } the intMain () { *     intN, K, I, V, ans =0; $scanf"%d", &n);Panax Notoginseng      for(i =1; I <= N; ++i) { -scanf"%d", &k); the          while(k--) { +scanf"%d", &v); A A[i].p.push_back (v); the A[v].s.push_back (i); +         } -     } $      for(i =1; I <= N; ++i) topo (i); $      for(i =1; I <= N; ++i) ans + =Unstable[i]; -printf"%d\n"Nans); -     return 0; the}
View Code

There are other solutions, refer to: http://www.cnblogs.com/demian/p/6536799.html

Hihocoder1343:stable members "BFS topology sequencing"

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.