微软2016校园招聘在线笔试-Professor Q's Software

题目2 : 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

直接使用bfs即可:

  1 #include <iostream>
  2 #include <cstdlib>
  3 #include <vector>
  4 #include <unordered_map>
  5 #include <queue>
  6 
  7 using namespace std;
  8 
  9 class Module {
 10 public:
 11     int fanin;
 12     int access;
 13     vector<int> fanout;
 14     Module():access(0){}
 15 };
 16 
 17 
 18 int main() {
 19 
 20     int T, N, M;
 21     
 22     scanf("%d", &T);
 23 
 24     for (int i=0; i<T; i++) {
 25         scanf("%d%d", &N, &M);
 26         
 27         // signal slot
 28         unordered_map<int, vector<int> > singal_slot;
 29         
 30         // module & signal
 31         vector<Module> mods(N);
 32         queue<int> signals;
 33         
 34         // inital signals
 35         for (int i=0; i<M; i++) {
 36             int ts;
 37             scanf("%d", &ts);
 38             signals.push(ts);
 39         }
 40 
 41         // read module define
 42         for (int i=0; i<N; i++) {
 43             int S, K;
 44             scanf("%d%d", &S, &K);
 45             if (singal_slot.count(S) == 0) {
 46                 // create slot first
 47                 singal_slot.insert(make_pair(S, vector<int>()));
 48             }
 49             
 50             // slot must exist now, register mod
 51             vector<int>& wired_mods = singal_slot[S];
 52             wired_mods.push_back(i);
 53             
 54             // add fanout signal for current mod
 55             mods[i].fanin = S;
 56             vector<int>& outsig = mods[i].fanout;
 57             for (int i=0; i<K; i++) {
 58                 int os;
 59                 scanf("%d", &os);
 60                 outsig.push_back(os);
 61             }
 62         }
 63 
 64         while (!signals.empty()) {
 65             int sig = signals.front();
 66             signals.pop();
 67             //printf("read signal %d
", sig);
 68             // retrive the fired mods from signal slot
 69             if (singal_slot.count(sig) == 0) {
 70                 // no module fired by this signal
 71                 continue;
 72             }
 73             vector<int>& ms = singal_slot[sig];
 74             
 75             int mlen = ms.size();
 76             
 77             for (int i=0; i<mlen; i++) {
 78                 Module& mod = mods[ms[i]];
 79                 // access it
 80                 mod.access++;
 81                 // fanout signal
 82                 vector<int>& outsigs = mod.fanout;
 83                 for (int i=0; i<outsigs.size(); i++) {
 84                     //printf("fanout: %d
", outsigs[i]);
 85                     signals.push(outsigs[i]);
 86                 }
 87             }
 88         }
 89         // one iteration over
 90         if (N) {
 91             printf("%d", mods[0].access);
 92         }
 93         for (int i=1; i<N; i++) {
 94             printf(" %d", mods[i].access);
 95         }
 96         printf("
");
 97     }
 98 
 99     //system("pause");
100     return 0;
101 }
原文地址:https://www.cnblogs.com/lailailai/p/4393090.html