forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc177_d.cpp
More file actions
executable file
·45 lines (38 loc) · 745 Bytes
/
abc177_d.cpp
File metadata and controls
executable file
·45 lines (38 loc) · 745 Bytes
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
// Problem Code: abc177_d
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
int friends(int N, int M, vector<vector<int>>& adj) {
int len, max_len = 0;
vector<bool> visited(N);
// helper function
function<void(int)> dfs = [&](int s) {
visited[s] = true;
max_len = max(++len, max_len);
for (int& u: adj[s])
if (!visited[u])
dfs(u);
};
// visit each component
for (int i = 0; i < N; i++)
if (!visited[i]) {
len = 0;
dfs(i);
}
return max_len;
}
int main() {
int N, M;
cin >> N >> M;
vector<vector<int>> adj(N);
for (int i = 0; i < M; i++) {
int A, B;
cin >> A >> B;
--A, --B;
adj[A].push_back(B);
adj[B].push_back(A);
}
cout << friends(N, M, adj);
return 0;
}