Leikou Check-in 2020.1.12 Project Management

Topic: The
company has n projects and m teams in total, and each project is either taken over by no one or one of the m teams is responsible.
group[i] represents the group to which the i-th project belongs. If no one currently takes over this project, then group[i] is equal to -1. (Projects and teams are numbered from scratch) The team may not take over any projects.
Please help arrange the progress of these projects as required, and return to the sorted list of projects: projects in the
same group will be next to each other in the list after sorting.
There are certain dependencies between projects, we use a list beforeItems to represent, where beforeItems[i] represents all the items that should be completed before the i-th project (located on the left of the i-th project).
If there are multiple solutions, just return any one of them. If there is no suitable solution, please return an empty list.
Example 1:
Input: n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5], [6],[3,6],[],[],[]]
Output: [6,3,4,1,5,2,0,7]
Example 2:
Input: n = 8, m = 2 , group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[ 4],[]]
Output: []
Explanation: It is roughly the same as example 1, but in the sorted list, 4 must be placed in front of 6.

Code (topological sort):

class Solution {
    
    
public:
    vector<int> topSort(vector<int>& deg, vector<vector<int>>& graph, vector<int>& items) {
    
    
        queue<int> Q;
        for (auto& item: items) {
    
    
            if (deg[item] == 0) {
    
    
                Q.push(item);
            }
        }
        vector<int> res;
        while (!Q.empty()) {
    
    
            int u = Q.front(); 
            Q.pop();
            res.emplace_back(u);
            for (auto& v: graph[u]) {
    
    
                if (--deg[v] == 0) {
    
    
                    Q.push(v);
                }
            }
        }
        return res.size() == items.size() ? res : vector<int>{
    
    };
    }

    vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {
    
    
        vector<vector<int>> groupItem(n + m);

        // 组间和组内依赖图
        vector<vector<int>> groupGraph(n + m);
        vector<vector<int>> itemGraph(n);

        // 组间和组内入度数组
        vector<int> groupDegree(n + m, 0);
        vector<int> itemDegree(n, 0);
        
        vector<int> id;
        for (int i = 0; i < n + m; ++i) {
    
    
            id.emplace_back(i);
        }

        int leftId = m;
        // 给未分配的 item 分配一个 groupId
        for (int i = 0; i < n; ++i) {
    
    
            if (group[i] == -1) {
    
    
                group[i] = leftId;
                leftId += 1;
            }
            groupItem[group[i]].emplace_back(i);
        }
        // 依赖关系建图
        for (int i = 0; i < n; ++i) {
    
    
            int curGroupId = group[i];
            for (auto& item: beforeItems[i]) {
    
    
                int beforeGroupId = group[item];
                if (beforeGroupId == curGroupId) {
    
    
                    itemDegree[i] += 1;
                    itemGraph[item].emplace_back(i);   
                } else {
    
    
                    groupDegree[curGroupId] += 1;
                    groupGraph[beforeGroupId].emplace_back(curGroupId);
                }
            }
        }

        // 组间拓扑关系排序
        vector<int> groupTopSort = topSort(groupDegree, groupGraph, id); 
        if (groupTopSort.size() == 0) {
    
    
            return vector<int>{
    
    };
        } 
        vector<int> ans;
        // 组内拓扑关系排序
        for (auto& curGroupId: groupTopSort) {
    
    
            int size = groupItem[curGroupId].size();
            if (size == 0) {
    
    
                continue;
            }
            vector<int> res = topSort(itemDegree, itemGraph, groupItem[curGroupId]);
            if (res.size() == 0) {
    
    
                return vector<int>{
    
    };
            }
            for (auto& item: res) {
    
    
                ans.emplace_back(item);
            }
        }
        return ans;
    }
};

Guess you like

Origin blog.csdn.net/weixin_45780132/article/details/112505952