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

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;
}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

NLP入门——RNN、LSTM模型的搭建、训练与预测

在卷积语言模型建模时&#xff0c;我们选取上下文长度ctx_len进行训练&#xff0c;预测时选取句子的最后ctx_len个分词做预测&#xff0c;这样句子的前0~seql-1-ctx_len个词对于预测没有任何帮助&#xff0c;这对于语言处理来说显然是不利的。 在词袋语言模型建模时&#xff0c…

C语言 底层逻辑详细阐述指针(一)万字讲解 #指针是什么? #指针和指针类型 #指针的解引用 #野指针 #指针的运算 #指针和数组 #二级指针 #指针数组

文章目录 前言 序1&#xff1a;什么是内存&#xff1f; 序2&#xff1a;地址是怎么产生的&#xff1f; 一、指针是什么 1、指针变量的创建及其意义&#xff1a; 2、指针变量的大小 二、指针的解引用 三、指针类型存在的意义 四、野指针 1、什么是野指针 2、野指针的成因 a、指…

【HarmonyOS】关于鸿蒙消息推送的心得体会 (一)

【HarmonyOS】关于鸿蒙消息推送的心得体会&#xff08;一&#xff09; 前言 这几天调研了鸿蒙消息推送的实现方式&#xff0c;形成了开发设计方案&#xff0c;颇有体会&#xff0c;与各位分享。 虽然没做之前觉得很简单的小功能&#xff0c;貌似只需要和华为服务器通信&…

玩转HarmonyOS NEXT之AppStorage应用全局UI状态存储

概述 AppStorage是应用全局的UI状态存储&#xff0c;是和应用的进程绑定的&#xff0c;由UI框架在应用程序启动时创建&#xff0c;为应用程序UI状态属性提供中央存储。 AppStorage是在应用启动的时候会被创建的单例。它的目的是为了提供应用状态数据的中心存储&#xff0c;这…

【HarmonyOS学习】Calendar Kit日历管理

简介 Calendar Kit提供日历与日程管理能力&#xff0c;包括日历的获取和日程的创建能力。 Calendar Kit为用户提供了一系列接口来获取日历账户&#xff0c;并使用特定的接口向日历账户中写入日程。 如果写入的日程带有提醒时间则系统会在时间到达时向用户发送提醒。 约束点…

Linux编程(通信协议---udp)

UDP&#xff08;用户数据报协议&#xff09;是一种无连接的网络协议&#xff0c;主要用于快速传输数据。以下是UDP协议的一些主要特点&#xff1a; 1. **无连接**&#xff1a;UDP是无连接的协议&#xff0c;这意味着在数据传输之前不需要建立连接。每个UDP数据包都是独立的&am…

remote: ERROR: commit b81ea84: missing Change-Id in message footer

