\(50pts\)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int A = 5e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f; inline int read() {
char c = getchar(); int x = 0, f = 1;
for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
} struct node {
int nxt, to;
} e[A << 1];
int n, cnt, head[A], f[A], ans[A], top = 0;
char sta[A], ss[A], a[A]; inline void add(int from, int to) {
e[++cnt].to = to;
e[cnt].nxt = head[from];
head[from] = cnt;
} int jisuan() {
int tp = 0, res = 0;
for(int i = top; i >= 1; i--) {
if(sta[i] == ')') ++tp;
else {
if(tp > 0) tp--;
else break;
}
if(tp == 0) res++;
}
return res;
} void dfs(int now, int fa) {
for(int i = head[now]; i; i = e[i].nxt) {
int to = e[i].to;
sta[++top] = a[to];
if(a[to] == '(') ans[to] = ans[now];
else ans[to] = ans[now] + jisuan();
dfs(to, now);
top--;
}
} int main() {
n = read(); scanf("%s", a + 1);
for(int i = 2, x; i <= n; i++) {
x = read();
f[i] = x;
add(x, i);
}
sta[++top] = a[1];
dfs(1, 0);
int sum = 0;
for(int i = 1; i <= n; i++) {
sum ^= (i * ans[i]);
}
cout << sum << '\n';
return 0;
}

\(100pts\):

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define int long long
using namespace std; const int A = 5e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f; inline int read() {
char c = getchar(); int x = 0, f = 1;
for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
} struct node { int nxt, to; } e[A << 1];
int n, cnt, head[A], f[A], ans[A], top = 0, sum, sta[A];
char ss[A], a[A]; inline void add(int from, int to) {
e[++cnt].to = to;
e[cnt].nxt = head[from];
head[from] = cnt;
} void dfs(int now, int faans, int top) {
int tp = 0;
if(a[now] == '(') sta[++top] = now;
else if(a[now] == ')') {
if(top == 0) ans[now] = 0;
else ans[now] = ans[f[sta[top]]] + 1, tp = sta[top], faans += ans[now], top--;
}
sum = sum ^ (faans * now);
for(int i = head[now]; i; i = e[i].nxt) {
int to = e[i].to;
dfs(to, faans, top);
}
if(tp) sta[++top] = tp;
} signed main() {
n = read(); scanf("%s", a + 1);
for(int i = 2, x; i <= n; i++) {
x = read();
f[i] = x;
add(x, i);
}
dfs(1, 0, 0);
cout << sum << '\n';
return 0;
}

洛谷 P5658 括号树的更多相关文章

  1. 洛谷 P5658 括号树 题解

    原题链接 简要题意: 求出以从每个节点到根形成的括号序列的合法对数. 算法一 观察到 \(n \leq 8\) ,所以我们可以用 纯粹的暴力 . 用 \(O(n)\) 时间得出当前节点到根的字符串. ...

  2. 2021.08.09 P5658 括号树(树形结构)

    2021.08.09 P5658 括号树(树形结构) [P5658 CSP-S2019] 括号树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 太长,在链接中. 分析及代码 ...

  3. 洛谷1087 FBI树 解题报告

    洛谷1087 FBI树 本题地址:http://www.luogu.org/problem/show?pid=1087 题目描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全 ...

  4. P5658 括号树

    P5658 括号树 题解 太菜了啥都不会写只能水5分数据 啥都不会写只能翻题解  题解大大我错了 我们手动找一下规律 我们设 w[ i ] 为从根节点到结点 i 对答案的贡献,也就是走到结点 i ,合 ...

  5. 洛谷P3018 [USACO11MAR]树装饰Tree Decoration

    洛谷P3018 [USACO11MAR]树装饰Tree Decoration树形DP 因为要求最小,我们就贪心地用每个子树中的最小cost来支付就行了 #include <bits/stdc++ ...

  6. 括号树 noip(csp??) 2019 洛谷 P5658

    洛谷AC通道 本题,题目长,但是实际想起来十分简单. 首先,对于树上的每一个后括号,我们很容易知道,他的贡献值等于上一个后括号的贡献值 + 1.(当然,前提是要有人跟他匹配,毕竟题目中要求了,是不同的 ...

  7. NOIP2017提高组Day2T3 列队 洛谷P3960 线段树

    原文链接https://www.cnblogs.com/zhouzhendong/p/9265380.html 题目传送门 - 洛谷P3960 题目传送门 - LOJ#2319 题目传送门 - Vij ...

  8. 洛谷P3703 [SDOI2017]树点涂色(LCT,dfn序,线段树,倍增LCA)

    洛谷题目传送门 闲话 这是所有LCT题目中的一个异类. 之所以认为是LCT题目,是因为本题思路的瓶颈就在于如何去维护同颜色的点的集合. 只不过做着做着,感觉后来的思路(dfn序,线段树,LCA)似乎要 ...

  9. 洛谷P3372线段树1

    难以平复鸡冻的心情,虽然可能在大佬眼里这是水题,但对蒟蒻的我来说这是个巨大的突破(谢谢我最亲爱的lp陪我写完,给我力量).网上关于线段树的题解都很玄学,包括李煜东的<算法竞赛进阶指南>中的 ...

随机推荐

  1. TimeSpan的用法

    TimeSpan的属性和方法: 下面的列表涵盖了其中的一部分: 属性: Add:与另一个TimeSpan值相加. Days: 返回用天数计算的TimeSpan值.Hours: 返回用小时计算的Time ...

  2. Cnblogs图片无法上传

      2019年5月28日以前的两三个月时间,使用cnblogs原来的接口时,提示图片无法上传,空间不足,实在没办法了,自己实现了博客代理,发现上传图片时返回了503错误,只好先把图片传到其它服务器,再 ...

  3. Python Poetry 学习和使用

    Poetry是啥? 是一个Python虚拟环境和依赖管理工具,另外它还提供了包管理功能,比如打包和发布.可以用来管理python库和python程序. 安装Poetry curl -sSL https ...

  4. Vue小练习 02

    用table标签渲染下面的数据, 最后一列为总分, 第一列为排名 scores = [ {name: 'Bob', math: 97, chinese: 89, english: 67}, {name ...

  5. cisco ssh实验--附带配置脚本-2019.11.19

    cisco ssh实验

  6. SQL查询语法30例

    学好SQL查询:无他,概手熟耳. 1. 基础表: 学生表: 老师表: 课程表: 成绩表: 2. 题目: 1.查询名字中含有"华"字的学生信息 select * from 学生 wh ...

  7. Python3字典update()方法

    描述 Python字典update()函数把字典参数dict2的key/value(键/值)对更新到字典dict里. update()方法语法: dict.update(dict2) 参数 dict2 ...

  8. Elasticsearch 6.x版本全文检索学习之聚合分析入门

    1.什么是聚合分析? 答:聚合分析,英文为Aggregation,是es除搜索功能外提供的针对es数据做统计分析的功能.特点如下所示: a.功能丰富,提供Bucket.Metric.Pipeline等 ...

  9. SpringBoot无法访问webapp目录下的文件

    springboot version:2.1.9-RELEASE 解决方案: 在pom中添加此段 完美解决,代码的作用是让src/main/webapp在编译的时候在resoureces路径下也生成w ...

  10. .net Core 使用AutoMapper

    在我们的项目中慢慢的要把数据库的实体模型和视图模型进行分离,防止被人拿到我们表字段.在学校的时候自己只是有将很多数据库模型,写成一个视图模型返回到前台. 首先我们把这两个包引入项目中去. 然后我们创建 ...