【算法笔记自学】第 9 章 提高篇(3)——数据结构专题(2)

news2024/11/15 11:19:12

9.1树与二叉树

#include <cstdio>

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    printf(n == m + 1 ? "Yes" : "No");
    return 0;
}

9.2二叉树的遍历

#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 50;

struct Node {
    int l, r;
} nodes[MAXN];

vector<int> pre;

void preOrder(int root) {
    if (root == -1) {
        return;
    }
    pre.push_back(root);
    preOrder(nodes[root].l);
    preOrder(nodes[root].r);
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &nodes[i].l, &nodes[i].r);
    }
    preOrder(0);
    for (int i = 0; i < (int)pre.size(); i++) {
        printf("%d", pre[i]);
        if (i < (int)pre.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 50;

struct Node {
    int l, r;
} nodes[MAXN];

vector<int> pre;

void preOrder(int root) {
    if (root == -1) {
        return;
    }
    preOrder(nodes[root].l);
    pre.push_back(root);
    preOrder(nodes[root].r);
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &nodes[i].l, &nodes[i].r);
    }
    preOrder(0);
    for (int i = 0; i < (int)pre.size(); i++) {
        printf("%d", pre[i]);
        if (i < (int)pre.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 50;

struct Node {
    int l, r;
} nodes[MAXN];

vector<int> pre;

void preOrder(int root) {
    if (root == -1) {
        return;
    }
    preOrder(nodes[root].l);
    preOrder(nodes[root].r);
    pre.push_back(root);
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &nodes[i].l, &nodes[i].r);
    }
    preOrder(0);
    for (int i = 0; i < (int)pre.size(); i++) {
        printf("%d", pre[i]);
        if (i < (int)pre.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

const int MAXN = 50;

struct Node {
    int l, r;
} nodes[MAXN];

vector<int> layer;

void layerOrder(int root) {
    queue<int> q;
    q.push(root);
    while (!q.empty()) {
        int front = q.front();
        q.pop();
        layer.push_back(front);
        if (nodes[front].l != -1) {
            q.push(nodes[front].l);
        }
        if (nodes[front].r != -1) {
            q.push(nodes[front].r);
        }
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &nodes[i].l, &nodes[i].r);
    }
    layerOrder(0);
    for (int i = 0; i < (int)layer.size(); i++) {
        printf("%d", layer[i]);
        if (i < (int)layer.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 50;

struct Node {
    int l, r;
} nodes[MAXN];

int getHeight(int root) {
    if (root == -1) {
        return 0;
    }
    int leftHeight = getHeight(nodes[root].l);
    int rightHeight = getHeight(nodes[root].r);
    return max(leftHeight, rightHeight) + 1;
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &nodes[i].l, &nodes[i].r);
    }
    printf("%d", getHeight(0));
    return 0;
}

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

const int MAXN = 50;

struct Node {
    int l, r;
} nodes[MAXN];

int layers[MAXN];

void layerOrder(int root) {
    queue<int> q;
    q.push(root);
    int layer = 1;
    while (!q.empty()) {
        int cnt = q.size();
        for (int i = 0; i < cnt; i++) {
            int front = q.front();
            q.pop();
            layers[front] = layer;
            if (nodes[front].l != -1) {
                q.push(nodes[front].l);
            }
            if (nodes[front].r != -1) {
                q.push(nodes[front].r);
            }
        }
        layer++;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &nodes[i].l, &nodes[i].r);
    }
    layerOrder(0);
    for (int i = 0; i < n; i++) {
        printf("%d", layers[i]);
        if (i < n - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 50;

struct Node {
    int l, r;
} nodes[MAXN];

vector<int> pre, in, post;

int buildTree(int preL, int preR, int inL, int inR) {
    if (preL > preR) {
        return -1;
    }
    int root = pre[preL];
    int inIndexOfRoot;
    for (int i = inL; i <= inR; i++) {
        if (in[i] == root) {
            inIndexOfRoot = i;
            break;
        }
    }
    int leftCount = inIndexOfRoot - inL;
    nodes[root].l = buildTree(preL + 1, preL + leftCount, inL, inIndexOfRoot - 1);
    nodes[root].r = buildTree(preL + leftCount + 1, preR, inIndexOfRoot + 1, inR);
    return root;
}

void postOrder(int root) {
    if (root == -1) {
        return;
    }
    postOrder(nodes[root].l);
    postOrder(nodes[root].r);
    post.push_back(root);
}

int main() {
    int n, x;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &x);
        pre.push_back(x);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", &x);
        in.push_back(x);
    }
    int root = buildTree(0, n - 1, 0, n - 1);
    postOrder(root);
    for (int i = 0; i < (int)post.size(); i++) {
        printf("%d", post[i]);
        if (i < (int)post.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 50;

struct Node {
    int l, r;
} nodes[MAXN];

vector<int> pre, in, post;

int buildTree(int postL, int postR, int inL, int inR) {
    if (postL > postR) {
        return -1;
    }
    int root = post[postR];
    int inIndexOfRoot;
    for (int i = inL; i <= inR; i++) {
        if (in[i] == root) {
            inIndexOfRoot = i;
            break;
        }
    }
    int leftCount = inIndexOfRoot - inL;
    nodes[root].l = buildTree(postL, postL + leftCount - 1, inL, inIndexOfRoot - 1);
    nodes[root].r = buildTree(postL + leftCount, postR - 1, inIndexOfRoot + 1, inR);
    return root;
}

void preOrder(int root) {
    if (root == -1) {
        return;
    }
    pre.push_back(root);
    preOrder(nodes[root].l);
    preOrder(nodes[root].r);
}

int main() {
    int n, x;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &x);
        post.push_back(x);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", &x);
        in.push_back(x);
    }
    int root = buildTree(0, n - 1, 0, n - 1);
    preOrder(root);
    for (int i = 0; i < (int)pre.size(); i++) {
        printf("%d", pre[i]);
        if (i < (int)pre.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <map>
#include <vector>
using namespace std;

const int MAXN = 50;

struct Node {
    int l, r;
} nodes[MAXN];

vector<int> pre, in, layer;

int buildTree(vector<int> layer, int inL, int inR) {
    if (layer.empty()) {
        return -1;
    }
    int root = layer[0];
    map<int, bool> isLeft;
    int inIndexOfRoot;
    for (int i = inL; i <= inR; i++) {
        if (in[i] == root) {
            inIndexOfRoot = i;
            break;
        } else {
            isLeft[in[i]] = true;
        }
    }
    vector<int> leftLayer, rightLayer;
    for (int i = 1; i < layer.size(); i++) {
        if (isLeft[layer[i]]) {
            leftLayer.push_back(layer[i]);
        } else {
            rightLayer.push_back(layer[i]);
        }
    }
    nodes[root].l = buildTree(leftLayer, inL, inIndexOfRoot - 1);
    nodes[root].r = buildTree(rightLayer, inIndexOfRoot + 1, inR);
    return root;
}

void preOrder(int root) {
    if (root == -1) {
        return;
    }
    pre.push_back(root);
    preOrder(nodes[root].l);
    preOrder(nodes[root].r);
}

int main() {
    int n, x;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &x);
        layer.push_back(x);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", &x);
        in.push_back(x);
    }
    int root = buildTree(layer, 0, n - 1);
    preOrder(root);
    for (int i = 0; i < (int)pre.size(); i++) {
        printf("%d", pre[i]);
        if (i < (int)pre.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 50;

struct Node {
    int data;
    int l, r;
} nodes[MAXN];

int treePathSum = 0;

void getTreePathSum(int root, int nodePathSum) {
    if (root == -1) {
        return;
    }
    nodePathSum += nodes[root].data;
    if (nodes[root].l == -1 && nodes[root].r == -1) {
        treePathSum += nodePathSum;
    } else {
        getTreePathSum(nodes[root].l, nodePathSum);
        getTreePathSum(nodes[root].r, nodePathSum);
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &nodes[i].data);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &nodes[i].l, &nodes[i].r);
    }
    getTreePathSum(0, 0);
    printf("%d", treePathSum);
    return 0;
}

#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 50;

struct Node {
    int data;
    int l, r;
} nodes[MAXN];

int treeWeightedPathLength = 0;

void getTreeWeightedPathLength(int root, int nodePathLength) {
    if (root == -1) {
        return;
    }
    if (nodes[root].l == -1 && nodes[root].r == -1) {
        treeWeightedPathLength += nodes[root].data * nodePathLength;
    } else {
        nodePathLength++;
        getTreeWeightedPathLength(nodes[root].l, nodePathLength);
        getTreeWeightedPathLength(nodes[root].r, nodePathLength);
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &nodes[i].data);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &nodes[i].l, &nodes[i].r);
    }
    getTreeWeightedPathLength(0, 0);
    printf("%d", treeWeightedPathLength);
    return 0;
}

9.3树的遍历

#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 50;

struct Node {
    vector<int> children;
} nodes[MAXN];

vector<int> pre;

void preOrder(int root) {
    pre.push_back(root);
    for (int i = 0; i < nodes[root].children.size(); i++) {
        preOrder(nodes[root].children[i]);
    }
}

int main() {
    int n, k, child;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &child);
            nodes[i].children.push_back(child);
        }
    }
    preOrder(0);
    for (int i = 0; i < pre.size(); i++) {
        printf("%d", pre[i]);
        if (i < (int)pre.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 50;

struct Node {
    vector<int> children;
} nodes[MAXN];

vector<int> post;

void postOrder(int root) {
    for (int i = 0; i < nodes[root].children.size(); i++) {
        postOrder(nodes[root].children[i]);
    }
    post.push_back(root);
}

int main() {
    int n, k, child;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &child);
            nodes[i].children.push_back(child);
        }
    }
    postOrder(0);
    for (int i = 0; i < post.size(); i++) {
        printf("%d", post[i]);
        if (i < (int)post.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

const int MAXN = 50;

struct Node {
    vector<int> children;
} nodes[MAXN];

vector<int> layer;

void layerOrder(int root) {
    queue<int> q;
    q.push(root);
    while (!q.empty()) {
        int front = q.front();
        q.pop();
        layer.push_back(front);
        for (int i = 0; i < nodes[front].children.size(); i++) {
            q.push(nodes[front].children[i]);
        }
    }
}

int main() {
    int n, k, child;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &child);
            nodes[i].children.push_back(child);
        }
    }
    layerOrder(0);
    for (int i = 0; i < layer.size(); i++) {
        printf("%d", layer[i]);
        if (i < (int)layer.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 50;

struct Node {
    int data;
    vector<int> children;
} nodes[MAXN];

int treePathSum = 0;

void getTreePathSum(int root, int nodePathSum) {
    nodePathSum += nodes[root].data;
    if (nodes[root].children.empty()) {
        treePathSum += nodePathSum;
    }
    for (int i = 0; i < nodes[root].children.size(); i++) {
        getTreePathSum(nodes[root].children[i], nodePathSum);
    }
}

int main() {
    int n, k, child;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &nodes[i].data);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &child);
            nodes[i].children.push_back(child);
        }
    }
    getTreePathSum(0, 0);
    printf("%d", treePathSum);
    return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 50;

struct Node {
    int data;
    vector<int> children;
} nodes[MAXN];

int treePathLength = 0;

void getTreePathLength(int root, int edgeCount) {
    if (nodes[root].children.empty()) {
        treePathLength += nodes[root].data * edgeCount;
    }
    for (int i = 0; i < nodes[root].children.size(); i++) {
        getTreePathLength(nodes[root].children[i], edgeCount + 1);
    }
}

int main() {
    int n, k, child;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &nodes[i].data);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &child);
            nodes[i].children.push_back(child);
        }
    }
    getTreePathLength(0, 0);
    printf("%d", treePathLength);
    return 0;
}

