邪门RE!!!!

P3367 【模板】并查集

iamputin @ 2024-06-09 08:04:53

本来想着10分钟过一道题的,没想到第一题直接全给我爆RE ???????

这代码哪里有问题了,本地可以过,洛谷IDE也可以过,一提交就全RE

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MAXN = 100005;
const int inf = numeric_limits<int>::max();
int fa[MAXN];
int n, m;

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

int unity(int x, int y) {
  x = find(x), y = find(y);
  if (x != y) fa[x] = y;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> m;
  init();
  for (int i = 0; i < m; ++i) {
    int z, x, y;
    cin >> z >> x >> y;
    if (z == 1) {
      unity(x, y);
    } else {
      cout << ((find(x) == find(y)) ? "Y" : "N") << endl;
    }
  }
}

by yjy_echo @ 2024-06-09 08:08:23

@ttourist 16行unity函数里无返回值,应改为void类型求关


by iamputin @ 2024-06-09 08:09:49

@yjy_echo 我去逆天!已关


by NC20061226 @ 2024-06-09 08:10:05

void unity(int x, int y) {
  x = find(x), y = find(y);
  if (x != y) fa[x] = y;
}

by iamputin @ 2024-06-09 08:10:09

这显得我很弱智(((


by yjy_echo @ 2024-06-09 08:12:42

@ttourist 没事,洛谷RE就只有几个可能:\ 1.数组越界\ 2.开了O2\ 3.函数不是void却没返回值\ 以上几点在本地测一般都会过,但是洛谷就会RE


by iamputin @ 2024-06-09 08:13:21

@yjy_echo 有道理有道理


by WydnksqhbD @ 2024-06-09 08:45:57

@yjy_echo 你说的对但是

return 1;


by yjy_echo @ 2024-06-09 08:47:04

@WydnksqhbD 这个没看懂,能麻烦说明下吗


by WydnksqhbD @ 2024-06-09 08:47:54

@yjy_echo 好像返回非 0 数会 RE()


by yjy_echo @ 2024-06-09 08:48:43

@WydnksqhbD 没试过,感觉有搞头


| 下一页