博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微软2016校园招聘在线笔试 B Professor Q's Software [ 拓扑图dp ]
阅读量:6296 次
发布时间:2019-06-22

本文共 3314 字,大约阅读时间需要 11 分钟。

 

 

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

样例输入
33 2123 256123 2 456 256456 3 666 111 256256 1 903 1100100 2 200 200200 1 300200 05 111 2 2 32 2 3 43 2 4 54 2 5 65 2 6 7
样例输出
1 1 31 2 21 1 2 3 5

题意:

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

 

题解:

来自天猫的思路。

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

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

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

 

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 14 using namespace std;15 16 typedef pair
PII;17 typedef pair
PIII;18 19 #define LL long long20 #define ULL unsigned long long21 #define m_p make_pair22 #define l_b lower_bound23 #define p_b push_back24 #define w1 first25 #define w2 second26 #define maxlongint 214748364727 #define biglongint 213906214328 29 const int maxn=100005;30 const int A=100000;31 32 int TT,N,M,o,sc,tj;33 vector
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
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

 

转载于:https://www.cnblogs.com/njczy2010/p/4391823.html

你可能感兴趣的文章
用CSS画一个带阴影的三角形
查看>>
前端Vue:函数式组件
查看>>
程鑫峰:1.26特朗.普力挺美元力挽狂澜,伦敦金行情分析
查看>>
safari下video标签无法播放视频的问题
查看>>
01 iOS中UISearchBar 如何更改背景颜色,如何去掉两条黑线
查看>>
对象的继承及对象相关内容探究
查看>>
Spring: IOC容器的实现
查看>>
Serverless五大优势,成本和规模不是最重要的,这点才是
查看>>
Nginx 极简入门教程!
查看>>
iOS BLE 开发小记[4] 如何实现 CoreBluetooth 后台运行模式
查看>>
Item 23 不要在代码中使用新的原生态类型(raw type)
查看>>
为网页添加留言功能
查看>>
JavaScript—数组(17)
查看>>
Android 密钥保护和 C/S 网络传输安全理论指南
查看>>
以太坊ERC20代币合约优化版
查看>>
Why I Began
查看>>
同一台电脑上Windows 7和Ubuntu 14.04的CPU温度和GPU温度对比
查看>>
js数组的操作
查看>>
springmvc Could not write content: No serializer
查看>>
Python系语言发展综述
查看>>