forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2485 - Highways.cpp
78 lines (59 loc) · 1.35 KB
/
2485 - Highways.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <cstdio>
#include <algorithm>
using namespace std;
struct edge{
int u,v,w;
edge(){}
bool operator < (edge X)const{
return w < X.w;
}
}L[124750];
int parent[500],rank[500];
void init(int N){
for(int i = 0;i < N;++i){
parent[i] = i;
rank[i] = 0;
}
}
int Find(int x){
if(parent[x] != x) parent[x] = Find(parent[x]);
return parent[x];
}
void Union(int x, int y){
int PX = Find(x),PY = Find(y);
if(rank[PX] < rank[PY]) parent[PY] = PX;
else{
parent[PX] = PY;
if(rank[PX] == rank[PY]) ++rank[PY];
}
}
int main(){
int T,N,E,w;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
E = 0;
for(int i = 0;i < N;++i){
for(int j = 0;j < N;++j){
scanf("%d",&w);
if(i < j){
L[E].u = i;
L[E].v = j;
L[E].w = w;
++E;
}
}
}
init(N);
sort(L,L + E);
int ans = 0;
for(int i = 0;i < E;++i){
if(Find(L[i].u) != Find(L[i].v)){
Union(L[i].u,L[i].v);
ans = max(ans,L[i].w);
}
}
printf("%d\n",ans);
}
return 0;
}