P1 : Professor Q's Software
-
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
Description
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.
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 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.
Output
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).
题意:有n个模块,每个模块都可以被一个信号激活,激活后的模块又会发出若干个信号从而激活其它模块,但不会出现循环。现在给定初始发出的m个信号,问每个模块会被运行多少次。 注意:一个信号可能激活多个模块。
思路:拓扑排序来统计每个点被遍历的次数。 注意不能以信号作为点,因为一个信号可能激活多个模块,要以模块为节点建图。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 142857
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
int degree[MAXN], flow[MAXN];
bool vis[MAXN];
vector<int> G[MAXN], out[MAXN], in[MAXN];
int main()
{
int t; Ri(t);
W(t)
{
int n, m; Ri(n); Ri(m); int signal;
for(int i = 0; i <= n; i++) G[i].clear(), out[i].clear(), in[i].clear(), degree[i] = 0, flow[i] = 0;
for(int i = 1; i <= m; i++) Ri(signal), out[0].push_back(signal);
for(int i = 1; i <= n; i++)
{
int num; Ri(signal); Ri(num); in[signal].push_back(i);
W(num) Ri(signal), out[i].push_back(signal);
}
for(int i = 0; i <= n; i++)
{
for(int j = 0; j < out[i].size(); j++)
{
signal = out[i][j];
for(int k = 0; k < in[signal].size(); k++)
{
int v = in[signal][k];
G[i].push_back(v); degree[v]++;
}
}
}
queue<int> Q;
for(int i = 0; i <= n; i++) if(degree[i] == 0) Q.push(i); flow[0] = 1;
while(!Q.empty())
{
int u = Q.front(); Q.pop(); int top = G[u].size();
for(int i = 0; i < top; i++)
{
int v = G[u][i]; flow[v] = (flow[v] + flow[u]) % MOD;
if(--degree[v] == 0) Q.push(v);
}
}
printf("%d", flow[1]);
for(int i = 2; i <= n; i++)
printf(" %d", flow[i]);
printf("\n");
}
return 0;
}