神谷英樹監督彈幕遊戲《太陽登陸艦》12月9日上市
阿新 • • 發佈:2021-09-03
B - Highways
https://vjudge.net/contest/348411#problem/B
POJ - 2485
//Kruskal #include<iostream> #include<cstdio> #include<cstdlib> #include<vector> #include<algorithm> #define rep(i,x,y) if ((x)<=(y)) for (register int i=(x);i<=(y);i++) using namespace std; struct node{ int a,b,data; }; int pre[100000]; int find(int a) { if (pre[a]==a) return a; else return pre[a]=find(pre[a]); } void uni(int a,int b) { a=find(a); b=find(b); if (a!=b) pre[a]=b; } bool cmp(node x,node y) { if (x.data<y.data) return true; return false; } int main() { int t,n,x,tot=0; cin>>t; while (t--) { vector <node> a; tot=0; a.clear(); cin>>n; rep(i,1,n) pre[i]=i; rep(i,1,n) rep(j,1,n) { scanf("%d",&x); node temp; if (i<j) { temp.a=i; temp.b=j; temp.data=x; a.push_back(temp); } } sort(a.begin(),a.end(),cmp); rep(i,0,a.size()-1) { if (find(a[i].a)!=find(a[i].b)) { uni(a[i].a,a[i].b); tot++; if (tot==n-1) { cout<<a[i].data<<endl; break; } } } } return 0; }