zoukankan      html  css  js  c++  java
  • HZNUACM寒假集训Day1小结 STL 并查集

       常用STL

           1.优先队列 priority_queue

                内部是用堆(heap)实现的

                priority_queue<int> pq; 默认为一个“越小的整数优先级越低的优先队列”

                对于一些常见的优先队列,STL提供了更简单的定义方法 例如:“越小的整数优先级越大的优先队列”可以写成“priority_queue<int,vector<int>,greater<int> >pq"

               自定义优先级 

    struct Node {
        int x, y;
        friend bool operator < (const Node& a, const Node& b) {
            return a.x < b.x;   //用小于号,表示小的先出列
        }
    };
    priority_queue<Node> q;

               2.map  

                 内部是一颗红黑树

                 map的遍历

    map<string, int> mp;
    for (auto it = mp.begin(); it != mp.end(); it++) {
        if (it->second > max) {
            max = it->second; ss = it->first;
        }
    }

                3.接口 erase() 和 count(),empty()要比count()快

                4.  4.pair<>类型可以简化代码 

                二叉树的遍历

                节点

    struct treeNode {
        string data;
    
        treeNode* left;
        treeNode* right;
    
        treeNode() {
            data = "";
            left = NULL;
            right = NULL;
        }
    };

               1.先序遍历

    void PreOrder(treeNode* node) {
        if (node == NULL) return;
        cout << node->data<<"->";
    
        if (node->left != NULL) {
            PreOrder(node->left);
        }
        if (node->right != NULL) {
            PreOrder(node->right);
        }
    }

              2.中序遍历

    void InOrder(treeNode* node) {
        if (node == NULL) return;
        
        if (node->left != NULL) {
            InOrder(node->left);
        }
    
        cout << node->data << "->";
    
        if (node->right != NULL) {
            InOrder(node->right);
        }
    }

             3.后序遍历

               

    void PosOrder(treeNode* node) {
        if (node == NULL) return;
    
        if (node->left != NULL) {
            PosOrder(node->left);
        }
        if (node->right != NULL) {
            PosOrder(node->right);
        }
        cout << node->data << "->";
    }

      并查集

        并查集用于集合联通和查询

        两个核心函数 Union 和Find

       

    int Find(int x) {   //递归实现
        return pre[x] != x ? pre[x] = Find(pre[x]) : x;
    }
    int Find() {
        int p, tmp;
        p = x;
        while (x != pre[x]) x = pre[x];
        while (p != x) {      //路径压缩
            tmp = pre[p];     //tmp暂存p的父节点
            pre[p] = x;       //pre[p]指向祖先节点
            p = tmp;
        }
        return x;
    }
    void Union(int x, int y) {
        int p = Find(x); int q = Find(y);
        if (p != q) pre[q] = p;
    }

       畅通工程  http://acm.hdu.edu.cn/showproblem.php?pid=1232

       

    #include<iostream>
    using namespace std;
    int pre[1003];
    int Find(int x) {
        if (x == pre[x])    return x;
        else {
            pre[x] = Find(pre[x]);
            return pre[x];
        }
    }
    int Union(int x, int y) {
        int p = Find(x);
        int q = Find(y);
        if (q != p) {
            pre[q] = p; return 1;
        }
        return 0;
    }
    int main() {
        std::ios::sync_with_stdio(0);
        cin.tie(0);
        int n, m;
        int x, y;
        while(scanf("%d",&n)!=EOF){
            if (n == 0) break;
            scanf("%d", &m);
            int ans = n - 1;
        for (int i = 1; i <=n; i++) {
            pre[i] = i;
        }
        for (int i = 0; i < m; i++) {
            scanf("%d%d", &x, &y);
            ans-=Union(x, y);
        }
        if (ans >= 0) printf("%d\n", ans);
        else printf("0\n");
        }
        return 0;
    }

       How Many Tables http://acm.hdu.edu.cn/showproblem.php?pid=1213

        

    #include<iostream>
    #include<string>
    #include<cmath>
    #include<cstring>
    #include<vector>
    #include<map>
    #include<set>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<sstream>
    #pragma warning(disable:4996) 
    const int maxn = 1e6 + 5;
    const double PI = acos(-1.0);
    typedef long long ll;
    using namespace std;
    int tot;
    void read() {
        char ch;
        while ((ch = getchar()) != '\n');
    }
    
    int pre[1005];
    int Find(int x) {
        if (x == pre[x])    return x;
        else {
            pre[x] = Find(pre[x]);
            return pre[x];
        }
    }
    void Union(int x,int y) {
        int p = Find(x);
        int q = Find(y);
        if (q != p) {
            pre[q] = p; 
            tot--;
        }
    }
    int main() {
        std::ios::sync_with_stdio(0);
        cin.tie(0);
        int n, m,k;
        int t,x,y;
        cin >> t;
        while (t--) {
            cin >> n >> m;
            tot = n - 1;
            for (int i = 1; i <= n; i++) pre[i] = i;
            for (int i = 0; i < m; i++) {
                cin >> x >> y;
                Union(x, y);
            }
            cout << tot+1<< "\n";
        }
        return 0;
    }
  • 相关阅读:
    python 打印出水仙花数
    pycharm 2020 激活码 破解教程
    Python 封装一个函数,查找文字字符串数字英文下标
    Python 分解质因数
    python 封装一个取符串长度的函数
    Python 正整数相加其余忽略
    Python 输入字符串找(String)下标 没有返回-1
    CPU 和 GPU 的区别
    Deferred Shading,延迟渲染(提高渲染效率,减少多余光照计算)【转】
    BumpMap、NormalMap的区别
  • 原文地址:https://www.cnblogs.com/hznumqf/p/12236698.html
Copyright © 2011-2022 走看看