classSolution { public: intnumWays(int n, vector<vector<int>>& r, int k){ unordered_map<int, unordered_set<int>> m; for (auto& x : r) { int a = x[0], b = x[1];
if (m.count(a)) { m[a].insert(b); } else { unordered_set<int> s; s.insert(b); m[a] = s; } } queue<int> q; q.push(0); while (q.size() && k-- > 0) { int len = q.size(); while (len--) { auto t = q.front(); q.pop(); if (!m.count(t)) continue; auto s = m[t]; for (int ne : s) { q.push(ne); } } } int ans = 0; while (q.size()) { if (q.front() == n - 1) ans++; q.pop(); } return ans; } };