9.4二叉查找树(BST)

#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 50;

struct Node {
    int data;
    int l, r;
} nodes[MAXN];

int nodeCount = 0;

int newNode(int data) {
    nodes[nodeCount].data = data;
    nodes[nodeCount].l = nodes[nodeCount].r = -1;
    return nodeCount++;
}

int insert(int root, int data) {
    if (root == -1) {
        return newNode(data);
    }
    if (data < nodes[root].data) {
        nodes[root].l = insert(nodes[root].l, data);
    } else {
        nodes[root].r = insert(nodes[root].r, data);
    }
    return root;
}

vector<int> pre;

void preOrder(int root) {
    if (root == -1) {
        return;
    }
    pre.push_back(nodes[root].data);
    preOrder(nodes[root].l);
    preOrder(nodes[root].r);
}

int main() {
    int n, data, root = -1;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &data);
        root = insert(root, data);
    }
    preOrder(root);
    for (int i = 0; i < (int)pre.size(); i++) {
        printf("%d", pre[i]);
        if (i < (int)pre.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <vector>
using namespace std;

vector<int> in;

bool isBST() {
    for (int i = 1; i < in.size(); i++) {
        if (in[i] <= in[i - 1]) {
            return false;
        }
    }
    return true;
}

int main() {
    int n, x;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &x);
        in.push_back(x);
    }
    printf(isBST() ? "Yes" : "No");
    return 0;
}

