Advertisement
Singasking

Untitled

Jul 5th, 2023
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1.  
  2. int dfs(int node, vector<int>& A, vector<vector<int>>& adj, vector<int>& visited, int goodCount, int C) {
  3. visited[node] = 1;
  4. int count = 0;
  5. if (adj[node].size() == 0) {
  6. if (goodCount <= C) {
  7. return 1;
  8. }
  9. return 0;
  10. }
  11. if(goodCount>C) return 0;
  12. for (auto n : adj[node]) {
  13. if (visited[n] == 0) {
  14. if (A[n] == 1) {
  15. count += dfs(n, A, adj, visited, goodCount + 1, C);
  16. }
  17. if (A[n] == 0) {
  18. count += dfs(n, A, adj, visited, goodCount, C);
  19. }
  20. }
  21. }
  22. visited[node]=0;
  23. return count;
  24. }
  25.  
  26. int Solution::solve(vector<int>& A, vector<vector<int>>& B, int C) {
  27. vector<vector<int>> adj(A.size() + 1);
  28. vector<int> visited(A.size() + 1, 0);
  29. for (auto edge : B) {
  30. adj[edge[0]].push_back(edge[1]);
  31. //adj[edge[1]].push_back(edge[0]);
  32. }
  33. return dfs(1, A, adj, visited, 0, C);
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement