首页 > 其他分享 >P1536 村村通

P1536 村村通

时间:2022-10-31 09:46:19浏览次数:33  
标签:P1536 int fy scanf fx ans find 村村通

简简单单的一个并查集

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
int u[N], v[N], f[1010];
int find (int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main(){
  int n, m;
  while(scanf("%d", &n) && n != 0){
  scanf("%d", &m);
  for(int i = 1;i <= n;i ++) f[i] = i;
  for(int i = 1;i <= m;i ++)scanf("%d%d", &u[i], &v[i]);
  int ans = n;
  for(int i = 1;i <= m;i ++){
    int fx = find(u[i]), fy = find(v[i]);
      if(fx != fy){
        f[fx] = fy;
        ans --;
       }
    }
    printf("%d\n", ans - 1);
  }
  return 0;
}

标签:P1536,int,fy,scanf,fx,ans,find,村村通
From: https://www.cnblogs.com/loser--QAQ/p/16843210.html

相关文章

  • 并查集--村村通
    题目描述某市调查城镇交通状况,得到现有城镇道路统计表。表中列出了每条道路直接连通的城镇。市政府“村村通工程”的目标是使全市任何两个城镇间都可以实现交通(但不一定有直......