-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnected_components.cpp
More file actions
52 lines (47 loc) · 1.08 KB
/
connected_components.cpp
File metadata and controls
52 lines (47 loc) · 1.08 KB
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
#include <bits/stdc++.h>
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
using namespace std;
typedef long long ll;
typedef long double ld;
#define endl "\n"
#define debug(args...) cout << (#args) << " = " << (args) << endl
#define MOD 1000000007
#define vi vector<int>
#define fl forward_list
#define pb push_back
#define pf push_front
#define read(st) getline(cin, st)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define MAXN 100000
int n;
vector<int> g[MAXN];
bool used[MAXN];
vector<int> comp;
void dfs(int v)
{
used[v] = true;
comp.push_back(v);
for (size_t i = 0; i < (int)g[v].size(); ++i)
{
int to = g[v][i];
if (!used[to])
dfs(to);
}
}
void find_comps()
{
for (int i = 0; i < n; ++i)
used[i] = false;
for (int i = 0; i < n; ++i)
if (!used[i])
{
comp.clear();
dfs(i);
cout << "Component:";
for (size_t j = 0; j < comp.size(); ++j)
cout << ' ' << comp[j];
cout << endl;
}
}