题目链接: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);
}
}
};