Created
March 23, 2020 08:41
-
-
Save niklasjang/67bbc7f7808ebeb22914a2fb5e01f001 to your computer and use it in GitHub Desktop.
[PS][완전탐색][DFS]/[기본1][DFS 경로의 길이]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| using namespace std; | |
| vector<int> v[10010]; | |
| int n=0; //노드의 갯수 | |
| bool visited[10010]; | |
| int ans = 0; | |
| void dfs(int curr){ | |
| /*현재 노드의 edge를 방문하기 전 */ | |
| visited[curr] = true; | |
| cout<<curr<<'\n'; | |
| ans++; | |
| /*현재 노드의 edge를 방문 */ | |
| for(int i=0; i<v[curr].size(); i++){ | |
| int next= v[curr][i]; | |
| if(!visited[next]) dfs(next); | |
| } | |
| /*현재 노드의 edge를 방문한 후 */ | |
| //visited[curr] = false; //가능한 모든 DFS 경로를 탐색하기 위한 visted 취소 | |
| } | |
| int main (void){ | |
| int i=0, a=0,b=0; | |
| cin>> n; | |
| for(int i=0; i<n; i++){ | |
| cin>> a>> b; | |
| v[a].push_back(b); | |
| v[b].push_back(a); | |
| } | |
| dfs(0); | |
| cout << ans; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment