求调!!!

P1197 [JSOI2008] 星球大战

vk3601h @ 2024-11-24 20:09:38

P1197 [JSOI2008] 星球大战求调

代码在Windows11下使用MinGW或者在WSL2中的Ubuntu20.04下使用g++9.4.0都可以编译通过,但提交到洛谷上就CE

请各位大佬赐教!!!

#include <bits/stdc++.h>
#define rint register int
using namespace std;
const int N = 4e5 + 10;
int n, m, k, brk[N], ans[N];
bool flag[N];

//Graph
int cnt, head[N], from[N << 1], to[N << 1], nxt[N << 1];

inline void add(int u, int v){
    to[++cnt] = v, from[cnt] = u, nxt[cnt] = head[u], head[u] = cnt;
    to[++cnt] = u, from[cnt] = v, nxt[cnt] = head[v], head[v] = cnt;
}

//UnionSet
int fa[N], siz[N];

inline void init() {for (rint i = 1; i <= n; ++i) fa[i] = i, siz[i] = 1;}

inline int query(int x) {return fa[x] == x ? x : fa[x] = query(fa[x]);}

inline void unite(int x, int y){
    x = query(x), y = query(y);
    if (x == y) return;
    if (siz[x] < siz[y]) swap(x, y);
    fa[y] = x;
    siz[x] += siz[y];
}

inline int read(){
    int x = 0;
    bool pos = true;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') pos = false;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    return pos ? x : ~(x - 1);
}

char ch[35];
inline void write(int x){
    if (!x) {putchar('0'); return;}
    if (x < 0) putchar('-'), x = ~(x - 1);
    int t = 0;
    while (x) ch[++t] = (char)(x % 10 + '0'), x /= 10;
    for (rint i = t; i; --i) putchar(ch[i]);
}

int main(){
    //freopen("test.in", "r", stdin);

    n = read(), m = read();
    for (rint i = 1; i <= m; ++i){
        int x = read() + 1, y = read() + 1;
        add(x, y);
    }
    k = read();
    for (rint i = 1; i <= k; ++i) brk[i] = read() + 1, flag[brk[i]] = true;

    init();
    int tot = n - k;
    for (rint i = 1; i <= cnt; ++i){
        if (!flag[from[i]] && !flag[to[i]] && query(from[i]) != query(to[i])){
            unite(from[i], to[i]);
            tot--;
        }
    }
    ans[k + 1] = tot;

    for (rint i = k; i; --i){
        flag[brk[i]] = false; tot++;
        for (rint j = head[brk[i]]; j; j = nxt[j]){
            if (!flag[to[j]] && query(brk[i]) != query(to[j])){
                unite(brk[i], to[j]);
                tot--;
            }
        }
        ans[i] = tot;
    }

    for (rint i = 1; i <= k + 1; ++i) write(ans[i]), putchar('\n');
    return 0;
}

by vk3601h @ 2024-11-24 20:24:40

神秘的问题

为什么当我把

#include <bits/stdc++.h>

替换为

#include <iostream>
#include <vector>
#include <algorithm>

不仅编译正常,还AC了?!


by Estrella_Explore @ 2024-11-24 22:00:48

@vk3601h

诶怎么又是你(((

你的 int brk[N]unistd.h 中的一个同名的函数重复里

/usr/include/unistd.h:1070:12: 附注:previous declaration ‘int brk(void*)’
 1070 | extern int brk (void *__addr) __THROW __wur;
      |            ^~~

|