2016微软实习在线笔试题 - 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.
signals.png

输入
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

  • 方法一:使用栈进行处理,该方法逻辑简单,容易写代码
#include <vector>
#include <stack>
#include <iostream>

using namespace std;

void find (int** a, int len, int b, vector<int>& next);

int main()
{
    int T = 0;
    int N = 0, M = 0;
    cin >> T;

    for(int i = 0; i < T; ++i)
    {
        // N: module num; M: init num
        cin >> N >> M;

        // get the init
        int* init = new int[M]();
        for (int j = 0; j < M; ++j)
            cin >> init[j];

        // get the modules
        int** mods = new int*[N];
        for (int j = 0; j < N; ++j)
        {
            int modId = 0, childNum = 0; 
            cin >> modId >> childNum;

            mods[j] = new int[childNum + 2]();
            mods[j][0] = modId;
            mods[j][1] = childNum;
            for (int k = 0; k < childNum; ++k)
                cin >> mods[j][k+2];
        }

        int* cnt = new int[N]();
        stack<int> s;
        vector<int> next;
        for (int j = 0; j < M; ++j)
            s.push(init[j]);
        while (!s.empty())
        {
            next.clear();
            find(mods, N, s.top(), next);
            s.pop();
            for (vector<int>:: iterator iter = next.begin(); iter != next.end(); ++iter)
            {
                cnt[*iter]++;
                for (int j = 0; j < mods[*iter][1]; ++j)
                    s.push(mods[*iter][2+j]);
            }
        }

        for (int j = 0; j < N; ++j)
            cout << cnt[j] << " ";
        cout << endl;

        // release memory
        delete[] init;
        for (int j = 0; j < N; ++j)
            delete[] mods[j];
        delete[] cnt;
    }

}

void find (int** a, int len, int b, vector<int>& next)
{
    for (int i = 0; i < len; ++i)
    {
        if (b == a[i][0])
            next.push_back(i);
    }
}
  • 方法二:最简单直接的查找方法,这个要求写代码时要留意别处理出错。
#include <vector>
#include <iostream>

using namespace std;

void get (int** a, int len, int line, int* cnt);
void find (int** a, int len, int b, vector<int>& next);

int main()
{
    int T = 0;
    int N = 0, M = 0;
    cin >> T;

    for(int i = 0; i < T; ++i)
    {
        // N: module num; M: init num
        cin >> N >> M;

        // get the init
        int* init = new int[M]();
        for (int j = 0; j < M; ++j)
            cin >> init[j];

        // get the modules
        int** mods = new int*[N];
        for (int j = 0; j < N; ++j)
        {
            int modId = 0, childNum = 0; 
            cin >> modId >> childNum;

            mods[j] = new int[childNum + 2]();
            mods[j][0] = modId;
            mods[j][1] = childNum;
            for (int k = 0; k < childNum; ++k)
                cin >> mods[j][k+2];
        }

        int* cnt = new int[N]();
        for (int j = 0; j < M; ++j)
        {
            vector<int> next;
            find(mods, N, init[j], next);
            for (vector<int>::iterator iter = next.begin(); iter != next.end(); ++iter)
            {
                    cnt[*iter]++;
                    get (mods, N, *iter, cnt);
            }
        }

        for (int j = 0; j < N; ++j)
            cout << cnt[j] << " ";
        cout << endl;
    }

}

void get (int** a, int len, int line, int* cnt)
{
    for (int i = 0; i < a[line][1]; ++i)
    {
        vector<int> next;
        find(a, len, a[line][i+2], next);
        for (vector<int>::iterator iter = next.begin(); iter != next.end(); ++iter)
        {
            cnt[*iter]++;
            get(a, len, *iter, cnt);
        }
    }
}

void find (int** a, int len, int b, vector<int>& next)
{
    for (int i = 0; i < len; ++i)
    {
        if (b == a[i][0])
            next.push_back(i);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值