Leetcode 1462. 课程表 IV DFS+反向构图/Floyd/拓扑排序

原题链接:1462. 课程表 IV

Leetcode 1462. 课程表 IV DFS+反向构图/Floyd/拓扑排序_第1张图片
Leetcode 1462. 课程表 IV DFS+反向构图/Floyd/拓扑排序_第2张图片
DFS+反向构图

这个做法是参考了这道题:Leetcode 2192. 有向无环图中一个节点的所有祖先 逆向建图+DFS

class Solution {
public:
    vector<vector<int>> adj;
    map<pair<int,int>,int> mp;
    vector<vector<int>> rec;
    void dfs(int now)
    {
        for(auto x:adj[now])
        {
            if(!mp[{now,x}])
            {
                mp[{now,x}]=1;
                rec[now].push_back(x);
            }
            if(rec[x].size()==0) dfs(x);
            for(auto y:rec[x])
            {
                if(!mp[{now,y}])
                {
                    rec[now].push_back(y);
                    mp[{now,y}]=1;
                }
            }
        }
    }
    vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>>& prerequisites, vector<vector<int>>& queries) {
        vector<bool> res;
        if(prerequisites.size()==0)
        {
            for(int i=0;i<queries.size();i++) res.push_back(false);
            return res;
        }
        adj.resize(numCourses);
        rec.resize(numCourses);
        for(auto x:prerequisites)   adj[x[1]].push_back(x[0]);
        for(auto x:queries)
        {
            if(mp[{x[1],x[0]}]) res.push_back(true);
            else 
            {
                dfs(x[1]);
                if(mp[{x[1],x[0]}]) res.push_back(true);
                else res.push_back(false);
            }
        }
        return res;

    }
};

Floyd 参考:1462. c++几乎双百的预计算解法

class Solution {
public:
    vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>>& prerequisites, vector<vector<int>>& queries) {
        vector<bool> res;
        if(prerequisites.size()==0)
        {
            for(int i=0;i<queries.size();i++) res.push_back(false);
            return res;
        }
        vector<vector<bool>> g(numCourses,vector<bool>(numCourses,false));
        for(auto x:prerequisites)   g[x[0]][x[1]]=true;
        for(int k=0;k<numCourses;k++)
        {
            for(int i=0;i<numCourses;i++)
            {
                for(int j=0;j<numCourses;j++)
                {
                    if(g[i][k]&&g[k][j]) g[i][j]=true;
                }
            }
        }
        for(auto x:queries) res.push_back(g[x[0]][x[1]]);
        return res;
    }
};

拓扑排序: 参考:简单易懂的拓扑排序!

class Solution {
public:
    vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>>& prerequisites, vector<vector<int>>& queries) {
        vector<bool> res;
        if(prerequisites.size()==0)
        {
            for(int i=0;i<queries.size();i++) res.push_back(false);
            return res;
        }
        vector<vector<int>> adj(numCourses);
        vector<int> indegree(numCourses);
        vector<set<int>> pre(numCourses);
        for(auto x:prerequisites)   
        {
            int a=x[0],b=x[1];
            adj[a].push_back(b);
            indegree[b]++;
        }
        queue<int> q; 
        for(int i=0;i<numCourses;i++)
        {
            if(!indegree[i]) q.push(i);
        }
        while(!q.empty())
        {
            int now=q.front(); q.pop();
            for(auto x:adj[now])
            {
                indegree[x]--;
                pre[x].insert(pre[now].begin(),pre[now].end());
                pre[x].insert(now);
                if(!indegree[x]) q.push(x);
            }
        }
        for(auto x:queries) 
        {
            if(pre[x[1]].find(x[0])!=pre[x[1]].end()) res.push_back(true);
            else res.push_back(false);
        }
        return res;
    }
};

你可能感兴趣的:(Leetcode,深度优先,leetcode,算法,图论,力扣)