每项工作可能会有前置工作,有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];
}
}