알고리즘 공부
백준 트리 1068
컴퓨터과학
2023. 9. 5. 22:44
https://www.acmicpc.net/problem/1068
1068번: 트리
첫째 줄에 트리의 노드의 개수 N이 주어진다. N은 50보다 작거나 같은 자연수이다. 둘째 줄에는 0번 노드부터 N-1번 노드까지, 각 노드의 부모가 주어진다. 만약 부모가 없다면 (루트) -1이 주어진다
www.acmicpc.net
ㅠㅠ 1시간만에 해결했네요... 이런문제를 어떻게 20분안에 풀까용 ㅠㅠ
포인트는 시작점은 어디든 랜덤위치라는것 그리고 삭제 위치 이후로는 dfs 더이상 진행하지않는다. 그리고 카운트는 시작할때 해주고 진행이 끝날때까지 카운트를 하지않습니다.
#include <iostream>
#include <vector>
#include <queue>
#include<string>
#include<algorithm>
#include<cmath>
#include<unordered_map>
#include<map>
#include<stack>
using namespace std;
struct Point {
int i, j;
int sum = 0;
int cnt=0;
};
void Prints(vector<vector<int>>maps, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void Prints(vector<vector<bool>>maps, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void Prints(vector<vector<char>>maps, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[i][j];
}
cout << endl;
}
cout << endl;
}
void Prints(vector<vector<vector<char>>>maps, int n, int m, int k) {
for (int t = 0; t < k; t++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[t][i][j];
}
cout << endl;
}
cout << endl;
}
cout << endl;
}
void Prints(vector<vector<vector<bool>>>maps, int n, int m, int k) {
for (int t = 0; t < k; t++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[t][i][j];
}
cout << endl;
}
cout << endl;
}
cout << endl;
}
void Prints(vector<int>line, int n) {
for (int i = 0; i < n; i++) {
cout << line[i] << ",";
}
cout << endl;
}
void Prints(string str, int n) {
for (int i = 0; i < n; i++) {
cout << str[i] << ",";
}
cout << endl;
}
bool checkmaps(vector<vector<int>>maps, vector<vector<int>>anw, int n, int m) {
bool bCheck = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (maps[i][j] != anw[i][j]) {
bCheck = true;
i = n + 1;
j = m + 1;
continue;
}
}
}
return bCheck;
}
bool findstart(vector<vector<int>>maps, vector<vector<int>>anw, int n, int m, int) {
bool bCheck = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (maps[i][j] != anw[i][j]) {
bCheck = true;
i = n + 1;
j = m + 1;
continue;
}
}
}
return bCheck;
}
struct Data {
Point p;
int cnt;
int sum;
};
vector<int>lists;
vector<bool>bCheck;
int n;
int del;
int maxs = 0;
int gcnt = 0;
bool bFind = false;
void dfs(int k, int cnt) {
bCheck[k] = true;
if (bFind==false) {
bFind = true;
gcnt++;
}
for (int i = 0; i < n; i++) {
if (lists[i] == k&& bCheck[i]==false) {
if (del != i) {
dfs(i,cnt+1);
}
}
}
bFind = false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//트리 리프 노드
//자식의 개수가 0인 노드
//0
cin >> n;
int start;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
bCheck.push_back(false);
lists.push_back(tmp);
if (tmp == -1) {
start = i;
}
}
cin >> del;
if (del == start) {
cout << 0;
return 0;
}
dfs(start, 0);
cout << gcnt;
return 0;
}