9.5平衡二叉树(AVL树)

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 50;

struct Node {
    int data;
    int height;
    int l, r;
} nodes[MAXN];

int nodeCount = 0;

int newNode(int data) {
    nodes[nodeCount].data = data;
    nodes[nodeCount].height = 1;
    nodes[nodeCount].l = nodes[nodeCount].r = -1;
    return nodeCount++;
}

int getHeight(int root) {
    if (root == -1) {
        return 0;
    } else {
        return nodes[root].height;
    }
}

void updateHeight(int root) {
    nodes[root].height = max(getHeight(nodes[root].l), getHeight(nodes[root].r)) + 1;
}

int getBalanceFactor(int root) {
    return getHeight(nodes[root].l) - getHeight(nodes[root].r);
}

int insert(int root, int data) {
    if (root == -1) {
        return newNode(data);
    }
    if (data < nodes[root].data) {
        nodes[root].l = insert(nodes[root].l, data);
    } else {
        nodes[root].r = insert(nodes[root].r, data);
    }
    updateHeight(root);
    return root;
}

vector<int> balanceFactor;

void inOrder(int root) {
    if (root == -1) {
        return;
    }
    inOrder(nodes[root].l);
    balanceFactor.push_back(getBalanceFactor(root));
    inOrder(nodes[root].r);
}

