jayati

Reorder Routes to Make All Paths Lead to the City Zero

May 27th, 2024
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int count = 0;
  4.     void dfs(int node, int parent, vector<vector<pair<int, int>>>& adj) {
  5.         for (auto& [child, sign] : adj[node]) {
  6.             if (child != parent) {
  7.                 count += sign;
  8.                 dfs(child, node, adj);
  9.             }
  10.         }
  11.     }
  12.  
  13.     int minReorder(int n, vector<vector<int>>& connections) {
  14.         vector<vector<pair<int, int>>> adj(n);
  15.         for (auto& connection : connections) {
  16.             adj[connection[0]].push_back({connection[1], 1});
  17.             adj[connection[1]].push_back({connection[0], 0});
  18.         }
  19.         dfs(0, -1, adj);
  20.         return count;
  21.     }
  22. };
Add Comment
Please, Sign In to add comment