8000 Update 20955.cpp · jh2ee/basic-algo-lecture@1636ec2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1636ec2

Browse files
authored
Update 20955.cpp
1 parent 8d40077 commit 1636ec2

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

0x19/solutions/20955.cpp

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
1-
// Authored by : BaaaaaaaaaaarkingDog
1+
// Authored by : Joshua-Shin
22
// Co-authored by : -
3-
// http://boj.kr/****************
3+
// http://boj.kr/fc8c44a625de4949b552c8f1dbb0c0a8
44
#include <bits/stdc++.h>
55
using namespace std;
6-
7-
int main(void){
8-
ios::sync_with_stdio(0);
9-
cin.tie(0);
10-
11-
}
6+
int n, m;
7+
vector<int> adj[100001];
8+
bool check[100001];
9+
int p[100001];
10+
int bfs(int x) {
11+
int cnt = 0;
12+
queue<int> q;
13+
check[x] = true;
14+
q.push(x);
15+
while(!q.empty()) {
16+
x = q.front();
17+
q.pop();
18+
for(auto nx: adj[x]) {
19+
if(nx == p[x]) continue;
20+
if(check[nx]) {
21+
cnt++;
22+
continue;
23+
}
24+
check[nx] = true;
25+
p[nx] = x;
26+
q.push(nx);
27+
}
28+
}
29+
return cnt/2;
30+
}
31+
int main() {
32+
cin.tie(0)->sync_with_stdio(0);
33+
cin >> n >> m;
34+
while(m--) {
35+
int a, b;
36+
cin >> a >> b;
37+
adj[a].push_back(b);
38+
adj[b].push_back(a);
39+
}
40+
memset(check, false, sizeof(check));
41+
int groupCnt = 0;
42+
int cutCnt = 0;
43+
for(int i = 1; i<=n; i++) {
44+
if(check[i]==false) {
45+
groupCnt++;
46+
cutCnt += bfs(i);
47+
}
48+
}
49+
cout << groupCnt -1 + cutCnt << '\n';
50+
return 0;
51+
}

0 commit comments

Comments
 (0)
0