/* * @Description: To iterate is human, to recurse divine. * @Autor: Recursion * @Date: 2022-05-16 22:59:39 * @LastEditTime: 2022-05-16 23:27:17 */ #include <bits/stdc++.h> #define LL long long using namespace std; const int maxn = 1e6 + 10; const int mod = 1e9 + 7; const int INF = 1e9 + 10; const int N = 1e6; int T,n,m,ans; int f[N]; int a[N]; int b[N]; int find(int x){ if(f[x] == x) return x; return f[x] = find(f[x]); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> T; int num = 0; while(T--){ ans = 0; num ++; cin >> n >> m; for(int i = 1;i <= n;i ++){ f[i] = i; a[i] = 0;//边 b[i] = 1;//点 } while(m -- ){ int x,y; cin >> x >> y; int u = find(x); int v = find(y); if(u == v){ a[v]++; } else{ f[u] = v; a[v] += a[u] + 1; b[v] += b[u]; } } for(int i = 1;i <= n;i ++){ if(i == f[i]&&(a[i] - b[i] >= 0)){ ans += a[i] - b[i]; } } cout << "Case #" << num << ": " << ans << endl; } return 0; }
标签:group,cout,P1763,cin,friendly,int,ans,const,find From: https://www.cnblogs.com/lhf123/p/17425243.html