題目2 : Professor Q's Software【微軟2016校園招聘在線筆試】

時間限制:10000ms
單點時限:1000ms
內存限制:256MB

描述

Professor Q develops a new software. The software consists of N modules which are numbered from 1 to N. The i-th module will be started up by signal Si. If signal Si is generated multiple times, the i-th module will also be started multiple times. Two different modules may be started up by the same signal. During its lifecircle, the i-th module will generate Ki signals: E1, E2, ..., EKi. These signals may start up other modules and so on. Fortunately the software is so carefully designed that there is no loop in the starting chain of modules, which means eventually all the modules will be stoped. Professor Q generates some initial signals and want to know how many times each module is started.


輸入

The first line contains an integer T, the number of test cases. T test cases follows.

For each test case, the first line contains contains two numbers N and M, indicating the number of modules and number of signals that Professor Q generates initially.

The second line contains M integers, indicating the signals that Professor Q generates initially.

Line 3~N + 2, each line describes an module, following the format S, K, E1, E2, ... , EK. S represents the signal that start up this module. K represents the total amount of signals that are generated during the lifecircle of this module. And E1 ... EK are these signals.

For 20% data, all N, M <= 10
For 40% data, all N, M <= 103
For 100% data, all 1 <= T <= 5, N, M <= 105, 0 <= K <= 3, 0 <= S, E <= 105.

Hint: HUGE input in this problem. Fast IO such as scanf and BufferedReader are recommended.

輸出

For each test case, output a line with N numbers Ans1, Ans2, ... , AnsN. Ansi is the number of times that the i-th module is started. In case the answers may be too large, output the answers modulo 142857 (the remainder of division by 142857).

樣例輸入
3
3 2
123 256
123 2 456 256
456 3 666 111 256
256 1 90
3 1
100
100 2 200 200
200 1 300
200 0
5 1
1
1 2 2 3
2 2 3 4
3 2 4 5
4 2 5 6
5 2 6 7
樣例輸出
1 1 3
1 2 2
1 1 2 3 5


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	public static void main(String args[]) throws IOException{
/*		Scanner sc = null;
		try {
			sc = new Scanner(new FileInputStream("D://desktop//test.txt"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		Scanner sc = new Scanner(System.in);
		int testCase = sc.nextInt();
		
		while(testCase-->0){
			Map<Integer,List<Integer>> map = new HashMap<>();
			Map<Integer,Integer> res = new HashMap<>();
			Map<Integer,Integer> keyNum = new HashMap<>();
			int n = sc.nextInt();
			int m = sc.nextInt();
			Queue<Integer> que = new LinkedList<>();
			for(int i=0;i<m;i++){
				que.offer(sc.nextInt());
			}
			int sys[] = null;
			if(n!=0){
				sys = new int[n];
			}
			for(int i=0;i<n;i++){
				sys[i]= sc.nextInt();
				if(keyNum.containsKey(sys[i])){
					int row = sc.nextInt();
					List<Integer> list = map.get(sys[i]);
					for(int j=0;j<row;j++){
						list.add(sc.nextInt());
					}
					res.put(sys[i], 0);
					map.put(sys[i], list);
					keyNum.put(sys[i], keyNum.get(sys[i])+1);
				}else{
					int row = sc.nextInt();
					List<Integer> list = new ArrayList<>();
					for(int j=0;j<row;j++){
						list.add(sc.nextInt());
					}
					res.put(sys[i], 0);
					map.put(sys[i], list);
					keyNum.put(sys[i], 1);
				}

			}
			while(!que.isEmpty()){
				int in = que.poll();
				if(res.containsKey(in)){
					res.put(in, res.get(in)+1);
					List<Integer> val = map.get(in);
					for(int i=0;i<val.size();i++){
						que.offer(val.get(i));
					}
				}
			}
			if(sys==null){
				System.out.print(0);
			}else{
				for (int i = 0; i < sys.length - 1; i++) {
					System.out.print(res.get(sys[i]) + " ");
				}
				System.out.print(res.get(sys[sys.length - 1]));
			}
	
			System.out.println();
		}
	}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章