
- class Solution {
- int res = 0;
- void dfs(int src,int prev,vector
int ,int>>>& map){ - for(auto& [dest,cnt]:map[src]){
- if(dest!=prev){
- res+=cnt;
- dfs(dest,src,map);
- }
- }
- }
- public:
- int minReorder(int n, vector
int >>& connections) { - //根据connections作图,制作一个所有相邻节点两两相连的图
- //并为原connections中有的路线赋值为1,没有的赋值为0
- vector
int,int>>> map(n); - for(auto& path:connections){
- map[path[0]].push_back({path[1],1});
- map[path[1]].push_back({path[0],0});
- }
- dfs(0,-1,map);
- return res;
- }
- };