传送门

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

微软2016校园招聘在线笔试 B Professor Q's Software [ 拓扑图dp ]的更多相关文章

  1. 微软2016校园招聘在线笔试-Professor Q's Software

    题目2 : Professor Q's Software 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Professor Q develops a new softw ...

  2. 微软2016校园招聘在线笔试第二场 题目1 : Lucky Substrings

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if and only if the number of different ch ...

  3. 微软2016校园招聘在线笔试 [Recruitment]

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A company plans to recruit some new employees. There are N ca ...

  4. 题目3 : Spring Outing 微软2016校园招聘在线笔试第二场

    题目3 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring outin ...

  5. 微软2016校园招聘在线笔试之Magic Box

    题目1 : Magic Box 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 The circus clown Sunny has a magic box. When ...

  6. hihocoder 1288 : Font Size (微软2016校园招聘4月在线笔试)

    hihocoder 1288 笔试第一道..wa了好几次,也是无语..hihocoder错了不会告诉你失败的时候的测试集,这样有时候就很烦.. 遍历所有的字体,从min(w,h)开始逐渐变小开始遍历. ...

  7. 微软2016校园招聘4月在线笔试 A FontSize

    题目链接:http://hihocoder.com/problemset/problem/1288 分析:题目中所求的是最大的FontSize(记为S),其应该满足P*[W/S]*[H/S] > ...

  8. 微软2016校园招聘4月在线笔试 ABC

    题目链接:http://hihocoder.com/contest/mstest2016april1/problems 第一题:输入N,P,W,H,代表有N段文字,每段有ai个字,每行有⌊W/S⌋个字 ...

  9. 微软2016校园招聘4月在线笔试 hihocoder 1289 403 Forbidden

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 Little Hi runs a web server. Sometimes he has to deny acces ...

随机推荐

  1. 【学习笔记】深入理解js原型和闭包(12)——简介【作用域】

    提到作用域,有一句话大家(有js开发经验者)可能比较熟悉:“javascript没有块级作用域”.所谓“块”,就是大括号“{}”中间的语句.例如if语句: 再比如for语句: 所以,我们在编写代码的时 ...

  2. 如何配置TomCat

    1.先查看你自己java的jdk的版本号 2.通过jdk版本号确定下载的Tomcat版本 ,因为我的是jdk 1.8的,所以要下载Tomcat 8版本 附上下载官网http://tomcat.apac ...

  3. 初探ABP--记一些常见的开发问题

    1.Update-Database : 无法将“Update-Database”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次. ...

  4. SAP CRM点了附件的超链接后报错的处理方式

    SAP CRM系统里,点击了附件的这些超链接后,如果是文本文件,会在浏览器里打开.如果是其他类型的文件,会弹出下载对话框. 然而最近我工作时遇到一个问题,点击超链接后,总是弹出Logon failed ...

  5. DROP DATABASE - 删除一个数据库

    SYNOPSIS DROP DATABASE name DESCRIPTION 描述 DROP DATABASE 删除一个现存数据库的目录入口并且删除包含数据的目录. 只有数据库所有者能够执行这条命令 ...

  6. 《少年先疯队》第九次团队作业:Beta冲刺第三天

    3.1 今日完成任务情况 姚玉婷:酒店系统中剩余功能的完善 马丽莎:酒店系统中管理员功能的测试 张   琼:酒店系统中会员功能的测试 孙苗坤:酒店系统中其余管理功能的测试文档的编写 3.2 成员贡献时 ...

  7. iview table icon dorpdown html页面级别vue组件 #vuez#

    iview table icon dorpdown html页面级别vue组件 <!DOCTYPE html> <html> <head> <meta cha ...

  8. Hadoop伪集群部署

    环境准备 [root@jiagoushi ~]# yum -y install lrzsz 已加载插件:fastestmirror Repository 'saltstack-repo': Error ...

  9. 小程序01 微信小程序介绍和开发准备

    前言 火爆的微信小程序:跳一跳.摩拜单车.美柚.大众点评.滴滴出行 背景 为什么会有小程序? 微信最早推出公众号和服务号,公众号和服务号所传播的网页经常出现加载缓慢甚至页面空白的情况. 微信小程序的诞 ...

  10. 多线程test

    import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import ...