传递信息

题目链接:https://leetcode-cn.com/problems/chuan-di-xin-xi/
题目描述:
传递信息
题解:


class Solution {
public:
    int result = 0;
    int numWays(int n, vector<vector<int>>& relation, int k) {
       vector<vector<int>> edge(n);
        //存储为图结构
        for(auto &iter: relation)
        {
           edge[iter[0]].push_back(iter[1]);
        }
        for(int i = 0; i < edge[0].size(); i++)
        {
            dfs(edge,edge[0][i], k - 1 , n);
        }
        
        return result;
        
    }
    void dfs(vector<vector<int>>& edge, int index, int k, int n)
    {
        if(k == 0)
        {
            if(index == n -1)
                result++;
            return;
        }
            
        for(int i = 0; i < edge[index].size(); i++)
        {
            dfs(edge, edge[index][i], k-1, n);
        }
    }
};

上一篇:pytorch从零搭建神经网络实现多分类(Fashion-Mnist)


下一篇:python知识——迭代器和生成器