CF 1465C Peaceful Rooks

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e5 + 10;
 4 int f[N];
 5 
 6 inline int find(int x)
 7 {
 8     return    (f[x] == x) ? x : f[x] = find(f[x]);
 9 }
10 
11 int main(){
12     int t; cin >> t;
13     while(t--)
14     {
15         int n, m; cin >> n >> m;
16         for(int i = 1 ; i <= n ; i++){
17             f[i] = i;
18         }
19         int res = 0;
20         for(int i = 1 ; i <= m ; i++){
21             int x, y; cin >> x >> y;
22             if(x == y)    continue;
23             if(find(x) != find(y)){
24                 res += 1;
25                 f[y] = x;
26             }else{
27                 res += 2;
28             }
29         }
30         cout << res << endl;
31     }
32     
33     return 0;
34 }

 

posted @ 2021-01-18 12:16  LegendN  阅读(87)  评论(0编辑  收藏  举报