int main() {
    int n, data, root = -1;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &data);
        root = insert(root, data);
    }
    inOrder(root);
    for (int i = 0; i < (int)balanceFactor.size(); i++) {
        printf("%d", balanceFactor[i]);
        if (i < (int)balanceFactor.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 50;

struct Node {
    int data;
    int height;
    int l, r;
} nodes[MAXN];

int nodeCount = 0;

int newNode(int data) {
    nodes[nodeCount].data = data;
    nodes[nodeCount].height = 1;
    nodes[nodeCount].l = nodes[nodeCount].r = -1;
    return nodeCount++;
}

int getHeight(int root) {
    if (root == -1) {
        return 0;
    } else {
        return nodes[root].height;
    }
}

void updateHeight(int root) {
    nodes[root].height = max(getHeight(nodes[root].l), getHeight(nodes[root].r)) + 1;
}

int getBalanceFactor(int root) {
    return getHeight(nodes[root].l) - getHeight(nodes[root].r);
}

int insert(int root, int data) {
    if (root == -1) {
        return newNode(data);
    }
    if (data < nodes[root].data) {
        nodes[root].l = insert(nodes[root].l, data);
    } else {
        nodes[root].r = insert(nodes[root].r, data);
    }
    updateHeight(root);
    return root;
}

bool isAVL(int root) {
    if (root == -1) {
        return true;
    }
    return isAVL(nodes[root].l) && isAVL(nodes[root].r) && abs(getBalanceFactor(root)) <= 1;
}

int main() {
    int n, data, root = -1;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &data);
        root = insert(root, data);
    }
    printf(isAVL(root) ? "Yes" : "No");
    return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 50;

struct Node {
    int data;
    int height;
    int l, r;
} nodes[MAXN];

int nodeCount = 0;

int newNode(int data) {
    nodes[nodeCount].data = data;
    nodes[nodeCount].height = 1;
    nodes[nodeCount].l = nodes[nodeCount].r = -1;
    return nodeCount++;
}

int getHeight(int root) {
    if (root == -1) {
        return 0;
    } else {
        return nodes[root].height;
    }
}

void updateHeight(int root) {
    nodes[root].height = max(getHeight(nodes[root].l), getHeight(nodes[root].r)) + 1;
}

int getBalanceFactor(int root) {
    return getHeight(nodes[root].l) - getHeight(nodes[root].r);
}

int L(int root) {
    int temp = nodes[root].r;
    nodes[root].r = nodes[temp].l;
    nodes[temp].l = root;
    updateHeight(root);
    updateHeight(temp);
    return temp;
}

int R(int root) {
    int temp = nodes[root].l;
    nodes[root].l = nodes[temp].r;
    nodes[temp].r = root;
    updateHeight(root);
    updateHeight(temp);
    return temp;
}

int insert(int root, int data) {
    if (root == -1) {
        return newNode(data);
    }
    if (data < nodes[root].data) {
        nodes[root].l = insert(nodes[root].l, data);
        updateHeight(root);
        if (getBalanceFactor(root) == 2) {
            if (getBalanceFactor(nodes[root].l) == 1) {
                root = R(root);
            } else if (getBalanceFactor(nodes[root].l) == -1) {
                nodes[root].l = L(nodes[root].l);
                root = R(root);
            }
        }
    } else {
        nodes[root].r = insert(nodes[root].r, data);
        updateHeight(root);
        if (getBalanceFactor(root) == -2) {
            if (getBalanceFactor(nodes[root].r) == -1) {
                root = L(root);
            } else if (getBalanceFactor(nodes[root].r) == 1) {
                nodes[root].r = R(nodes[root].r);
                root = L(root);
            }
        }
    }
    return root;
}

vector<int> pre;

void preOrder(int root) {
    if (root == -1) {
        return;
    }
    pre.push_back(nodes[root].data);
    preOrder(nodes[root].l);
    preOrder(nodes[root].r);
}

int main() {
    int n, data, root = -1;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &data);
        root = insert(root, data);
    }
    preOrder(root);
    for (int i = 0; i < (int)pre.size(); i++) {
        printf("%d", pre[i]);
        if (i < (int)pre.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

9.6并查集

#include <cstdio>
#include <cstring>

const int MAXN = 100;
int father[MAXN];

int findFather(int x) {
    int xCopy = x;
    while (father[x] != x) {
        x = father[x];
    }
    int root = x;
    x = xCopy;
    while (father[x] != x) {
        int fatherX = father[x];
        father[x] = root;
        x = fatherX;
    }
    return root;
}

void unionSet(int a, int b) {
    int faA = findFather(a);
    int faB = findFather(b);
    if (faA != faB) {
        father[faA] = faB;
    }
}

void init(int n) {
    for (int i = 0; i < n; i++) {
        father[i] = i;
    }
}

int main() {
    int n, m, a, b;
    scanf("%d%d", &n, &m);
    init(n);
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &a, &b);
        unionSet(a - 1, b - 1);
    }
    int classCount = 0;
    for (int i = 0; i < n; i++) {
        if (father[i] == i) {
            classCount++;
        }
    }
    printf("%d", classCount);
    return 0;
}

