oiClass-puji P1278-树和数tree

Horean0574 rp++

原题链接

Algorithms: 树结构


很水的一道题

只需要判断如果 父节点的优先级 子节点的优先级 就需要加括号。

但是当给左节点加括号时,且父节点与左节点的优先级相同时,括号可以省略。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

const int N = 1e5 + 5;

int n, m, a[N], ans;
int g[N][2];

void dfs(const int &u) {
for (int i = 0; i < 2; ++i) {
int v = g[u][i];
if (!g[u][i]) continue;
if (a[v] <= a[u]) { // 需要括号
++ans;
}
if (a[v] == a[u] && !i) { // 可以省略
--ans;
}
dfs(v);
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);

cin >> n >> m;
for (int i = 1; i <= n; ++i) {
int l, r, x;
cin >> l >> r >> x;
g[i][0] = l;
g[i][1] = r;
a[i] = x;
}

dfs(1);

cout << ans;
}
  • Title: oiClass-puji P1278-树和数tree
  • Author: Horean0574
  • Created at : 2023-08-25 11:48:42
  • Updated at : 2023-08-30 21:24:25
  • Link: https://blog.hxrch.top/2023/08/25/oiClass-puji-P1278-树和数tree/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
oiClass-puji P1278-树和数tree