Leetcode 1466. 重新规划路线 正向+逆向DFS/BFS

news/2024/7/20 22:13:24 标签: 深度优先, leetcode, 宽度优先, 算法, 图论

原题链接:Leetcode 1466. 重新规划路线

在这里插入图片描述
在这里插入图片描述
BFS

class Solution {
public:
    vector<vector<int>> adj;
    vector<vector<int>> in;
    vector<int> visit;
    int minReorder(int n, vector<vector<int>>& connections) {
        adj.resize(n);
        visit.resize(n);
        in.resize(n);
        for(auto x:connections)
        {
            int a=x[0],b=x[1];
            adj[a].push_back(b);
            in[b].push_back(a);
        }
        int res=0;
        queue<int> q; 
        q.push(0);
        visit[0]=1;
        while(!q.empty())
        {
            int now=q.front(); q.pop();
            for(auto x:adj[now])
            {
                if(!visit[x])
                {
                    res++;
                    visit[x]=1;
                    q.push(x);
                }
            }
            for(auto x:in[now])
            {
                if(!visit[x])
                {
                    visit[x]=1;
                    q.push(x);
                }
            }
        }
        return res;
    }
};

DFS

class Solution {
public:
    vector<vector<int>> adj;
    vector<vector<int>> in;
    vector<int> visit;
    int res=0;
    void dfs(int now)
    {
        visit[now]=1;
        for(auto x:adj[now])
        {
            if(!visit[x])
            {
                res++;
                dfs(x);
            }
        } 
        for(auto x:in[now])
        {
            if(!visit[x]) dfs(x);
        }
    }
    int minReorder(int n, vector<vector<int>>& connections) {
        adj.resize(n);
        visit.resize(n);
        in.resize(n);
        for(auto x:connections)
        {
            int a=x[0],b=x[1];
            adj[a].push_back(b);
            in[b].push_back(a);
        }
        dfs(0);
        return res;
    }
};

http://www.niftyadmin.cn/n/1264493.html

相关文章

Leetcode 809. 情感丰富的文字 字符串模拟

原题链接&#xff1a; 这个代码写的真丑 不过好歹是写完了。。。 可是我被自己丑到&#xff0c;而且我还懒得改了&#xff0c;回去洗澡 class Solution { public:int expressiveWords(string s, vector<string>& words) {vector<int> nums;string s1;for(in…

小程序根据vant weapp官网引入时,找不到路径问题

错误如下 官网组件路径 "usingComponents": { "van-button": "vant/weapp/button/index" } 在查看自己组件路径后&#xff0c;将其改为 "usingComponents": { "van-button": "vant/weapp/button" } 成功&…

校园二手平台——微信小程序

由于课程需要&#xff0c;所以学着做了一个小程序&#xff0c;用的是云数据库&#xff0c;没有使用云函数&#xff0c;具体思路如下 源码在这里&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1YFWfqYA719Kx07m3Ytjsdg?pwd8osz 提取码&#xff1a;8osz 一、功能设…

Leetcode 2477. 到达首都的最少油耗 dfs+贪心

原题链接&#xff1a;Leetcode 2477. 到达首都的最少油耗 计算节点的子节点数&#xff0c;使用贪心策略&#xff0c;让一辆车尽量载更多的人 class Solution { public:vector<vector<int>> adj;long long res0;long long dfs(int now,int seats,int pre){long lon…

Module parse failed:Unexpected token (9:6)

在学习vue打包时&#xff0c;发生错误 反复寻找&#xff0c;发现是文件里多写了个export&#xff0c;删掉就好了。

Leetcode 959. 由斜杠划分区域 DFS+模拟/并查集

原题链接&#xff1a;Leetcode 959. 由斜杠划分区域 解法一&#xff1a;将一个方块看成3x3的矩阵&#xff0c;使用dfs判断有多少块区域&#xff0c;参考&#xff1a; [C] [动画] 转换成岛屿个数 class Solution { public:void dfs(vector<vector<int>>& g,int…

sqlalchemy.orm.exc.UnmappedInstanceError: Class ‘builtins.NoneType‘ is not mapped

错误的为&#xff1a; message Message.query.filter(message_id Message.id) db.session.delete(message) db.session.commit() 更改为&#xff1a; message Message.query.get(message_id) db.session.delete(message) db.session.commit() 先用get通过id号获取记录&…

Leetcode 1971. 寻找图中是否存在路径 DFS/BFS

原题链接&#xff1a;Leetcode 1971. 寻找图中是否存在路径 DFS class Solution { public:vector<vector<int>> adj;vector<int> visit;bool flagfalse;void dfs(int now,int destination){if(nowdestination) flagtrue;visit[now]1;for(auto x:adj[now]){…