Leetcode 2192. 有向无环图中一个节点的所有祖先 逆向建图+DFS/拓扑排序

news/2024/7/20 21:57:48 标签: 深度优先, leetcode, 算法, 图论, 数据结构

原题链接:Leetcode 2192. 有向无环图中一个节点的所有祖先
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    vector<vector<int>> adj;
    vector<vector<int>> res;
    vector<int> indegree;
    map<pair<int,int>,int> mp;
    void dfs(int now)
    {
        for(auto x:adj[now])
        {
            if(!mp[{now,x}])
            {
                res[now].push_back(x);
                mp[{now,x}]=1;
            }
            if(res[x].size()==0) dfs(x);
            for(auto y:res[x])
            {
                if(!mp[{now,y}])
                {
                    res[now].push_back(y);
                    mp[{now,y}]=1;
                }
            }
        }
    }
    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {
        res.resize(n);
        adj.resize(n);
        indegree.resize(n);
        for(auto x:edges)
        {
            int a=x[0],b=x[1];
            adj[b].push_back(a);
            indegree[a]++;
        }
        for(int i=0;i<n;i++)
        {
            if(!indegree[i])   dfs(i);
        }
        for(int i=0;i<n;i++) sort(res[i].begin(),res[i].end());
        return res;
    }
};

拓扑排序

class Solution {
public:
    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {
        vector<vector<int>> adj(n);
        vector<vector<int>> res(n);
        vector<int> indegree(n);
        vector<set<int>> pre(n);
        for(auto x:edges)
        {
            int a=x[0],b=x[1];
            adj[a].push_back(b);
            indegree[b]++;
        }
        queue<int> q; 
        for(int i=0;i<n;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(int i=0;i<n;i++)
        {
            res[i].insert(res[i].end(),pre[i].begin(),pre[i].end());
        }
        return res;
    }
};

http://www.niftyadmin.cn/n/1264510.html

相关文章

激活函数学习记录

激活函数&#xff1a; 使用激活函数的原因&#xff1a; 神经网络中每一层的输入输出都是一个线性求和的过程&#xff0c;下一层的输出只是承接了上一层输入函数的线性变换&#xff0c;如果没有激活函数&#xff0c;无论构造的神经网络多么复杂&#xff0c;有多少层&#xff0c;…

关于pytorch构建神经网络细节的学习记录

pytorch搭建简单的神经网络案例&#xff1a; Pytorch–1.使用Pytorch搭建一个简易的神经网络 对于前向传播与反向传播的理解&#xff1a;神经网络前向传播和反向传播公式推导&#xff08;公式图解&#xff09; 关于forward函数调用的理解&#xff1a; pytorch的学习之路&am…

python常用函数与机器学习基础知识记录

np.unique() 交叉熵理解&#xff1a;SOFTMAX_CROSS_ENTROPY,BINARY_CROSS_ENTROPY,SIGMOID_CROSS_ENTROPY简介 神经网络优化器讲解 神经网络中的优化器 (tensorflow2.0) PyTorch&#xff1a;torch.nonzero——非零元素的定位 【学习笔记】 pytorch中squeeze与unsqueeze函数…

如何利用DGL官方库中的rgcn链接预测代码跑自己的数据集(如何在DGL库的链接预测数据集模块定义自己的数据集类)

最近在忙我的省创&#xff0c;是有关于知识图谱的&#xff0c;其中有一个内容是使用rgcn的链接预测方法跑自己的数据集&#xff0c;我是用的dgl库中给出的在pytorch环境下实现rgcn的链接预测的代码&#xff0c;相关链接贴在这里&#xff1a; dgl库中关于rgcn的介绍文档 dgl库…

Leetcode 1334. 阈值距离内邻居最少的城市 Dijkstra/Floyd

原题链接&#xff1a;Leetcode 1334. 阈值距离内邻居最少的城市 Dijkstra class Solution { public:vector<vector<pair<int,int>>> adj;vector<int> visit;vector<int> dis;void dijkstra(int n,int d){dis[d]0;for(int i0;i<n;i){int u-1,…

Leetcode 1311. 获取你好友已观看的视频 DFS/BFS+map自定义排序

原题链接&#xff1a;Leetcode 1311. 获取你好友已观看的视频 map自定义排序 参考&#xff1a;C容器map可以排序吗&#xff1f; static bool cmp(const pair<string,int> &a,const pair<string,int> &b) {if(a.secondb.second) return a.first<b.first…

Leetcode 1557. 可以到达所有点的最少点数目 拓扑排序

原题链接&#xff1a;Leetcode 1557. 可以到达所有点的最少点数目 这也太简单了吧。。。。我不敢相信 class Solution { public:vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) {vector<int> indegree(n);for(auto x…

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

原题链接&#xff1a;1462. 课程表 IV DFS反向构图 这个做法是参考了这道题&#xff1a;Leetcode 2192. 有向无环图中一个节点的所有祖先 逆向建图DFS class Solution { public:vector<vector<int>> adj;map<pair<int,int>,int> mp;vector<vector…