-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path2019-E-A.cpp
71 lines (59 loc) · 1.28 KB
/
2019-E-A.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<bits/stdc++.h>
using namespace std;
#define ms(x,v) memset(x,(v), sizeof(x))
#define msn(x,v,n) memset(x,(v),sizeof(x[0]) * n)
#define INF 0x3f3f3f3f
typedef long long LL;
typedef pair<int,int> PII;
const int MAXN = 1e5+10;
int par[MAXN];
int FIND(int x){
return x == par[x] ? x : par[x] = FIND(par[x]);
}
void UNOIN(int x,int y){
x = FIND(x);
y = FIND(y);
if(x !=y)
par[x] = par[y];
}
void solve(){
int n,m;
cin >> n >> m;
for(int i=1 ; i <=n ; ++i)
par[i] = i;
for(int i=0 ; i<m ; ++i){
int x,y;
cin >>x >> y;
UNOIN(x,y);
}
vector<vector<int> > G(n+1);
for(int i=1 ; i<=n ; ++i)
G[FIND(i)].push_back(i);
int k=0;
int ans =0;
for(int i=1 ; i<=n ; ++i){
if(!G[i].empty()){
ans += G[i].size() -1;
k+=1;
}
}
ans += 2 * (k-1);
cout << " " << ans ;
}
int main(int argc, char const *argv[])
{
ios :: sync_with_stdio(0);
cin.tie(0);
// cout.tie(0);
std::cout.precision(8);
std::cout.setf( std::ios::fixed, std:: ios::floatfield );
int T;
cin >> T;
for(int kase =1; kase <= T ; ++kase)
{
cout << "Case #"<<kase << ":";
solve();
cout << '\n';
}
return 0;
}