#include <cstdio>
#include <cstring>

const int MAXN = 100;
int father[MAXN];

int findFather(int x) {
    int xCopy = x;
    while (father[x] != x) {
        x = father[x];
    }
    int root = x;
    x = xCopy;
    while (father[x] != x) {
        int fatherX = father[x];
        father[x] = root;
        x = fatherX;
    }
    return root;
}

void unionSet(int a, int b) {
    int faA = findFather(a);
    int faB = findFather(b);
    if (faA != faB) {
        father[faA] = faB;
    }
}

void init(int n) {
    for (int i = 0; i < n; i++) {
        father[i] = i;
    }
}

int main() {
    int n, m, a, b;
    scanf("%d%d", &n, &m);
    init(n);
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &a, &b);
        unionSet(a - 1, b - 1);
    }
    bool linked = true;
    for (int i = 1; i < n; i++) {
        if (findFather(i) != findFather(0)) {
            linked = false;
        }
    }
    printf(linked ? "Yes" : "No");
    return 0;
}
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 100;
int father[MAXN];
int score[MAXN];

vector<int> classes;

int findFather(int x) {
    int xCopy = x;
    while (father[x] != x) {
        x = father[x];
    }
    int root = x;
    x = xCopy;
    while (father[x] != x) {
        int fatherX = father[x];
        father[x] = root;
        x = fatherX;
    }
    return root;
}

void unionSet(int a, int b) {
    int faA = findFather(a);
    int faB = findFather(b);
    if (faA != faB) {
        if (score[faA] < score[faB]) {
            father[faA] = faB;
        } else {
            father[faB] = faA;
        }
    }
}

void init(int n) {
    for (int i = 0; i < n; i++) {
        father[i] = i;
    }
}

int main() {
    int n, m, a, b;
    scanf("%d%d", &n, &m);
    init(n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &score[i]);
    }
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &a, &b);
        unionSet(a - 1, b - 1);
    }
    for (int i = 0; i < n; i++) {
        if (findFather(i) == i) {
            classes.push_back(score[i]);
        }
    }
    sort(classes.rbegin(), classes.rend());
    printf("%d\n", (int)classes.size());
    for (int i = 0; i < classes.size(); i++) {
        printf("%d", classes[i]);
        if (i < (int)classes.size() - 1) {
            printf(" ");
        }
    }
    return 0;
}

