forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
46 lines (33 loc) · 983 Bytes
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// Source : https://leetcode-cn.com/problems/chuan-di-xin-xi/
/// Author : liuyubobobo
/// Time : 2020-04-18
#include <iostream>
#include <vector>
#include <set>
using namespace std;
/// Memory Search
/// Time Complexity: O(n*n*k)
/// Space Complexity: O(n*k)
class Solution {
public:
int numWays(int n, vector<vector<int>>& relation, int k) {
vector<set<int>> g(n);
for(const vector<int>& arc: relation)
g[arc[0]].insert(arc[1]);
vector<vector<int>> dp(n, vector<int>(k + 1, -1));
return dfs(g, 0, n - 1, k, dp);
}
private:
int dfs(const vector<set<int>>& g, int start, int end, int step,
vector<vector<int>>& dp){
if(step == 0) return start == end;
if(dp[start][step] != -1) return dp[start][step];
int res = 0;
for(int next: g[start])
res += dfs(g, next, end, step - 1, dp);
return dp[start][step] = res;
}
};
int main() {
return 0;
}