红黑树,BFS,DFS,pair(键值对)

红黑树{set map}

set 自带去重的有序集合     

set<int>s;

第一个元素(最小值)s.begin() 

最后一个元素(最大值)s.rbegin() 

删除  s.erase()

插入元素  s.insert()

查询集合里有没有 k s.conut(k)  s.find(k)


#include<bits/stdc++.h>
using namespace std;
int a[5]={1,4,2,2,7};
set<int>s;
int main()
{
    for(auto g:a){
        s.insert(g);
    }
    //最大值
    cout<<*s.begin()<<endl;
    //最小值
    cout<<*s.rbegin()<<endl;
    //遍历
    for(auto g:s){
        cout<<g<<" ";
    }
    //删除
    s.erase(2);
    cout <<endl;
    for(auto g:s){
        cout<<g<<" ";
    }
    return 0;
}

map 高级数组

pair 键值对

#include<bits/stdc++.h>
using namespace std;
// pair
map<pair<int,int>,int> a;
int main()
{
    int q; cin >>q;
    for (int i = 1 ; i <= q ; i++){
        int x , y , c;
        cin >> x >> y >> c;
        pair<int,int> b;
        b = make_pair(x , y);
        a[b] += c;
    }
    int m; cin >> m;
    for (int i = 1 ; i <= m ; i++){
        int x , y; cin >> x >> y;
        cout << a[make_pair(x , y)] << endl;
    }
    return 0;
}

 

关闭IO同步流,优化运行速度 ios::sync_with_stdio(false);

点少 邻接矩阵    二位数组

点多,但是边有限  矢量版邻接矩阵

vector<int> e[10005]//不带权值

vector<pair<int,int>> e[10005]//带权值

 

 

访问-遍历{DFS(深度优先遍历),BFS(广度优先遍历)}

1,DFS

递归{递归出口,干什么,转移}

#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
vector<int> e[maxn];
int bk[maxn]; // 某个点是否被访问
// 任务:输出所有可能的简单路径(不含环)
vector<int> way; // 记录当前的访问路径
void dfs(int u/*现在在哪个点*/)
{
    bk[u] = 1; // 该点已经被访问过了
    way.push_back(u);
    bool ok = false;
    for (auto v : e[u]){
        if (bk[v] == 1) continue;
        dfs(v);
        ok = true;
    }
    // 无路可走
    if (ok == false){
        for (auto g : way)
            cout << g << " ";
         cout << endl;
    }
    bk[u] = 0; // 返回之前,将改点置为0
    way.pop_back();
    return ;
}
int main()
{
    int n , m; cin >> n >> m;
    for (int i = 1 ; i <= m ; i++){
        int x , y; cin >> x >> y;
        e[x].push_back(y);
        e[y].push_back(x);
    }
    dfs(1);
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萨达大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值