9.7堆

#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 1000 + 1;
int heap[MAXN];

void downAdjust(int low, int high) {
    int i = low, j = i * 2;
    while (j <= high) {
        if (j + 1 <= high && heap[j + 1] > heap[j]) {
            j = j + 1;
        }
        if (heap[j] > heap[i]) {
            swap(heap[j], heap[i]);
            i = j;
            j = i * 2;
        } else {
            break;
        }
    }
}

void createHeap(int n) {
    for (int i = n / 2; i >= 1; i--) {
        downAdjust(i, n);
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &heap[i]);
    }
    createHeap(n);
    for (int i = 1; i <= n; i++) {
        printf("%d", heap[i]);
        if (i < n) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 1000 + 1;
int heap[MAXN];

void downAdjust(int low, int high) {
    int i = low, j = i * 2;
    while (j <= high) {
        if (j + 1 <= high && heap[j + 1] > heap[j]) {
            j = j + 1;
        }
        if (heap[j] > heap[i]) {
            swap(heap[j], heap[i]);
            i = j;
            j = i * 2;
        } else {
            break;
        }
    }
}

void createHeap(int n) {
    for (int i = n / 2; i >= 1; i--) {
        downAdjust(i, n);
    }
}

int deleteTop(int n) {
    if (n > 0) {
        heap[1] = heap[n--];
        downAdjust(1, n);
    }
    return n;
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &heap[i]);
    }
    createHeap(n);
    n = deleteTop(n);
    for (int i = 1; i <= n; i++) {
        printf("%d", heap[i]);
        if (i < n) {
            printf(" ");
        }
    }
    return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 50 + 1;
int heap[MAXN];

void downAdjust(int low, int high) {
    int i = low, j = i * 2;
    while (j <= high) {
        if (j + 1 <= high && heap[j + 1] > heap[j]) {
            j = j + 1;
        }
        if (heap[j] > heap[i]) {
            swap(heap[j], heap[i]);
            i = j;
            j = i * 2;
        } else {
            break;
        }
    }
}

void createHeap(int n) {
    for (int i = n / 2; i >= 1; i--) {
        downAdjust(i, n);
    }
}

void heapSort(int n) {
    createHeap(n);
    for (int i = n; i > 1; i--) {
        swap(heap[i], heap[1]);
        downAdjust(1, i - 1);
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &heap[i]);
    }
    heapSort(n);
    for (int i = 1; i <= n; i++) {
        printf("%d", heap[i]);
        if (i < n) {
            printf(" ");
        }
    }
    return 0;
}

9.8哈夫曼树

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int minCostToMergeFruits(vector<int>& fruits) {
    // 使用优先队列(最小堆)来处理
    priority_queue<int, vector<int>, greater<int>> minHeap(fruits.begin(), fruits.end());
    
    int totalCost = 0;
    
    // 当堆中还有超过一个元素时,进行合并操作
    while (minHeap.size() > 1) {
        // 取出最小的两个果堆
        int first = minHeap.top();
        minHeap.pop();
        int second = minHeap.top();
        minHeap.pop();
        
        // 合并这两个果堆
        int mergedCost = first + second;
        totalCost += mergedCost;
        
        // 将新的合并后的果堆放回堆中
        minHeap.push(mergedCost);
    }
    
    return totalCost;
}

int main() {
    int n;
    cin >> n;
    vector<int> fruits(n);
    
    for (int i = 0; i < n; ++i) {
        cin >> fruits[i];
    }
    
    int result = minCostToMergeFruits(fruits);
    cout << result << endl;
    
    return 0;
}

#include <cstdio>
#include <queue>
using namespace std;

priority_queue<int, vector<int>, greater<int> > pq;

int main() {
    int n, weight;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &weight);
        pq.push(weight);
    }
    int ans = 0;
    while (pq.size() > 1) {
        int top1 = pq.top();
        pq.pop();
        int top2 = pq.top();
        pq.pop();
        pq.push(top1 + top2);
        ans += top1 + top2;
    }
    printf("%d", ans);
    return 0;
}

#include <iostream>
#include <string>
#include <queue>
using namespace std;

const int MAXC = 26;
int charCnt[MAXC] = {0};

