-
카테고리
-
세부 분야
알고리즘 · 자료구조
-
해결 여부
미해결
테케는 통과하는데 왜 틀렸을까요
23.09.26 17:47 작성 조회수 145
0
#include<iostream>
#include<vector>
using namespace std;
int n,d,p,root,visited[54];
vector<int>adj[54];
int dfs(int here){
visited[here]=1;
int ret=0;
if(adj[here].size()==0)return 1;
for(int there:adj[here]){
if(visited[there])continue;
ret+=dfs(there);
}
return ret;
}
int main(){
cin >> n;
for(int i=0;i<n;i++){
cin >> p;
if(p==-1){
root=i;
continue;
}
adj[p].push_back(i);
}
cin >> d;
if(d==root)cout<<0<<"\n";
else{
visited[d]=1;
cout << dfs(root) << "\n";
}
}
답변을 작성해보세요.
답변 2