QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#809159 | #7216. Boxes on tree | Grand_Elf | WA | 1ms | 3992kb | C++17 | 2.3kb | 2024-12-11 12:40:54 | 2024-12-11 12:41:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int T, n, vn, p[N], lg[N], dep[N], vis[N], f[N][13];
long long ans;
bool used[N], on[N];
vector<int> e[N], id[N];
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1;
f[u][0] = fa;
for (int j = 1; j <= 12; j++) {
f[u][j] = f[f[u][j - 1]][j - 1];
}
for (int v : e[u]) {
if (v == fa) {
continue;
}
dfs(v, u);
}
}
int LCA(int u, int v) {
if (dep[u] < dep[v]) {
swap(u, v);
}
while (dep[u] > dep[v]) {
u = f[u][lg[dep[u] - dep[v]]];
}
if (u == v) {
return u;
}
for (int j = 12; j >= 0; j--) {
if (f[u][j] != f[v][j]) {
u = f[u][j];
v = f[v][j];
}
}
return f[u][0];
}
int dis(int u, int v) {
return dep[u] + dep[v] - 2 * dep[LCA(u, v)];
}
void col(int x);
void coln(int u) {
for (int v = f[u][0]; v; v = f[v][0]) {
if (!used[v]) {
col(vis[v]);
}
}
}
void col(int x) {
for (int u : id[x]) {
used[u] = 1;
}
for (int u : id[x]) {
coln(u);
}
}
void find(int u, int fa) {
on[u] = p[u] != u;
for (int v : e[u]) {
if (v == fa) {
continue;
}
find(v, u);
on[u] |= on[v];
}
}
void getans(int u, int fa) {
if (!used[u]) {
if (u > 1 && on[u]) {
ans += 2;
}
col(vis[u]);
}
for (int v : e[u]) {
if (v == fa) {
continue;
}
getans(v, u);
}
}
void work() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= n; i++) {
e[i].clear();
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
e[u].emplace_back(v);
e[v].emplace_back(u);
}
dfs(1, 0);
ans = 0;
for (int i = 1; i <= n; i++) {
ans += dis(i, p[i]);
}
for (int i = 1; i <= n; i++) {
vis[i] = 0;
used[i] = 0;
on[i] = 0;
}
for (int i = 1; i <= n; i++) {
if (vis[i]) {
continue;
}
vn++;
id[vn].clear();
for (int u = i; !vis[u]; u = p[u]) {
vis[u] = vn;
id[vn].emplace_back(u);
}
}
find(1, 0);
getans(1, 0);
cout << ans << '\n';
}
int main() {
// freopen("classic.in", "r", stdin);
// freopen("classic.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
for (int i = 2; i < N; i++) {
lg[i] = lg[i / 2] + 1;
}
// cin >> T;
T = 1;
while (T--) {
work();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3860kb
input:
3 1 3 2 1 2 2 3
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3860kb
input:
4 2 1 4 3 1 3 3 2 3 4
output:
6
result:
ok 1 number(s): "6"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3992kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3852kb
input:
10 1 6 3 4 5 2 7 8 9 10 10 8 8 6 2 1 6 5 7 6 3 4 5 1 8 9 4 8
output:
8
result:
ok 1 number(s): "8"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3920kb
input:
10 1 3 9 4 5 6 7 8 2 10 7 4 2 1 8 10 1 8 6 5 6 4 9 8 8 6 2 3
output:
10
result:
ok 1 number(s): "10"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
10 8 2 3 9 5 6 7 1 4 10 5 1 5 8 6 5 3 6 10 8 9 3 7 1 6 2 4 10
output:
20
result:
ok 1 number(s): "20"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3904kb
input:
10 1 2 3 5 4 6 7 8 9 10 1 6 1 4 9 2 5 4 10 7 1 3 10 1 9 3 10 8
output:
4
result:
ok 1 number(s): "4"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3948kb
input:
10 1 2 3 6 5 4 8 7 9 10 2 10 9 6 4 2 1 8 2 9 9 5 6 3 7 6 8 10
output:
18
result:
ok 1 number(s): "18"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3928kb
input:
10 6 9 4 1 2 10 3 7 5 8 7 9 10 9 3 1 10 2 8 6 4 10 8 7 4 3 4 5
output:
30
result:
ok 1 number(s): "30"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3920kb
input:
10 10 8 9 3 2 4 5 7 1 6 6 10 4 6 10 5 7 1 9 6 3 6 4 7 8 6 1 2
output:
32
result:
ok 1 number(s): "32"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3920kb
input:
10 8 3 9 6 5 1 2 10 4 7 8 6 7 9 1 9 10 2 10 8 10 3 9 5 7 2 4 8
output:
28
result:
ok 1 number(s): "28"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3988kb
input:
10 9 2 6 4 5 7 10 3 8 1 2 3 9 4 4 5 9 2 8 6 8 3 3 1 9 7 9 10
output:
20
result:
ok 1 number(s): "20"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
10 3 1 9 4 2 8 5 10 6 7 8 10 4 8 4 5 5 3 4 1 10 9 6 9 6 2 7 3
output:
32
result:
ok 1 number(s): "32"
Test #14:
score: -100
Wrong Answer
time: 0ms
memory: 3856kb
input:
20 1 2 13 4 5 3 7 8 9 10 11 14 6 12 15 17 16 18 19 20 14 19 10 12 8 18 8 1 18 9 4 8 2 4 11 4 17 7 16 12 4 17 6 3 13 5 6 11 8 20 5 3 19 13 6 12 15 1
output:
42
result:
wrong answer 1st numbers differ - expected: '34', found: '42'