首次拉取代码后,在本地已经编辑添加了代码并且想要提交到远端仓库 git add . git commit 当commit之后想要pull的时候报错了 git pull 执行到git pull 时出现这个问题,这是由于Change-Id没了,提示: ! [remote rejected] HEAD -> refs/for/master (commit b81ea84: mis…

git回退分支版本git reset --hard HEAD

git回退分支版本git reset --hard HEAD git reset --hard HEAD 上面命令清除本地所有修改&#xff0c;与下面相似&#xff1a; git reset --hard origin/master 等同于&#xff1a; git reset --hard HEAD~0 说明&#xff1a; HEAD 当前版本 HEAD^ 上一个版本 HEAD^^ 上上…

JVM---对象是否存活及被引用的状态

1.如何判断对象是否存活 1.1 引用计数算法 概念&#xff1a;在对象头部增加一个引用计数器,每当有一个地方引用它时&#xff0c;计数器值就加一&#xff1b;当引用失效时&#xff0c;计数器值就减一&#xff1b;任何时刻计数器为零的对象就是不可能再被使用的。 优点&#xff1…

LabVIEW学习-LabVIEW储存Excel表格

上述实现了将格式化的时间和正弦波的频率振幅相位以及正弦波数据输入到excel表格中。 下面介绍其中使用到的函数&#xff1a; 1. 所在位置&#xff0c;函数选板->定时->获取日期/时间(秒) 2. 将获取的时间进行格式化处理&#xff0c;输出格式化的日期/时间字符串。 函…

AI赋能基础设施巡检,技术革新助力水泥建筑缺陷检测分析,基于YOLOv8模型开发构建水泥建筑场景下裂缝缺陷分割检测识别系统

在现代化城市建设的宏伟蓝图中&#xff0c;公路、隧道、桥梁、大坝等水泥类基础设施如同城市的血脉&#xff0c;支撑着社会的正常运转与经济的蓬勃发展。然而&#xff0c;时间的侵蚀与自然的考验使得这些建筑不可避免地面临老化与损坏的问题&#xff0c;裂缝作为其中最为常见的…

AV1 编码标准环路滤波和后处理技术概述

AV1 环路滤波 去块滤波器 在视频编码的环路滤波管道中&#xff0c;去块滤波器&#xff08;deblocking filter&#xff09;用于减少量化引起的变换块边界处的块状伪影。 总结&#xff1a; 去块滤波器的应用&#xff1a; 对于亮度&#xff08;luma&#xff09;色度分量&#xff…

minIO集成springboot

问题 minIO与spring集成。 步骤 创建桶 创建key 找到创建账号页面&#xff0c;如下图&#xff1a; 点击创建&#xff0c;如下图&#xff1a; 设置如下权限&#xff1a; {"Version": "2012-10-17","Statement": [{"Effect": &q…

华为OD算法题汇总

60、计算网络信号 题目 网络信号经过传递会逐层衰减&#xff0c;且遇到阻隔物无法直接穿透&#xff0c;在此情况下需要计算某个位置的网络信号值。注意:网络信号可以绕过阻隔物 array[m][n]&#xff0c;二维数组代表网格地图 array[i][j]0&#xff0c;代表i行j列是空旷位置 a…

如何在所有docker命令前加上一个sudo

如果当前登录用户不是root不用&#xff0c;使用docker命令的时候&#xff0c;需要在前面加上一个sudo 提升权限。 但是每次都加&#xff0c;就感觉特别的麻烦&#xff0c;如何简化呢&#xff1f; 解决办法 打开你的shell配置文件&#xff08;例如&#xff0c;如果你使用bash&am…

Spring Cloud Eureka快读入门Demo

1.什么是Eureka&#xff1f; Eureka 由 Netflix 开发&#xff0c;是一种基于REST&#xff08;Representational State Transfer&#xff09;的服务&#xff0c;用于定位服务&#xff08;服务注册与发现&#xff09;&#xff0c;以实现中间层服务的负载均衡和故障转移&#xff…

C语言 | Leetcode C语言题解之第239题滑动窗口最大值

题目&#xff1a; 题解&#xff1a; int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {int prefixMax[numsSize], suffixMax[numsSize];for (int i 0; i < numsSize; i) {if (i % k 0) {prefixMax[i] nums[i];} else {prefixMax[i] fmax(pref…

甄选范文“论软件维护方法及其应用”软考高级论文,系统架构设计师论文

论文真题 软件维护是指在软件交付使用后,直至软件被淘汰的整个时间范围内,为了改正错误或满足 新的需求而修改软件的活动。在软件系统运行过程中,软件需要维护的原因是多种多样的, 根据维护的原因不同,可以将软件维护分为改正性维护、适应性维护、完善性维护和预防性 维护…

Mindspore框架CycleGAN模型实现图像风格迁移|(三)损失函数计算

Mindspore框架&#xff1a;CycleGAN模型实现图像风格迁移算法 Mindspore框架CycleGAN模型实现图像风格迁移|&#xff08;一&#xff09;CycleGAN神经网络模型构建 Mindspore框架CycleGAN模型实现图像风格迁移|&#xff08;二&#xff09;实例数据集&#xff08;苹果2橘子&…

JAVA 异步编程(异步,线程,线程池)一

目录 1.概念 1.1 线程和进程的区别 1.2 线程的五种状态 1.3 单线程,多线程,线程池 1.4 异步与多线程的概念 2. 实现异步的方式 2.1 方式1 裸线程&#xff08;Thread&#xff09; 2.1 方式2 线程池&#xff08;Executor&#xff09; 2.1.1 源码分析 2.1.2 线程池创建…