我的代码为什么会编译错误啊!!!
查看原帖
我的代码为什么会编译错误啊!!!
675208
coder2009楼主2024/5/7 22:45

不正常啊!!!

#include <bits/stdc++.h>

using namespace std;

constexpr int MAX_NUM = 2e5 + 5;
int fa[MAX_NUM], close[MAX_NUM];
bool open[MAX_NUM];
vector<int> g[MAX_NUM];

int find(int a) {
    if (a != fa[a]) {
        fa[a] = find(fa[a]);
    }
    return fa[a];
}

void Union(int x, int y) {
    int a = find(x);
    int b = find(y);
    if (a != b) {
        fa[max(a, b)] = min(a, b);
    }
}

bool same(int x, int y) {
    return find(x) == find(y);
}

void best_coder() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        fa[i] = i;
        open[i] = true;
    }
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; ++i) {
        int tmp;
        cin >> tmp;
        close[i] = tmp;
        open[close[i]] = false;
    }

    int cnt = n - k;
    for(int i = 0; i < n; ++i){
        if (!open[i]) {
            continue;
        }
        for (auto j : g[i]) {
            if (open[j] && !same(i, j)) {
                Union(i, j);
                --cnt;
            }
        }
    }
    stack<int> ans;
    ans.push(cnt);
    for (int i = k - 1; i >= 0; --i) {
        ++cnt;
        open[close[i]] = true;
        for (auto j : g[close[i]]) {
            if (open[j] && !same(close[i], j)) {
                Union(close[i], j);
                --cnt;
            }
        }
        ans.push(cnt);
    }
    while (!ans.empty()) {
        cout << ans.top() << '\n';
        ans.pop();
    }
}

void happy_coder() {

}

int main() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    // 小码匠
    best_coder();

    // 最优解
    // happy_coder();

    return 0;
}

2024/5/7 22:45
加载中...