- 题解
校区巴士联通
- @ 2026-8-3 18:33:58
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int N=1e5+10;
int fa[N],ans;
struct edge {
int a,b,w;
} e[N];
bool cmp(edge x,edge y) {
return x.w>y.w;
}
int find(int u) {
if(fa[u]==u)return u;
return fa[u]=find(fa[u]);
}
int main() {
while(1) {
ans=0;
cin>>n;
if(n==0) return 0;
cin>>m;
for(int i=1; i<=1000; i++) {
fa[i]=i;
}
for(int i=1; i<=m; i++) {
cin>>e[i].a>>e[i].b;
e[i].w=e[i].b-e[i].a;
}
sort(e+1,e+m+1,cmp);
for(int i=1; i<=m; i++) {
int fx=find(e[i].a),fy=find(e[i].b);
if(fx!=fy) {
fa[fx]=fy;
ans++;
}
}
cout<<max(0,n-1-ans)<<endl;
}
return 0;
}
2 条评论
-
周嘉濠 铂金 可达班2025 @ 2026-8-4 12:08:05
1
-
@ 2026-8-3 19:04:20
1
- 1