\(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. html5的 history模式和hash模式

    直观区别 hash 带一个# history 没有# 各自特点 hash: 仅 hash 符号之前的内容会被包含在请求中,**因此对于后端来说,即使没有做到对路由的全覆盖,也不会返回 404 错误.* ...

  2. ViewAnimator

    ViewAnimator是一个基类,它继承了FrameLayout,因此它表现出FrameLayout的特征,可以将多View组件叠在一起.ViewAnimator额外增加的功能正如它的名字所暗示的, ...

  3. SpringBoot项目里,让TKmybatis支持可以手写sql的Mapper.xml文件

    SpringBoot项目通常配合TKMybatis或MyBatis-Plus来做数据的持久化. 对于单表的增删改查,TKMybatis优雅简洁,无需像传统mybatis那样在mapper.xml文件里 ...

  4. Ubuntu上安装python模块

    sudo apt-get install python-pip   ----先安装 pip模块 sudo pip install openpyxl ---通过pip安装python模块

  5. August 18th, 2019. Week 34th, Sunday

    Fear doesn't shut you down, it wakes you up. 恐惧不会消磨你的意志,它能激发你的潜能. We all know that fear is powerful, ...

  6. csp2019后的感慨

    你还记得曾经加入oi的初衷吗? ... 我们都不想输,可谁都没有赢... --前言 没有太大的感想,也不配去写感想...就记录一下初学者失败的原因吧.希望看过的人能引以为戒. 做题的时候,不到万不得已 ...

  7. python - selenium模块简介

    为什么要使用Selenium? 很多浏览器渲染页面的方式都很难找出其规律, 但是利用Selenium来驱动加载网页就可以直接拿到javaScript渲染后的结果了, 不需要再担心其相关的加密系统 声明 ...

  8. 【Java基础】Java中你必须知道的知识点

    目录 Java中面向对象的基础知识 1. 什么是面向对象  2. 三大基本特征和五项基本原则 3. Java的平台无关性 4. 值传递和引用传递 5. 方法重载和重写 6. 基本数据类型 7. 包装类 ...

  9. synchronized&volatile

    synchronized(JVM实现的锁) 通过这两个关键字,我们可以很容易的实现同步多个任务的行为,可以实现同一时刻,只能有一条线程去访问共享资源 一: 修饰普通方法 多个线程,共同去竞争访问,方法 ...

  10. Python调用Redis

    #!/usr/bin/env python # -*- coding:utf-8 -*- # ************************************* # @Time : 2019/ ...