visited: ๋ฐฉ๋ฌธ์ฌ๋ถ ์ฒดํฌ
1 | 2 | 3 | 4 | 5 | 6 | 7 |
3 | 1 | 3 | 7 | 3 | 4 | 6 |
1(current)์ด 3(next)์ ์ง๋ชฉํ์ง๋ง, 3(current)์์ 3(next)์ ์ง๋ชฉํ๊ธฐ ๋๋ฌธ์ 3 ํผ์์๋ง ํ์ด๊ณ 1์ ํ์ด ๋์ง ๋ชปํ๋ค.
๋ฐ๋ก
6
2 3 4 5 6 2
output : 1
5
2 5 4 5 2
output : 3
6
1 3 4 3 2 6
output : 2
13
2 4 5 2 4 1 8 9 10 11 9 10 10
output : 8
10
2 5 7 1 6 8 8 3 5 10
output : 6
10
2 7 10 5 3 3 9 10 6 3
output : 8
6
2 1 1 2 6 3
output : 4
#include <stdio.h>
#include <deque>
#include <queue>
using namespace std;
void DFS(int start, deque<int> *graph, bool *visited, bool *done, int *count){
visited[start] = true;
printf("[+] %d visited\n", start);
int next = graph[start][0];
if(visited[next] == false){
DFS(next, graph, visited, done, count);
}
// count that included in project
else if(!done[next]){
for(int i=next; i !=start; i=graph[i][0]) { (*count)++; printf("%d counted.\n", i); }
(*count)++; // for counting start
printf("%d counted.\n", start);
}
done[start] = true;
}
int main(){
int T; // T: # of testcase
scanf("%d", &T);
for(int i=0; i<T; i++){
int n;
int count=0; // n: # of students
scanf("%d", &n);
deque<int> graph[n+1];
bool visited[n+1];
bool done[n+1];
fill(visited, visited+n+1, false);
fill(done, done+n+1, false);
for(int j=1; j<=n; j++){
int num;
scanf("%d", &num);
graph[j].push_back(num);
}
for(int j=1; j<=n; j++){
if(visited[j] == false) DFS(j, graph, visited, done, &count);
}
printf("%d\n", n-count);
}
}
๊ฐ graph[i]๋ ํ๋์ฉ๋ง ์์๋ฅผ ๊ฐ์ง๋ฏ๋ก, start์์ next๋ ํญ์ graph[start][0]์ด ๋๋ค.
์ด๊ฒ์ผ๋ก ์๊ฐ๋ณต์ก๋๋ฅผ ์ค์ฌ์ ์๊ฐ์ด๊ณผ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
๋ํ visited์ผ ๋, ๋ฐฉ๋ฌธ์ด ๋๋์ง ์์์ผ๋ฉด ํด๋น visited๋ถํฐ graph[i][0]์ ๋ํด count++์ ํด์ ํ๋ก์ ํธ์ ์ํ ๊ฒ์ ์ฒดํฌํ๊ณ , start์ ๊ฐ๋ค๋ฉด ๋๋ธ๋ค. ๋ฐ๋ณต๋ฌธ์ด ๋๋๋ฉด start์ ๋ํ count๋ ํ๋ค.
๋ง์ฝ start = 6์ด๊ณ ๋ค์ next = 4๋ผ๋ฉด ์ด๋ฏธ visited์ด๋ฏ๋ก ๋ฐ๋ณต๋ฌธ์ ์์ํด์ 4๋ถํฐ ์นด์ดํ ์ ํ๊ณ ,
i=4, i=graph[4]=7, i=graph[7]=6์ผ๋ก ๋ค์ start๋ก ๋์์ค๋ฉด ๋๋ธ๋ค. ์ด๋ 6์ ๋ํ ์นด์ดํ ๋ ํด์ผ ํ๋ฏ๋ก ๋ฐ๋ณต๋ฌธ ๋๋๊ณ ํ๋ฒ ๋ ์นด์ดํ ์ ํด์ค๋ค.
๊ทธ๋ฆฌ๊ณ start์ ๋ํด done[start]=true๋ก ๋ฐฉ๋ฌธ์ด ๋๋๊ณ ๋์ด์ ๋ฐฉ๋ฌธํ ์ผ์ด ์์์ ํ์ํ๋ค.
#include <bits/stdc++.h>
using namespace std;
void DFS(int start, int *graph, bool *visited, bool *done, int *count){
visited[start] = true;
int next = graph[start];
if(visited[next] == false) DFS(next, graph, visited, done, count);
// count that included in project
else if(!done[next]){
for(int i=next; i !=start; i=graph[i])(*count)++;
(*count)++; // for counting start
}
done[start] = true;
}
int main(){
int T; // T: # of testcase
scanf("%d", &T);
for(int i=0; i<T; i++){
int n;
int count=0; // n: # of students
scanf("%d", &n);
int graph[n+1]={0,};
bool visited[n+1];
bool done[n+1];
fill(visited, visited+n+1, false);
fill(done, done+n+1, false);
for(int j=1; j<=n; j++) scanf("%d", &graph[j]);
for(int j=1; j<=n; j++){
if(visited[j] == false) DFS(j, graph, visited, done, &count);
}
printf("%d\n", n-count);
}
}
๋ฌธ์ ํ์ด ๊ณผ์ ์์๋ STL๋ก ๊ตฌํ์ ํ์๋๋ฐ, ์๊ฐ์ด๊ณผ ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์ง ์์์ ๊ทธ๋ฅ deque ๊ฐ์ STL ์ ์ฐ๊ณ ๋ฐฐ์ด๋ก graph ๋ง๋ค๊ณ ์ ๋ ฅ๋ฐ์ผ๋๊น ์๊ฐ์ด๊ณผ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ณ ๋ง์์ต๋๋ค๋ฅผ ๋ฐ์ ์ ์์๋ค.
n-count๋ count๊ฐ ํ๋ก์ ํธ์ ์ํ ์ฌ๋ ์์ด๋ฏ๋ก n-count๋ก ํ๋ก์ ํธ์ ์ํ์ง ์์ ์๋ก ๋ต์ ๊ตฌํ๋ค.
[๋ฐฑ์ค(BOJ)] 2667๋ฒ: ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ (DFS/BFS, C++) (0) | 2021.02.27 |
---|---|
[๋ฐฑ์ค(BOJ)] 11047๋ฒ: ๋์ 0 (Greedy Algorithm(๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ), C++) (0) | 2021.02.26 |
[๋ฐฑ์ค(BOJ)] 2331๋ฒ: ๋ฐ๋ณต์์ด (DFS/BFS, C++) (0) | 2021.02.26 |
[๋ฐฑ์ค(BOJ)] 10451๋ฒ: ์์ด ์ฌ์ดํด (DFS/BFS, C++) (0) | 2021.02.25 |
[๋ฐฑ์ค(BOJ)] 1707๋ฒ: ์ด๋ถ ๊ทธ๋ํ (DFS/BFS, C++) (0) | 2021.02.25 |