蒟蒻求助

P1547 [USACO05MAR] Out of Hay S

@[陆小果](/space/show?uid=113932) 等等这题不是求最大值吗。。
by 1saunoya @ 2019-09-27 12:20:38


`ans = max(ans,a[i].w)`
by 1saunoya @ 2019-09-27 12:21:01


@[清风ღ](/space/show?uid=96580) 因为我之前排过序了,所以后面的一定更大,直接赋值就行了.....
by lxg_honoka @ 2019-09-27 17:33:52


为啥你find了两次
by 1saunoya @ 2019-09-27 17:34:49


@[陆小果](/space/show?uid=113932)
by 1saunoya @ 2019-09-27 17:34:52


@[清风ღ](/space/show?uid=96580) 好像不影响结果叭...
by lxg_honoka @ 2019-09-27 17:36:53


``` for (int i = 1; i <= m; i++) { int x = find(a[i].u), y = find(a[i].v); if (x != y) { ans = a[i].w; unionset(a[i].u, a[i].v); }// here }
by 1saunoya @ 2019-09-27 17:37:53


@[陆小果](/space/show?uid=113932)
by 1saunoya @ 2019-09-27 17:37:57


呃,改了一下,然后还是10分...QAQ 我最看不懂的是RE.... ```cpp #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int maxn = 40000 + 10; int fa[4000 + 10]; int n, m, ans = -1; struct stu { int u, v, w; }a[maxn]; bool cmp (stu a, stu b) { return a.w < b.w; } int read() { int c, s = 0; c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c < '9') { s *= 10; s += c - '0'; c = getchar(); } return s; } void makeset (int size) { for (int i = 1; i <= size; i++) fa[i] = i; return; } int find (int x) { if (fa[x] != x) fa[x] = find(fa[x]); return fa[x]; } /* void unionset (int x, int y) { x = find(x), y = find(y); if (x == y) return; fa[x] = y; } */ void kruskal () { makeset(n); sort(a+1, a+m+1, cmp); for (int i = 1; i <= m; i++) { int x = find(a[i].u), y = find(a[i].v); if (x != y) { ans = a[i].w; fa[x] = y; } } } int main() { n = read(), m = read(); for (int i = 1; i <= m; i++) { a[i].u = read(); a[i].v = read(); a[i].w = read(); } kruskal(); printf("%d\n", ans); return 0; } ```
by lxg_honoka @ 2019-09-27 17:42:14


我查出了哪错了... 快读那里 ```cpp while (c >= '0' && c < '9') { s *= 10; s += c - '0'; c = getchar(); } ``` 少写了一个等号 Orz 非常感谢您帮我查错QAQ
by lxg_honoka @ 2019-09-27 18:10:59


|