-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdls.cpp
More file actions
40 lines (35 loc) · 875 Bytes
/
dls.cpp
File metadata and controls
40 lines (35 loc) · 875 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
#include <bits/stdc++.h>
#define mx 1000000
using namespace std;
vector < int > graph[mx];
bool vis[mx];
int dis[mx];
void dls(int vertex, int depth, int maxDepth) {
vis[vertex] = true;
if (depth > maxDepth) {
return; // Return if depth exceeds the maximum depth limit.
}
for (int i = 0; i < graph[vertex].size(); i++) {
int next = graph[vertex][i];
if (!vis[next]) {
dis[next] = dis[vertex] + 1;
dls(next, depth + 1, maxDepth); // Increment depth for the child nodes.
}
}
}
void solve() {
int edge, source, maxDepth;
cin >> edge;
for (int i = 0; i < edge; i++) {
int v1, v2;
cin >> v1 >> v2;
graph[v1].push_back(v2);
graph[v2].push_back(v1);
}
cin >> source >> maxDepth;
dis[source] = 0;
dls(source, 0, maxDepth); // Start the DLS from the source node with maxDepth limit.
}
int main() {
solve();
}