求助,WA和MLE都有。

P1991 无线通讯网

@[Claire0918](/user/660816) 首先你并查集函数就写错了。
by Adchory @ 2022-12-30 19:26:26


```cpp #include<bits/stdc++.h> using namespace std; const int maxn = 500 + 10; struct pos{ int x, y; }; struct edge{ int p1, p2, d; }; bool cmp(const edge & a, const edge & b){ return a.d < b.d; } int s, p; pos a[maxn]; int father[maxn]; int c; double res = -1; vector<edge> G; inline int get_father(int x){ if (father[x] == x){ return x; } father[x] = get_father(father[x]); return father[x]; } int main(){ scanf("%d %d", &s, &p); for (int i = 1; i <= p; i++){ father[i] = i; } for (int i = 1; i <= p; i++){ scanf("%d %d", &a[i].x, &a[i].y); for (int j = 1; j < i; j++){ G.push_back({i, j, (a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y)}); } } sort(G.begin(), G.end(), cmp); c = p; for (auto x: G){ int ti = get_father(x.p1), tj = get_father(x.p2); if (ti != tj){ father[ti] = tj; c--; res = max(res, sqrt(x.d)); } if (c == s){ break; } } printf("%.2lf", res); return 0; } ``` @[Reimu_Hakurei](/user/590600)
by Claire0918 @ 2022-12-30 19:36:43


[评测记录](https://www.luogu.com.cn/record/98289866) 过了,谢谢。
by Claire0918 @ 2022-12-30 19:39:02


@[Reimu_Hakurei](/user/590600) 但是并查集哪里错了
by Claire0918 @ 2022-12-30 19:43:02


@[Claire0918](/user/660816) `father[x] == get_father(father[x]);` 看清楚点……
by Adchory @ 2022-12-30 19:44:09


啊这……
by Claire0918 @ 2022-12-30 19:46:47


|