hihoCoder 1136-Professor Q’s Software #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
此题需要厘清模块和信号之间的关系,一个信号可以触发多个模块,一个模块也可以产生多个信号。利用空间换时间,再搜索的时候快速找出关系。使用广度优先搜索解决。 完整代码如下: [cpp] #include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<queue> using namespace std; const int kMaxNMSE = 100005, kMaxK = 5, kMod = 142857; int t, n, m, s, k; int ans[kMaxNMSE]; int signals[kMaxNMSE]; vector<vector<int>> model_signals; vector<vector<int>> signal_models; queue<int> sgls; void Init() { memset(ans, 0, sizeof(ans)); memset(signals, 0, sizeof(signals)); model_signals.clear(); model_signals.resize(kMaxNMSE); signal_models.clear(); signal_models.resize(kMaxNMSE); } void BFS(int sgl) { sgls.push(sgl); while (!sgls.empty()) { int tmp = sgls.front(); sgls.pop(); if (signal_models[tmp].size()) { for (int i = 0; i < signal_models[tmp].size(); i++) { int mdl = signal_models[tmp][i]; ans[mdl]++; for (int j = 0; j < model_signals[mdl].size(); j++) sgls.push(model_signals[mdl][j]); } } } } int main() { //freopen("input.txt", "r", stdin); scanf("%d", &t); while (t–) { Init(); scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) scanf("%d", &signals[i]); for (int i = 1; i <= n; i++) { scanf("%d %d", &s, &k); signal_models[s].push_back(i); int tmp; while (k–) { scanf("%d", &tmp); model_signals[i].push_back(tmp); } } for (int i = 1; i <= m; i++) BFS(signals[i]); for (int i = 1; i <= n; i++) printf("%d ", ans[i] % kMod); printf("\n"); } return 0; } [/cpp] 本代码提交AC,用时899MS,内存24MB。]]>