0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 19:25:25 +00:00

Compare commits

...

4 Commits

14 changed files with 298 additions and 0 deletions

88
LibreOJ/144/144.cpp Normal file
View File

@ -0,0 +1,88 @@
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e6 + 5;
int n, m, r, v[N];
long long f[N];
int cnt, in[N], out[N];
std::vector<int> g[N];
int lowbit(int x) {
return x & -x;
}
void add(int x, int y) {
for (int i = x; i <= n; i += lowbit(i)) {
f[i] += y;
}
}
long long query(int x) {
long long res = 0;
for (int i = x; i; i -= lowbit(i)) {
res += f[i];
}
return res;
}
void dfs(int u, int f) {
in[u] = ++cnt;
add(cnt, v[u]);
for (int v : g[u]) {
if (v == f) continue;
dfs(v, u);
}
out[u] = cnt;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> r;
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(r, 0);
while (m--) {
int op;
cin >> op;
if (op == 1) {
int a, x;
cin >> a >> x;
add(in[a], x);
} else { // op == 2
int a;
cin >> a;
cout << query(out[a]) - query(in[a] - 1) << endl;
}
}
return 0;
}

BIN
LibreOJ/144/data/144_1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/144/data/144_1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/144/data/144_2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/144/data/144_2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/144/data/144_3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/144/data/144_3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/144/data/144_4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/144/data/144_4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/144/data/144_5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/144/data/144_5.out (Stored with Git LFS) Normal file

Binary file not shown.

61
Luogu/B3642/B3642.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <iostream>
#include <algorithm>
#include <iterator>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e6 + 5;
int n;
int lch[N], rch[N];
int cnt_pre, pre[N],
cnt_in, in[N],
cnt_post, post[N];
void pre_order(int u) {
pre[++cnt_pre] = u;
if (lch[u]) pre_order(lch[u]);
if (rch[u]) pre_order(rch[u]);
}
void in_order(int u) {
if (lch[u]) in_order(lch[u]);
in[++cnt_in] = u;
if (rch[u]) in_order(rch[u]);
}
void post_order(int u) {
if (lch[u]) post_order(lch[u]);
if (rch[u]) post_order(rch[u]);
post[++cnt_post] = u;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> lch[i] >> rch[i];
}
pre_order(1);
in_order(1);
post_order(1);
std::copy_n(pre + 1, n, std::ostream_iterator<int>(cout, " "));
cout << endl;
std::copy_n(in + 1, n, std::ostream_iterator<int>(cout, " "));
cout << endl;
std::copy_n(post + 1, n, std::ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
}

49
Luogu/B4016/B4016.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
int n, dep[N];
std::vector<int> g[N];
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1;
for (int v : g[u]) {
if (v == fa) continue;
dfs(v, u);
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1, 0);
int u = std::max_element(dep + 1, dep + n + 1) - dep;
std::fill(std::begin(dep), std::end(dep), 0);
dfs(u, 0);
cout << *std::max_element(dep + 1, dep + n + 1) - 1 << endl;
return 0;
}

70
Luogu/P1395/P1395.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <iostream>
#include <numeric>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 5e4 + 5;
int n;
int siz[N], // siz: 子树大小(所有子树上节点数 + 该节点)
weight[N]; // weight: 所有子树大小最大值
int centroid; // centroid: 重心
int dep[N];
std::vector<int> g[N];
void dfs1(int u, int f) {
siz[u] = 1;
weight[u] = 0;
for (int v : g[u]) {
if (v == f) continue;
dfs1(v, u);
siz[u] += siz[v];
weight[u] = std::max(weight[u], siz[v]);
}
// 顶上也算一棵子树
weight[u] = std::max(weight[u], n - siz[u]);
// 记录重心
if (centroid == 0) centroid = u;
else if (weight[u] < weight[centroid]) centroid = u;
// 重心相同,取编号小的
else if (weight[u] == weight[centroid] && u < centroid) centroid = u;
}
void dfs2(int u, int f) {
dep[u] = dep[f] + 1;
for (int v : g[u]) {
if (v == f) continue;
dfs2(v, u);
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs1(1, 0);
dfs2(centroid, 0);
cout << centroid << ' ' << std::accumulate(dep + 1, dep + 1 + n, 0) - n << endl;
return 0;
}