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

直接使用bfs即可:

 #include <iostream>
#include <cstdlib>
#include <vector>
#include <unordered_map>
#include <queue> using namespace std; class Module {
public:
int fanin;
int access;
vector<int> fanout;
Module():access(){}
}; int main() { int T, N, M; scanf("%d", &T); for (int i=; i<T; i++) {
scanf("%d%d", &N, &M); // signal slot
unordered_map<int, vector<int> > singal_slot; // module & signal
vector<Module> mods(N);
queue<int> signals; // inital signals
for (int i=; i<M; i++) {
int ts;
scanf("%d", &ts);
signals.push(ts);
} // read module define
for (int i=; i<N; i++) {
int S, K;
scanf("%d%d", &S, &K);
if (singal_slot.count(S) == ) {
// create slot first
singal_slot.insert(make_pair(S, vector<int>()));
} // slot must exist now, register mod
vector<int>& wired_mods = singal_slot[S];
wired_mods.push_back(i); // add fanout signal for current mod
mods[i].fanin = S;
vector<int>& outsig = mods[i].fanout;
for (int i=; i<K; i++) {
int os;
scanf("%d", &os);
outsig.push_back(os);
}
} while (!signals.empty()) {
int sig = signals.front();
signals.pop();
//printf("read signal %d\n", sig);
// retrive the fired mods from signal slot
if (singal_slot.count(sig) == ) {
// no module fired by this signal
continue;
}
vector<int>& ms = singal_slot[sig]; int mlen = ms.size(); for (int i=; i<mlen; i++) {
Module& mod = mods[ms[i]];
// access it
mod.access++;
// fanout signal
vector<int>& outsigs = mod.fanout;
for (int i=; i<outsigs.size(); i++) {
//printf("fanout: %d\n", outsigs[i]);
signals.push(outsigs[i]);
}
}
}
// one iteration over
if (N) {
printf("%d", mods[].access);
}
for (int i=; i<N; i++) {
printf(" %d", mods[i].access);
}
printf("\n");
} //system("pause");
return ;
}

微软2016校园招聘在线笔试-Professor Q's Software的更多相关文章

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

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

  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. python random 之基础点名器

    import os ,sysimport randomcalled =set() # 创建一个空集合f =open('name.txt ' , 'r')#打开文件读取名字data =f.read()# ...

  2. IOS----UIScrollerView的使用

    刚刚遛狗回来,前段时间创建的这篇博客一直没有填充内容,今天把scrollerview正好整理一下. 1.scrollerview的主要作用:当界面显示不开要显示的内容,scrollerview提供了滑 ...

  3. Java中常用到的文件操作那些事(一)——替换doc文档模板,生成真实合同案例

    工作中,我们时常会遇到一些操作文件的操作,比如在线生成合同模板,上传/下载/解析Excel,doc文档转为pdf等操作.本文就已工作中遇到的在线生成合同为例,简要地介绍一种文档替换写法. 本文目的:给 ...

  4. 使用python 模仿mybinlog 命令 二进制分析mysql binlog

    出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该声明. ...

  5. Java多线程——不变性与安全发布

    1.不变性 某个对象在被创建后其状态就不能被修改,那么这个对象就称为不可变对象,不可变对象一定是线程安全的.不可变对象很简单.他们只有一种状态,并且该状态由构造函数来控制. 当满足以下条件时,对象才是 ...

  6. GeneXus学习笔记——入门篇

    使用GeneXus做开发做了有一段时间了 却发现一个问题(O_O)?就是除了相关的Wiki外 网上其他地方的相关资料都很少 于是乎我就想在这记录一些东西 来帮助以后会用到的人(°ー°") 那 ...

  7. *args and **kwargs

    首先要知道, 并不是必须写成*args 和**kwargs. 只有变量前面的 *(星号)才是必须的. 你也可以写成*var 和**vars. 而写成*args 和**kwargs只是一个通俗的命名约定 ...

  8. Mac 10.12安装Google浏览器

    说明:先安装旧版本后续再升级,主要是资源难找. 下载: (链接: https://pan.baidu.com/s/1eROfQyY 密码: n6ij)

  9. DiagnosticFormatter

    关于这个类的继承体系如下: 1.DiagnosticFormatter类在com.sun.tools.javac.api包中,其定义如下: /** * Provides simple function ...

  10. JavaScript设计模式-16.装饰者模式(上)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...