Skip to content

Commit ab775f0

Browse files
authored
Create 2019-E-A.cpp
1 parent 7346944 commit ab775f0

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

Diff for: kick-start/2019-E-A.cpp

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

0 commit comments

Comments
 (0)