Stable Members

来源:互联网 发布:如何做网站推广优化 编辑:程序博客网 时间:2024/05/18 07:07

描述

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

Every week each member sends a report of his own learning progress and the reports collected from his pupils (if there is any) to his mentors. The group is so well designed that there is no loop in the reporting chain so no one receives his own report from his pupil. 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 be easily cut off by a single member's (other than the master and himself) absence from the reporting duty. They are called unstable members while the others are stable members. Given the reporting network of the group, can you find out how many members are stable?

Assume there are 4 members in the group. Member 1 and 2 both have 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 is the only unstable member in the group because if member 3 is absent his learning report will be unable to be sent to the master. 

输入

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

The i-th line of the following N lines describe the mentors of the i-th member. The first integer is Ki, the number of mentors of the i-th member. Then follows Ki integers A1 ... AN, which are his mentors' numbers. Number 0 indicates that the master is one of his 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 the number of stable members.

样例输入
51 01 02 1 21 32 4 3 
样例输出
3


参考:http://www.cnblogs.com/demian/p/6536799.html

基本思路:有向无环图,有master做mentor的node肯定是stable的;

mentor中没有master,但是有2个以上stable的mentor,那么当前节点也是stable的;

如果只有一个mentor,那肯定是unstable的;

如果有一个以上的mentor,但是没有多于(包含)2个的stable mentor,这种情况就会很尴尬,比如下面,虽然X的2个mentor都是unstable的,但是X确是stable的


后来受http://www.cnblogs.com/demian/p/6536799.html的启发,对于每个节点,关键是要找到一个key node,这个key node的含义是:只要node A到达了A的key node,那么可以保证A一定可以达到master。

那怎么求key node呢?首先如果node本身是stable的,那么key node就是本身,如果是unstable的,那key node就是最靠近master的stable node

具体就是:如果一个节点的所有mentor node不多于2个节点是stable的,那这个node就是unstable的

于是写了个循环版本的:

public class WA {public static void main(String[] args) {Scanner sc = new Scanner(System.in);List<Set<Integer>> p = new ArrayList<Set<Integer>>(), s = new ArrayList<Set<Integer>>();int N = sc.nextInt();for(int i=0; i<=N; i++) {p.add(new HashSet<Integer>());s.add(new HashSet<Integer>());}for(int i=1; i<=N; i++) {int n = sc.nextInt();for(int j=0; j<n; j++) {int t = sc.nextInt();p.get(i).add(t);s.get(t).add(i);}}int[] keyPoint = new int[1+N];// check one by onefor(int i=1; i<=N; i++) {if(keyPoint[i] != 0)continue;keyPoint[i] = i;// consider to be stable// check son nodefor(int ss : s.get(i)) {boolean unstable = true;// check parent nodefor(int pp : p.get(ss)) {if(keyPoint[pp] != i) {unstable = false;break;}}if(unstable) {keyPoint[ss] = i; // can be certain that ss is unstable}}}int stable = 0;for(int i=1; i<=N; i++)if(keyPoint[i] == i)stable++;System.out.println(stable);}}

这写的有个问题:在最外面一层循环,我们认为这个node是stable的,及代码

keyPoint[i] = i;// consider to be stable
所以在确认为某个节点是unstable的时候要即使把后续可能是unstable的也处理掉,于是有了BFS版本的

package h178;import java.util.ArrayList;import java.util.HashSet;import java.util.LinkedList;import java.util.List;import java.util.Queue;import java.util.Scanner;import java.util.Set;/* * 1. bfs:正义也许会迟到,但一定不会忘记砸到你的头顶 * 2. bfs with topo order */public class BFS {public static void main(String[] args) {Scanner sc = new Scanner(System.in);List<Set<Integer>> p = new ArrayList<Set<Integer>>(), s = new ArrayList<Set<Integer>>();int N = sc.nextInt();for(int i=0; i<=N; i++) {p.add(new HashSet<Integer>());s.add(new HashSet<Integer>());}for(int i=1; i<=N; i++) {int n = sc.nextInt();for(int j=0; j<n; j++) {int t = sc.nextInt();p.get(i).add(t);s.get(t).add(i);}}int[] keyPoint = new int[1+N];// check one by onefor(int i=1; i<=N; i++) {if(keyPoint[i] != 0)continue;Queue<Integer> q = new LinkedList<Integer>();q.add(i);keyPoint[i] = i;// consider to be stable// check son nodewhile(!q.isEmpty()) {int j = q.remove();for(int ss : s.get(j)) {boolean unstable = true;// check parent nodefor(int pp : p.get(ss)) {if(keyPoint[pp] != i) {unstable = false;break;}}if(unstable) {keyPoint[ss] = i; // can be certain that ss is unstableq.add(ss);}}}}int stable = 0;for(int i=1; i<=N; i++)if(keyPoint[i] == i)stable++;System.out.println(stable);}}


另外一个思路是:用拓扑排序的方式确定遍历节点的顺序

package h178;import java.util.ArrayList;import java.util.HashSet;import java.util.LinkedList;import java.util.List;import java.util.Queue;import java.util.Scanner;import java.util.Set;/* * 1. bfs:正义也许会迟到,但一定不会忘记砸到你的头顶 * 2. bfs with topo order:维护outDegree(inDegree也是一样的,就看你在定义有向图时候的规定)的数组,有序的遍历节点 */public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);List<Set<Integer>> p = new ArrayList<Set<Integer>>(), s = new ArrayList<Set<Integer>>();int N = sc.nextInt();for(int i=0; i<=N; i++) {p.add(new HashSet<Integer>());s.add(new HashSet<Integer>());}int[] outDegree = new int[1+N];for(int i=1; i<=N; i++) {int n = sc.nextInt();outDegree[i] = n;for(int j=0; j<n; j++) {int t = sc.nextInt();p.get(i).add(t);s.get(t).add(i);}}int[] keyPoint = new int[1+N];Queue<Integer> qq = new LinkedList<Integer>();qq.add(0);while(!qq.isEmpty()) {int i = qq.remove();for(int ss : s.get(i)) {outDegree[ss] --;if(outDegree[ss] == 0)qq.add(ss);}if(i == 0)continue;Set<Integer> cntOfKeyPoint = new HashSet<Integer>();for(int pp : p.get(i)) {cntOfKeyPoint.add(keyPoint[pp]);}if(cntOfKeyPoint.size() != 1 || cntOfKeyPoint.iterator().next() == 0) {keyPoint[i] = i;} else {keyPoint[i] = cntOfKeyPoint.iterator().next();}}int stable = 0;for(int i=1; i<=N; i++)if(keyPoint[i] == i)stable++;System.out.println(stable);}}

好像有个Dominator Tree(支配树)的数据结构

java内存分析工具里会提到这个概念?

因为分析出支配树,就可以知道,删除某个点后,哪些点不可能访问到了,也要一并删除掉了

原创粉丝点击