Input | Output |
9 10 1 2 1 3 1 5 2 4 3 6 3 7 3 9 5 8 6 7 8 9 9 3 |
1 2 4 3 6 7 9 8 5 |
#include<bits/stdc++.h> #include<iostream> using namespace std; int n, m; vector<int> adj[1001]; bool visited[1001]; void inp() { cin >> n >> m; for(int i = 0; i < m; i++) { int x, y; cin >> x >> y; adj[x].push_back(y); // Do thi co huong thi comment dong code sau adj[y].push_back(x); } memset(visited, false, sizeof(visited)); } void dfs(int u) { cout << u << " "; // Danh dau la u da duoc ghe tham visited[u] = true; for(int v: adj[u]) { // Neu dinh v chua duoc tham if(!visited[v]) { dfs(v); } } } int main() { // Chuyen NHAP, XUAT thanh file freopen("lab_2_1_input.INP", "r", stdin); freopen("lab_2_1_output.OUT", "w", stdout); // INPUT inp(); // OUTPUT: duyet DFS dfs(1); }
Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.
Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!
Khám phá, trải nghiệm ngay
Vui lòng đăng nhập để gởi bình luận!
Đăng nhậpChưa có bình luận nào!