mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-24 03:31:59 +00:00
parent
b4b5c2040e
commit
4db607fadb
68
S2OJ/1538/1538.cpp
Normal file
68
S2OJ/1538/1538.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 3005;
|
||||
|
||||
int n, root, max_dep;
|
||||
long long ans;
|
||||
std::vector<int> g[N];
|
||||
std::array<int, N> f1, f2, cnt;
|
||||
|
||||
void dfs(int u, int f, int d) {
|
||||
cnt[d]++;
|
||||
max_dep = std::max(max_dep, d);
|
||||
|
||||
for (int v : g[u]) {
|
||||
if (v == f) continue;
|
||||
|
||||
dfs(v, u, d + 1);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n;
|
||||
|
||||
for (int i = 1, x; i <= n; i++) {
|
||||
cin >> x;
|
||||
|
||||
if (x) {
|
||||
g[x].emplace_back(i);
|
||||
g[i].emplace_back(x);
|
||||
} else {
|
||||
root = i;
|
||||
}
|
||||
}
|
||||
|
||||
dfs(root, 0, 1);
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
std::fill(f1.begin(), f1.end(), 0);
|
||||
std::fill(f2.begin(), f2.end(), 0);
|
||||
|
||||
for (int u : g[i]) {
|
||||
max_dep = 0;
|
||||
std::fill(cnt.begin(), cnt.end(), 0);
|
||||
|
||||
dfs(u, i, 1);
|
||||
|
||||
for (int j = 1; j <= max_dep; j++) {
|
||||
ans += f1[j] * cnt[j];
|
||||
f1[j] += f2[j] * cnt[j];
|
||||
f2[j] += cnt[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << endl;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
S2OJ/1538/data/problem.conf
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/problem.conf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree1.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree1.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree10.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree10.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree11.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree11.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree11.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree11.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree12.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree12.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree12.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree12.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree13.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree13.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree13.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree13.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree14.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree14.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree14.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree14.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree15.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree15.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree15.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree15.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree16.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree16.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree16.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree16.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree17.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree17.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree17.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree17.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree18.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree18.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree18.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree18.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree19.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree19.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree19.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree19.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree2.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree2.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree20.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree20.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree20.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree20.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree3.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree3.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree4.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree4.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree5.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree5.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree6.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree6.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree6.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree7.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree7.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree7.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree8.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree8.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree8.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree9.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1538/data/tinytree9.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1538/data/tinytree9.out
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user