-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtst2.cpp
More file actions
31 lines (24 loc) · 779 Bytes
/
tst2.cpp
File metadata and controls
31 lines (24 loc) · 779 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
void DFS(vector<pair<int, int>> graph[], int src, int prev_len, int *max_len,
vector<bool> &visited) {
visited[src] = 1;
int curr_len = 0;
pair<int, int> adjacent;
for (int i = 0; i < graph[src].size(); i++) {
adjacent = graph[src][i];
if (!visited[adjacent.first]) {
curr_len = prev_len + adjacent.second;
DFS(graph, adjacent.first, curr_len, max_len, visited);
}
if ((*max_len) < curr_len)
*max_len = curr_len;
curr_len = 0;
}
}
int longestCable(vector<pair<int, int>> graph[], int n) {
int max_len = INT_MIN;
for (int i = 1; i <= n; i++) {
vector<bool> visited(n + 1, false);
DFS(graph, i, 0, &max_len, visited);
}
return max_len;
}