求助20分MLE

P3367 【模板】并查集

虽然但是,这题N最大值不是只有10000吗
by stupidman1 @ 2022-08-12 23:00:30


@[psyqin](/user/87578) find函数错了
by char_cha_ch @ 2022-08-12 23:24:28


```cpp #include <bits/stdc++.h> using namespace std; const int MaxN = 1000010; int p[MaxN]; int find(int x) { if (p[x] != x) return p[x] = find(p[x]); return x; } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { p[i] = i; } while (m--) { int x, y, z; scanf("%d%d%d", &x, &y, &z); if (x == 1) { p[find(y)] = find(z); } else { if (find(y) == find(z)) cout << "Y" << endl; else cout << "N" << endl; } } return 0; } ``` @[psyqin](/user/87578)
by char_cha_ch @ 2022-08-12 23:26:56


@[stupidman1](/user/353858) 开大一点应该没事吧(
by codeli @ 2022-08-13 11:28:31


@[kirihara233](/user/701221) 谢谢大佬!确实是find错了 ``` #include <iostream> using namespace std; const int N = 100010; int p[N]; int find(int x) { if (p[x] != x) p[x] = find(p[x]); return p[x]; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) p[i] = i; while (m--) { int x, y, z; scanf("%d%d%d", &x, &y, &z); if (x == 1) { p[find(y)] = find(z); } else { if (find(y) == find(z)) cout << "Y" << endl; else cout << "N" << endl; } } return 0; } ```
by codeli @ 2022-08-13 11:29:20


``` cpp #include<bits/stdc++.h> using namespace std; int pre[10010]; int find(int x) { if(pre[x] == x)return x; return pre[x] = find(pre[x]); } void join(int x, int y) { int fx = pre[x]; int fy = pre[y]; if(fx != fy){ pre[fy] = fx; } } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { pre[i] = i; } for (int i = 0; i < m; i++) { int op, a, b; cin >> op >> a >> b; if(op == 1){ join(a, b); } else{ if(find(a) == find(b)){ cout<<"Y"<<endl; } else{ cout<<"N" <<endl; } } } } ```
by Saraphines @ 2023-07-25 11:03:15


|