【笔试】每天工作量

每项工作可能会有前置工作,有n项工作,有序,给出他们的前置工作,问在n天中每天的工作量是多少。

第i天必须完成第i项工作,比如,设工作有a1,a2,...,ai,第一天必须完成a1,如果a1有前置工作,是a3和a4,a3和a4没有前置工作,则第一天必须完成a1, a3, a4,相应的,在第3天、第四天的工作量可以是0.

输入:n项工作,然后给出第i项工作的前置工作个数+前置工作的id

输出:每天的工作量

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <cmath>

using namespace std;
struct Node{
    Node() {
        touch = false;
    }
    vector<Node*> next;
    bool touch;
};
class Solution {
public:
    void touch(Node* node) {
        if(node->touch)
            return;
        if(!node->touch) {
            node->touch = true;
            for(auto pre: node->next) {
                if(!pre->touch)
                    touch(pre);
            }
        }
    }
    void getPartition(vector<Node*> &nodes) {
        int pre = 0;
        for(auto node:nodes) {
            touch(node);
            int count = 0;
            for (auto node: nodes) {
                if(node->touch) count++;
            }
            cout << count-pre << ' ';
            pre = count;
        }
        cout << endl;
    }
};

int main() {
    int n;
    cin >> n;
    vector<Node*> nodes;
    for (int i = 0; i < n; ++i) {
        nodes.emplace_back(new Node);
    }
    for (int i = 0; i < n; ++i) {
        int a;
        scanf("%d", &a);
        for (int j = 0; j < a; ++j) {
            int tmp;
            scanf("%d", &tmp);
            nodes[i]->next.emplace_back(nodes[tmp-1]);
        }
    }
    Solution s;
    s.getPartition(nodes);
    for (int i = 0; i < nodes.size(); ++i) {
        delete nodes[i];
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值