priority_queue<int, vector<int>, greater<int> > pq;

int main() {
    string s;
    cin >> s;
    for (int i = 0; i < s.length(); i++) {
        charCnt[s[i] - 'A']++;
    }
    for (int i = 0; i < MAXC; i++) {
        if (charCnt[i] > 0) {
            pq.push(charCnt[i]);
        }
    }
    int ans = 0;
    while (pq.size() > 1) {
        int top1 = pq.top();
        pq.pop();
        int top2 = pq.top();
        pq.pop();
        pq.push(top1 + top2);
        ans += top1 + top2;
    }
    printf("%d", ans);
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1931900.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

useState函数

seState是一个react Hook(函数)&#xff0c;它允许我们像组件添加一个状态变量&#xff0c;从而控制影响组件的渲染结果 数据驱动试图 本质&#xff1a;和普通JS变量不同的是&#xff0c;状态变量一旦发生变化组件的视图UI也会随着变化(数据驱动试图) 使用 修改状态 注意&am…

tracert及Test-netconnection使用

tracert命令 tracert <参数> <目标 IP 或主机名>-d 不将地址解析成主机名。 -h maximum_hops 搜索目标的最大跃点数。 -j host-list 与主机列表一起的松散源路由(仅适用于 IPv4)。 -w timeout 等待每个回复的超时时间(以毫秒为单位)。 -R 跟踪往返行程路径(仅适用…

【iOS】——ARC源码探究

一、ARC介绍 ARC的全称Auto Reference Counting. 也就是自动引用计数。使用MRC时开发者不得不花大量的时间在内存管理上&#xff0c;并且容易出现内存泄漏或者release一个已被释放的对象&#xff0c;导致crash。后来&#xff0c;Apple引入了ARC。使用ARC&#xff0c;开发者不再…

docker搭建普罗米修斯监控gpu

ip8的服务器监控ip110和ip111的服务器 被监控的服务器110和111只需要安装node-export和nvidia-container-toolkit 下载镜像包 docker pull prom/node-exporter docker pull prom/prometheus docker pull grafana/grafana新建目录 mkdir /opt/prometheus cd /opt/prometheus/…

微软的vscode和vs2022快捷键官网链接

vscode官方文档:https://code.visualstudio.com/docs/ vscode快捷键官方文档:https://code.visualstudio.com/docs/getstarted/keybindings vs2022官方文档:https://learn.microsoft.com/zh-cn/visualstudio/ide/?viewvs-2022 vscode快捷键官方文档:https://learn.microsoft.c…

石头剪刀布休息(猜拳游戏)

自己写的简易版 //2024.07.17 import java.util.Scanner; import java.util.Random; public class GuessingGame {public static void main(String[] args) {Tom tm new Tom();System.out.println("");for (int i 0; i < 3; i) {Random r new Random();tm.com…

数据可视化在智慧医疗中的重要应用

在现代智慧医疗的推动下&#xff0c;数据可视化技术正日益成为医疗领域的重要工具。通过将复杂的医疗数据转换为直观的图表和图形&#xff0c;数据可视化不仅提升了医疗服务的效率&#xff0c;还极大地改善了患者的就医体验。 在智慧医疗中&#xff0c;数据可视化首先在电子病历…

Android View的绘制流程

1.不管是View的添加&#xff0c;还是调用View的刷新方法invalidate()或者requestLayout()&#xff0c;绘制都是从ViewRootImpl的scheduleTraversals()方法开始 void scheduleTraversals() {if (!mTraversalScheduled) {mTraversalScheduled true;mTraversalBarrier mHandler…

什么牌子的充电宝小巧容量大!盘点小巧性价比高充电宝!

想象一下&#xff0c;当准备轻装出门&#xff0c;无论是逛街购物、户外运动&#xff0c;还是长途旅行&#xff0c;一个小巧玲珑却又容量充足的充电宝&#xff0c;能轻松地被塞进的口袋、背包的小角落里&#xff0c;随时随地为的电子设备“续命”&#xff0c;这是多么令人欣喜的…

Tita的OKR:高端制造行业的OKR案例

高端设备制造行业的发展趋势&#xff1a; 产业规模持续扩大&#xff1a;在高技术制造业方面&#xff0c;航空、航天器及设备制造业、电子工业专用设备制造等保持较快增长。新能源汽车保持产销双增&#xff0c;新材料新产品生产也高速增长。 标志性装备不断突破&#xff1a;例如…

sqlite数据库,轻量级数据库的使用

什么是sqlite数据库 sqlite是具有零配置、无服务的特点&#xff0c;遵循 ACID 规则&#xff0c;是一款备受欢迎的轻量级数据库。 tips&#xff1a;ACID 规则即&#xff0c;A&#xff08;原子性&#xff09;、C&#xff08;一致性&#xff09;、I&#xff08;独立性&#xff0…

第一百六十七节 Java IO教程 - Java Zip字节数组

Java IO教程 - Java Zip字节数组 校验和 Java在java.util.zip包中提供了一个Adler32类来计算数据字节的Adler-32校验和。 我们需要调用这个类的update()方法将字节传递给它。 在同一个包中还有另一个名为CRC32的类&#xff0c;它允许您使用CRC32算法计算校验和。 以下代码…

2.10、matlab中字符、数字、矩阵、字符串和元胞合并为字符串并将字符串以不同格式写入读出excel

1、前言 在 MATLAB 中&#xff0c;可以使用不同的数据类型&#xff08;字符、数字、矩阵、字符串和元胞&#xff09;合并为字符串&#xff0c;然后将字符串以不同格式写入 Excel 文件。 以下是一个示例代码&#xff0c;展示如何将不同数据类型合并为字符串&#xff0c;并以不…

Java的maven项目pom文件因为版本问题导入失败的处理办法

前言 今天遇到的一个问题&#xff0c;而且网上的答案很乱&#xff0c;几乎每人各执一词&#xff0c;年份还差距很远&#xff0c;几乎没有为我解决问题提供什么帮助&#xff0c;感觉蛮怪的&#xff0c;确实不常见&#xff0c;但不应该每次都靠降低版本来解决这种小类小bug&…

【文心智能体】前几天百度热搜有一条非常有趣的话题《00后疯感工牌》,看看如何通过低代码工作流方式实现图片显示

00后疯感工牌体验&#xff1a;https://mbd.baidu.com/ma/s/6yA90qtM 目录 前言比赛推荐工作流创建工作流入口创建工作流界面工作流界面HTTP工具卡点地方 总结推荐文章 前言 前几天百度热搜有一条非常有有趣《00后疯感工牌》。 想着通过文心智能体去一键生成00后疯感工牌是不是…

防火墙的冗余基础知识+实验检测

将之前先理清需要注意的知识点&#xff1a; 1、注意防火墙冗余时的会话表必须保持一致&#xff0c;这里HRP技术已经做到 2、vrrp是自动开启抢占的&#xff0c;且是根据优先级进行抢占的 3、免费ARP的作用&#xff1a;告诉交换机的某个IP的mac地址变成了我的这个mac地址 4、HRP …

Calibration相机内参数标定

1.环境依赖 本算法采用张正友相机标定法进行实现&#xff0c;内部对其进行了封装。 环境依赖为 ubuntu20.04 opencv4.2.0 yaml-cpp yaml-cpp安装方式&#xff1a; &#xff08;1&#xff09;git clone https://github.com/jbeder/yaml-cpp.git #将yaml-cpp下载至本地 &a…

Sentinel规则持久化Push模式两种实现方式

文章目录 sentinel持久化push推模式微服务端的实现具体实现源码分析读数据源写数据源的实现 微服务端解析读数据源流程 修改源码的实现官方demo修改源码实现配置类flowauthoritydegreadparamsystemgateway修改源码 测试补充 前置知识 pull模式 sentinel持久化push推模式 pull拉…

效率飙升!用升级版思维导图搞定测试用例

Xmind思维导图&#xff1c;转&#xff1e;测试用例_如何将xmind改成测试用例-CSDN博客https://weiyv.blog.csdn.net/article/details/135920569 上一次的【xmind思维导图转测试用例】的文章浏览量飙升&#xff0c;这一次把工具又进行升级啦&#xff0c;是在线版的免费工具哦&am…

JRebelXRebel在线激活(亲测可用)

包含所有新旧版本&#xff0c;包括2023.4.2、2023.4.1、2023.4.0、2023.3.2、2023.3.1、2023.3.0、2023.2.2、2023.2.1、2023.2.0、2023.1.2、2023.1.1 等以及所有2022版本 JRebel&XRebel激活服务器地址 激活服务器地址&#xff08;路线1,推荐&#xff09;&#xff0c;可…