传送门

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

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

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <vector>
#include <queue> using namespace std; typedef pair<int,int> PII;
typedef pair<int,PII> PIII; #define LL long long
#define ULL unsigned long long
#define m_p make_pair
#define l_b lower_bound
#define p_b push_back
#define w1 first
#define w2 second
#define maxlongint 2147483647
#define biglongint 2139062143 const int maxn=;
const int A=; int TT,N,M,o,sc,tj;
vector<int> F[maxn];
int c[maxn],a[maxn],ans[maxn],vis[maxn],dp[maxn],inp[maxn]; void dfs(int s)
{
if (vis[s]==) return;
vis[s]=;
for (int i=;i<F[s].size();i++)
dfs(F[s][i]);
++o,ans[o]=s;
} int main()
{
scanf("%d",&TT);
for (int gb=;gb<=TT;gb++)
{
scanf("%d %d",&N,&M);
for (int i=;i<=M;i++) scanf("%d",&c[i]);
memset(inp,,sizeof(inp));
for (int i=;i<=A;i++) F[i].clear();
for (int i=;i<=N;i++)
{
scanf("%d",&a[i]);
scanf("%d",&sc);
for (int j=;j<=sc;j++)
{
scanf("%d",&tj);
if (tj>A) continue;
F[a[i]].p_b(tj);
++inp[tj];
}
}
o=;
memset(vis,,sizeof(vis));
for (int i=;i<=A;i++)
if (inp[i]==) dfs(i);
memset(dp,,sizeof(dp));
for (int i=;i<=M;i++) dp[c[i]]++;
for (int i=A+;i>=;i--)
{
sc=ans[i];
for (int j=;j<F[sc].size();j++)
dp[F[sc][j]]+=dp[sc],dp[F[sc][j]]%=;
}
for (int i=;i<N;i++) printf("%d ",dp[a[i]]);printf("%d\n",dp[a[N]]);
}
return ;
}

最新文章

  1. adminLTE的自动化菜单
  2. IEEE参考文献格式
  3. Note_Master-Detail Application(iOS template)_02_YJYAppDelegate.m
  4. Keil MDK 无法设置断点【worldsing】
  5. 原生javascript 改写的tab选项卡
  6. 《cracking the coding intreview》——链表
  7. 4.Apache Spark的工作原理
  8. jQuery各类选择器
  9. i的二次幂求和
  10. java任意n以内连续的和等于n
  11. mysql5.6更改datadir数据存储目录
  12. JAVA AES CBC 加密 解密
  13. Ubuntu安装VirtualBox以及CentOS7.5联网设置
  14. Excel分组快速自动填充编号
  15. [福大2018高级软工教学]团队Beta阶段成绩汇总
  16. 禁止浏览器缓存js
  17. 访问kubernetes ingress-controller
  18. 数据库SQL中Like的用法总结
  19. Set 集合论
  20. Navicat Premium 12 激活

热门文章

  1. 获得select被选中option的value和text
  2. IF-MIB::ifTable = No Such Object available on this agent at this OID
  3. linux下自定义pid实现任意数据采集
  4. IOStableviewsectionSet
  5. 推荐一个以动画效果显示github提交记录的黑科技工具:Gource
  6. (转)为Spring集成的Hibernate配置二级缓存
  7. laravel扩展包服务提供者的注册的两种方式
  8. pip install python-igraph 报错,C core of igraph 没有安装。
  9. Windows Server 2012 R2 with Update (x64) - DVD (Chinese-Simplified)
  10. [BZOJ3211]:花神游历各国(小清新线段树)