微软2016校园招聘在线笔试 B Professor Q's Software [ 拓扑图dp ]

传送门

 

 

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

题意:

一个有向无环图,初始访问某些点,访问过的点会沿着能连的边一直走到底,问,最后每个点分别被访问了几次。

 

题解:

来自天猫的思路。

拓扑图dp。一个很好的思路~~~

从根节点开始,如果某个节点访问了,它的所有儿子节点访问数+1。

由于是按照拓扑顺序来处理的(并且没有环),所以,在继续对儿子的儿子处理时,不会再出现儿子节点再增加访问数。

 

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <string>
 6 #include <cstdlib>
 7 #include <algorithm>
 8 #include <map>
 9 #include <set>
10 #include <utility>
11 #include <vector>
12 #include <queue>
13 
14 using namespace std;
15 
16 typedef pair<int,int> PII;
17 typedef pair<int,PII> PIII;
18 
19 #define LL long long
20 #define ULL unsigned long long
21 #define m_p make_pair
22 #define l_b lower_bound
23 #define p_b push_back
24 #define w1 first
25 #define w2 second
26 #define maxlongint 2147483647
27 #define biglongint 2139062143
28 
29 const int maxn=100005;
30 const int A=100000;
31 
32 int TT,N,M,o,sc,tj;
33 vector<int> F[maxn];
34 int c[maxn],a[maxn],ans[maxn],vis[maxn],dp[maxn],inp[maxn];
35 
36 void dfs(int s)
37 {
38     if (vis[s]==1) return;
39     vis[s]=1;
40     for (int i=0;i<F[s].size();i++)
41         dfs(F[s][i]);
42     ++o,ans[o]=s;
43 }
44 
45 int main()
46 {
47     scanf("%d",&TT);
48     for (int gb=1;gb<=TT;gb++)
49     {
50         scanf("%d %d",&N,&M);
51         for (int i=1;i<=M;i++) scanf("%d",&c[i]);
52         memset(inp,0,sizeof(inp));
53         for (int i=0;i<=A;i++) F[i].clear();
54         for (int i=1;i<=N;i++)
55         {
56             scanf("%d",&a[i]);
57             scanf("%d",&sc);
58             for (int j=1;j<=sc;j++)
59             {
60                 scanf("%d",&tj);
61                 if (tj>A) continue;
62                 F[a[i]].p_b(tj);
63                 ++inp[tj];
64             }
65         }
66         o=0;
67         memset(vis,0,sizeof(vis));
68         for (int i=0;i<=A;i++)
69             if (inp[i]==0) dfs(i);
70         memset(dp,0,sizeof(dp));
71         for (int i=1;i<=M;i++) dp[c[i]]++;
72         for (int i=A+1;i>=1;i--)
73         {
74             sc=ans[i];
75             for (int j=0;j<F[sc].size();j++)
76                 dp[F[sc][j]]+=dp[sc],dp[F[sc][j]]%=142857;
77         }
78         for (int i=1;i<N;i++) printf("%d ",dp[a[i]]);printf("%d\n",dp[a[N]]);
79     }
80     return 0;
81 }

 

posted on 2015-04-04 11:17  njczy2010  阅读(731)  评论(0编辑  收藏  举报