Professor Q's Software Java版本

题目描述:

#1136 : Professor Q's Software

时间限制: 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.util.*;
import java.io.*;
public class Main{
	private static Map<Integer,String> map = new HashMap<Integer,String>();
	private static String[] active;
    private static long[] count;
    private static boolean[] flag;
    
    public static void countActive(int num){
    	count[num]++;
    	String tmpStr = active[num];
    	if(tmpStr=="")
    		return;
    	String[] tmpArr = tmpStr.split(" ");
    	for(int k=1;k<tmpArr.length;k++){
    		int tmpNum = Integer.parseInt(tmpArr[k]);
    		count[tmpNum]++;
    	}
    }
    
    public static void depthTravel(int num){
    	if(flag[num])
    		return;
    	
    	String tmpStr = active[num];
    	if(tmpStr==""){
    		flag[num] = true;
    		return;
    	}
    		
		String[] tmpArr = tmpStr.split(" ");
		for(int k=1;k<tmpArr.length;k++){
			int tmpNum = Integer.parseInt(tmpArr[k]);
			depthTravel(tmpNum);
			active[num] = active[num]+active[tmpNum];
		}
		flag[num] = true;
    }
	
	public static void main(String[] args){
        BufferedReader strin = 
        new BufferedReader(new InputStreamReader(System.in));
        try{
            String str = strin.readLine();
			int T = Integer.parseInt(str);
			for(int i=0;i<T;i++){
			    String str1 = strin.readLine();
				String[] strArr1 = str1.split(" ");
				int N = Integer.parseInt(strArr1[0]);
				int M = Integer.parseInt(strArr1[1]);
				
				String str2 = strin.readLine();
				String[] strArr2 = str2.split(" ");
				int[] signals = new int[M];
				for(int j=0; j<M; j++){
					signals[j] = Integer.parseInt(strArr2[j]);
				}
				
				
				map.clear();
				int[][] module = new int[N][4];
				for(int j=0; j<N; j++){
					for(int k=0; k<4; k++){
						module[j][k] = -1;
					}
					String str3 = strin.readLine();
					String[] strArr3 = str3.split(" ");
					module[j][0] = Integer.parseInt(strArr3[0]);
					
					if(!map.containsKey(module[j][0])){
					    String tmpStr = ""+j;
					    map.put(module[j][0],tmpStr);
					}else{
					    String tmpStr = map.get(module[j][0]);
					    tmpStr = tmpStr+" "+j;
					    map.put(module[j][0], tmpStr);
					}
					
					int len = Integer.parseInt(strArr3[1]);
					for(int k=1; k<len+1; k++){
						module[j][k] = Integer.parseInt(strArr3[k+1]);
					}	
				}
				
				active = new String[N];
				count = new long[N];
				flag = new boolean[N];
				for(int j=0;j<N;j++){
					active[j]="";
					for(int k=1;k<4;k++){
						if(module[j][k]==-1)
							break;
						if(map.containsKey(module[j][k])){
							String tmpStr = map.get(module[j][k]);
							active[j] = active[j]+" "+tmpStr;
						}
						
					}
				}
				
				for(int j=0;j<M;j++){
					if(map.containsKey(signals[j])){
						String tmpStr = map.get(signals[j]);
						String[] tmpArr = tmpStr.split(" ");
						for(int k=0;k<tmpArr.length;k++){
							int num = Integer.parseInt(tmpArr[k]);
							depthTravel(num);
							countActive(num);
						}
					}
				}
				
				if(N > 0){
					System.out.print(count[0]%142857);
				}
				for(int k=1; k<N; k++){
					System.out.print(" ");
					System.out.print(count[k]%142857);
				}
				System.out.print("\r\n");
				
			}
        }catch(IOException e){
            e.printStackTrace();
        }
        
    }
}


代码写的不够简洁,没有优化;但程序是正确的。虽然很早就听说过动态规划,却一直没认真学过。这次算是一次练习和学习吧。还请大家多多指教。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值