This commit is contained in:
Zengtudor 2024-10-24 09:27:07 +08:00
parent dd20cc9fd8
commit 2d3a3ce03b

38
src/P5658/P5658.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using ll = long long;
const ll max_n = 5e5;
ll n, father;
std::string s;
std::vector<ll> son[max_n];
char c[max_n];
void dfs(const ll now, const std::stack<char> &parenthesis){
for(const auto i:son[now]){
auto new_parenthesis = parenthesis;
new_parenthesis.push(c[i]);
dfs(i, new_parenthesis);
}
}
int main(){
std::cin>>n;
std::cin>>s;
for(ll i{1};i<=n;i++){
c[i]=s[i-1];
}
for(ll i{2};i<=n;i++){
std::cin>>father;
son[father].push_back(i);
}
std::stack<char> first_parenthesis;
first_parenthesis.push(c[1]);
dfs(1, first_parenthesis);
}