File tree Expand file tree Collapse file tree 1 file changed +48
-8
lines changed Expand file tree Collapse file tree 1 file changed +48
-8
lines changed Original file line number Diff line number Diff line change 1
- // Authored by : BaaaaaaaaaaarkingDog
1
+ // Authored by : Joshua-Shin
2
2
// Co-authored by : -
3
- // http://boj.kr/****************
3
+ // http://boj.kr/fc8c44a625de4949b552c8f1dbb0c0a8
4
4
#include < bits/stdc++.h>
5
5
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
+ }
You can’t perform that action at this time.
0 commit comments