2015 Microsoft Internship Online PEN test-Professor Q ' s software

Source: Internet
Author: User

Time limit: 10000ms
Single point time limit: 1000ms
Memory Limit: 256MB
Describe
Professor Q develops a new software. The software consists of n modules which is numbered from 1 to N. The i-th module would be started up by signal Si. If signal Si is generated multiple times, the I-th module'll also be started multiple times. The modules different is started up by the same signal. During its lifecircle, the i-th module would generate Ki signals:e1, E2, ..., EKi. These signals may, start up, modules and so on. Fortunately the software is so carefully designed that there are no loop in the starting chain of modules, which means even Tually all the modules would be stoped. Professor Q generates some initial signals and want to know what many times each module is started.

Input
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, numbers N and M, indicating the number of modules and number of S Ignals 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 is the start up this module. K represents the total amount of signals that is generated during the lifecircle of this module. and E1 ... EK is 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 <=, 0 <= K <= 3, 0 <= S, E <= 105.

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

Output
For each test case, the output a line with N numbers Ans1, Ans2, ..., ansn. Ansi is the number of the Times, the I-th module is started. In case the answers is too large, output the answers modulo 142857 (the remainder of division by 142857).

Sample input
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
Sample output
1 1 3
1 2 2
1 1 2) 3 5

    • Method one: Using the stack for processing, the method is simple logic, easy to write code
#include <vector>#include <stack>#include <iostream>using namespace STD;voidFind (int* * A,intLenintB vector<int>& next);intMain () {intT =0;intN =0, M =0;Cin>> T; for(inti =0; i < T; ++i) {//N:module num; M:init Num        Cin>> N >> M;//Get the init        int* init =New intM (); for(intj =0; J < M; ++J)Cin>> Init[j];//Get the modules        int* * Mods =New int*[n]; for(intj =0; J < N; ++J) {intModid =0, Childnum =0;Cin>> Modid >> Childnum; MODS[J] =New int[Childnum +2](); mods[j][0] = Modid; mods[j][1] = Childnum; for(intK =0; K < Childnum; ++K)Cin>> mods[j][k+2]; }int* CNT =New intN (); Stack<int>S vector<int>Next for(intj =0; J < M; ++J) S.push (Init[j]); while(!s.empty ())            {next.clear ();            Find (mods, N, S.top (), next); S.pop (); for( vector<int>:: Iterator iter = Next.begin (); Iter! = Next.end (); ++iter) {cnt[*iter]++; for(intj =0; J < mods[*iter][1]; ++J) S.push (mods[*iter][2+J]); }        } for(intj =0; J < N; ++J)cout<< Cnt[j] <<" ";cout<< Endl;//Release memory        Delete[] init; for(intj =0; J < N; ++J)Delete[] mods[j];Delete[] CNT; }}voidFind (int* * A,intLenintB vector<int>& Next) { for(inti =0; i < Len; ++i) {if(b = = a[i][0]) Next.push_back (i); }}
    • Method Two: The simplest and most straightforward method of finding, this requires the code to be careful not to handle errors.
#include <vector>#include <iostream>using namespace STD;voidGet (int* * A,intLenintLineint* CNT);voidFind (int* * A,intLenintB vector<int>& next);intMain () {intT =0;intN =0, M =0;Cin>> T; for(inti =0; i < T; ++i) {//N:module num; M:init Num        Cin>> N >> M;//Get the init        int* init =New intM (); for(intj =0; J < M; ++J)Cin>> Init[j];//Get the modules        int* * Mods =New int*[n]; for(intj =0; J < N; ++J) {intModid =0, Childnum =0;Cin>> Modid >> Childnum; MODS[J] =New int[Childnum +2](); mods[j][0] = Modid; mods[j][1] = Childnum; for(intK =0; K < Childnum; ++K)Cin>> mods[j][k+2]; }int* CNT =New intN (); for(intj =0; J < M; ++J) { vector<int>Next Find (mods, N, Init[j], next); for( vector<int>:: Iterator iter = Next.begin (); Iter! = Next.end ();                    ++iter) {cnt[*iter]++;            Get (mods, N, *iter, CNT); }        } for(intj =0; J < N; ++J)cout<< Cnt[j] <<" ";cout<< Endl; }}voidGet (int* * A,intLenintLineint* cnt) { for(inti =0; I < a[line][1]; ++i) { vector<int>Next Find (A, Len, a[line][i+2], next); for( vector<int>:: Iterator iter = Next.begin (); Iter! = Next.end ();            ++iter) {cnt[*iter]++;        Get (A, Len, *iter, CNT); }    }}voidFind (int* * A,intLenintB vector<int>& Next) { for(inti =0; i < Len; ++i) {if(b = = a[i][0]) Next.push_back (i); }}

2015 Microsoft Internship Online PEN test-Professor Q ' s software

Related Article

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.