社区讨论

10pts求条

P5658[CSP-S 2019] 括号树参与者 1已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mhjaqa8r
此快照首次捕获于
2025/11/03 23:29
4 个月前
此快照最后确认于
2025/11/03 23:29
4 个月前
查看原帖
CPP
// #include <bits/stdc++.h>
// #define endl '\n'
// using namespace std;
// vector<int> mp[500005];
// int n;
// char ss[500005];
// int ans = 0;    
// void dfs(int pos, string s)
// {
//     int ls = 0;
//     int ks = 0;
//     for (int i = 0; i < s.length(); i++)
//     {
//         if (s[i] == '(') ls++;
//         if (s[i] == ')')
//         {
//             if (ls)
//             {
//                 ks++;
//                 ls--;
//             }
//         }
//     }
//     ans ^= ks * pos;
//     for (auto k : mp[pos])
//     {
//         dfs(k, s + ss[k]);
//     }
// }
// signed main()
// {
//     ios::sync_with_stdio(false);
//     ios_base::sync_with_stdio(false);
//     cin.tie(nullptr);
//     cout.tie(nullptr);
//     cin >> n;
//     cin >> ss + 1;
//     for (int i = 2; i <= n; i++)
//     {
//         int f;
//         cin >> f;
//         mp[f].push_back(i);
//     }
//     string s;
//     s += ss[1];
//     dfs(1, s);
//     cout << ans << endl;
// }
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
vector<int> mp[500005];
int n;
char ss[500005];
int ans = 0;    
void dfs(int pos, int cl, int nums)
{
    ans ^= pos * nums;
    for (auto k : mp[pos])
    {
        if (ss[k] == ')') {
            if (cl) {
                dfs(k, cl - 1, nums + 1);
            } else {
                dfs(k, 0, nums);
            }
        }
        else
        {
            dfs(k, cl + 1, nums);
        }
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n;
    cin >> ss + 1;
    for (int i = 2; i <= n; i++)
    {
        int f;
        cin >> f;
        mp[f].push_back(i);
    }
    dfs(1, (ss[1] == '(' ? 1 : 0), 0);
    cout << ans << endl;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...