Professor Q's Software Java版本

来源:互联网 发布:如何ping mac地址 编辑:程序博客网 时间:2024/06/08 08:10

题目描述:

#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).

样例输入
33 2123 256123 2 456 256456 3 666 111 256256 1 903 1100100 2 200 200200 1 300200 05 111 2 2 32 2 3 43 2 4 54 2 5 65 2 6 7
样例输出
1 1 31 2 21 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();        }            }}


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


0 0
原创粉丝点击