其他分享
首页 > 其他分享> > HZNU-ACM寒假集训Day1小结

HZNU-ACM寒假集训Day1小结

作者:互联网

   常用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) {   //递归实现
    if (pre[x] != x) x = Find(pre[x]);  //路径压缩
    return pre[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;
}

 

标签:pre,node,return,int,HZNU,ACM,Day1,include,Find
来源: https://www.cnblogs.com/hznumqf/p/12236698.html