Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26829 | Accepted: 12275 |
Description
Input
Output
Sample Input
1 3 0 990 692 990 0 179 692 179 0
Sample Output
692
Hint
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <limits> using namespace std; int T, N, mapp[510][510]; int p[250010], u[250010], v[250010], w[250010], r[250010]; bool cmp(const int i, const int j) { return w[i] < w[j]; } int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } int Kruskal() { int maxn = numeric_limits<int>::min(); for (int i = 0; i < N * N; i++) p[i] = i; for (int i = 0; i < N * N; i++) r[i] = i; for (int i = 0, cou = 0; i < N; i++) { for (int j = 0; j < N; j++) { u[cou] = i; v[cou] = j; w[cou] = mapp[i][j]; cou++; } } sort(r, r + N * N, cmp); //?? for (int i = 0; i < N * N; i++) { int e = r[i]; int x = find(u[e]), y = find(v[e]); if (x != y) { p[y] = x; maxn = max(maxn, w[e]); } } return maxn; } int main() { scanf("%d", &T); while (T--) { scanf("%d", &N); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &mapp[i][j]); } } printf("%d\n", Kruskal()); } return 0; }
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <limits> #include <vector> using namespace std; int T, N, p[510]; struct Edge { int u, v, w; Edge(int u, int v, int w) : u(u), v(v), w(w) {} }; vector<Edge> edges; int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } bool cmp(const Edge a, const Edge b) { return a.w < b.w; } int Kruskal() { int maxn = numeric_limits<int>::min(); for (int i = 0; i < N; i++) p[i] = i; sort(edges.begin(), edges.end(), cmp); for (int i = 0; i < edges.size(); i++) { int x = find(edges[i].u), y = find(edges[i].v); if (x != y) { p[y] = x; maxn = max(maxn, edges.at(i).w); } } return maxn; } int main() { scanf("%d", &T); while (T--) { edges.clear(); //注意初始化 scanf("%d", &N); int w; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &w); if (i > j) { edges.push_back(Edge(i, j, w)); } } } printf("%d\n", Kruskal()); } return 0; }