洛谷 P5658 括号树
\(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 括号树的更多相关文章
- 洛谷 P5658 括号树 题解
原题链接 简要题意: 求出以从每个节点到根形成的括号序列的合法对数. 算法一 观察到 \(n \leq 8\) ,所以我们可以用 纯粹的暴力 . 用 \(O(n)\) 时间得出当前节点到根的字符串. ...
- 2021.08.09 P5658 括号树(树形结构)
2021.08.09 P5658 括号树(树形结构) [P5658 CSP-S2019] 括号树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 太长,在链接中. 分析及代码 ...
- 洛谷1087 FBI树 解题报告
洛谷1087 FBI树 本题地址:http://www.luogu.org/problem/show?pid=1087 题目描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全 ...
- P5658 括号树
P5658 括号树 题解 太菜了啥都不会写只能水5分数据 啥都不会写只能翻题解 题解大大我错了 我们手动找一下规律 我们设 w[ i ] 为从根节点到结点 i 对答案的贡献,也就是走到结点 i ,合 ...
- 洛谷P3018 [USACO11MAR]树装饰Tree Decoration
洛谷P3018 [USACO11MAR]树装饰Tree Decoration树形DP 因为要求最小,我们就贪心地用每个子树中的最小cost来支付就行了 #include <bits/stdc++ ...
- 括号树 noip(csp??) 2019 洛谷 P5658
洛谷AC通道 本题,题目长,但是实际想起来十分简单. 首先,对于树上的每一个后括号,我们很容易知道,他的贡献值等于上一个后括号的贡献值 + 1.(当然,前提是要有人跟他匹配,毕竟题目中要求了,是不同的 ...
- NOIP2017提高组Day2T3 列队 洛谷P3960 线段树
原文链接https://www.cnblogs.com/zhouzhendong/p/9265380.html 题目传送门 - 洛谷P3960 题目传送门 - LOJ#2319 题目传送门 - Vij ...
- 洛谷P3703 [SDOI2017]树点涂色(LCT,dfn序,线段树,倍增LCA)
洛谷题目传送门 闲话 这是所有LCT题目中的一个异类. 之所以认为是LCT题目,是因为本题思路的瓶颈就在于如何去维护同颜色的点的集合. 只不过做着做着,感觉后来的思路(dfn序,线段树,LCA)似乎要 ...
- 洛谷P3372线段树1
难以平复鸡冻的心情,虽然可能在大佬眼里这是水题,但对蒟蒻的我来说这是个巨大的突破(谢谢我最亲爱的lp陪我写完,给我力量).网上关于线段树的题解都很玄学,包括李煜东的<算法竞赛进阶指南>中的 ...
随机推荐
- JS 测试 Prototype
JS 测试 Prototype 测试 JavaScript 框架库 - Prototype 引用 Prototype 如需测试 JavaScript 库,您需要在网页中引用它. 为了引用某个库,请使用 ...
- 【Beta阶段】第十周Scrum会议
[Beta阶段]第十周Scrum会议 本次会议为第十周第一次Scrum Meeting,会议对Alpha阶段的工作进行了反思总结,以及对Beta阶段工作的展望. 会议时间为2019.11.23.会议地 ...
- jenkins 分布式配置+allure集成+邮件发送
jenkins节点配置+allure集成+邮件发送这一套走下来感觉很麻烦,要配置的东西太多了,所以在此记录一下,防止以后忘了. 环境: 主机master:腾讯云服务器ubuntu18.04 执行机sl ...
- windows环境下Jmeter5.2的安装使用
一.安装配置JDK Jmeter5.2依赖JDK1.8+版本,JDK安装百度搜索JAVA下载JDK,地址:https://www.oracle.com/technetwork/java/javase/ ...
- Gerrit常见命令及最佳实践
概述 本文记录了笔者在使用Gerrit(一种免费.开放源代码的代码审查软件)过程中的一些微小的经验,在这里做个简单的分享. 克隆工程 git clone ssh://tusi@xx.xx.cn:294 ...
- Asp .Net Core Excel导入和导出
ASP .Net Core使用EPPlus实现Api导入导出,这里使用是EPPlus 4.5.2.1版本,.Net Core 2.2.在linux上运行的时候需要安装libgdiplus . 下面我们 ...
- ABP开发框架前后端开发系列---(14)基于Winform的ABP快速开发框架
前面介绍了很多ABP系列的文章,一步一步的把我们日常开发中涉及到的Web API服务构建.登录日志和操作审计日志.字典管理模块.省份城市的信息维护.权限管理模块中的组织机构.用户.角色.权限.菜单等内 ...
- PostgreSQL学习之路一
PostgreSQL的扩展PostGIS是最著名的开源GIS数据库. 安装PostgreSQL是第一步. 1.下载PostgreSQL的二进制安装文件 PostgreSQL官网–>Downloa ...
- Web前端基础(17):jQuery基础(四)
1. jQuery的属性操作 jquery的属性操作模块分为四个部分:html属性操作,dom属性操作,类样式操作和值操作 html属性操作:是对html文档中的属性进行读取,设置和移除操作.比如at ...
- 利用Python制作一个只属于和她的聊天器,再也不用担心隐私泄露啦!
------------恢复内容开始------------ 是否担心微信的数据流会被监视?是否担心你和ta聊天的小秘密会被保存到某个数据库里?没关系,现在我们可以用Python做一个只属于你和ta的 ...