QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#810157#7216. Boxes on treeLiuxizaiWA 1ms4920kbC++204.7kb2024-12-11 20:08:102024-12-11 20:08:19

Judging History

你现在查看的是最新测评结果

  • [2024-12-11 20:08:19]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4920kb
  • [2024-12-11 20:08:10]
  • 提交

answer

#include <bits/stdc++.h>
#define File(name) freopen(#name".in", "r", stdin); freopen(#name".out", "w", stdout);
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<typename T>
inline T read(){
    T n = 0; int f = 1; char ch = getchar();
    while(!isdigit(ch)){
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(isdigit(ch)){
        n = n * 10 + ch - '0';
        ch = getchar();
    }
    return f * n;
}
template<typename T>
void write(T n){
    if(n < 0) return putchar('-'), write(-n);
    if(n / 10) write(n / 10);
    putchar(n % 10 + '0');
}
void input() {}
template<typename Type, typename... Types>
void input(Type &arg, Types &...args){
    arg = read<Type>();
    input(args...);
}
namespace Main{
    const int N = 505;
    int t, n, p[N], fa[N], dep[N], dfn[N], siz[N], dfnCnt, st[13][N], ans;
    int bel[N], len[N];
    int e[N][N];
    bool vis[N];
    pair<int, int> mn[N];
    vector<int> g[N];
    vector<int> vec;
    inline int dmin(int u, int v) { return dep[u] < dep[v] ? u : v; }
    inline bool in_subt(int u, int r) { return dfn[u] >= dfn[r] && dfn[u] <= dfn[r] + siz[r] - 1; }
    void dfs(int u, int f){
        dep[u] = dep[f] + 1;
        siz[u] = 1;
        st[0][dfn[u] = ++dfnCnt] = f;
        for(int v: g[u]) if(v != f) dfs(v, u), siz[u] += siz[v];
    }
    int lca(int u, int v){
        if(u == v) return u;
        if(dfn[u] > dfn[v]) swap(u, v);
        int lg = log2(dfn[v] - dfn[u]);
        return dmin(st[lg][dfn[u] + 1], st[lg][dfn[v] - (1 << lg) + 1]);
    }
    int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
    int dis(int u, int s, int t){
        if(in_subt(s, u) ^ in_subt(t, u)) return 0;
        return min({dis(u, s), dis(u, t), dis(u, lca(s, t))});
    }
    void merge(int v, int u){
        bel[v] = u;
        vec.erase(find(vec.begin(), vec.end(), v));
        for(int i: vec) e[u][i] = min(e[u][i], e[v][i]);
        for(int i: vec) e[i][u] = min(e[i][u], e[i][v]);
    }
    void Main(){
        input(n);
        for(int i = 1; i <= n; i++) input(p[i]);
        for(int i = 1, u, v; i < n; i++){
            input(u, v);
            g[u].push_back(v), g[v].push_back(u);
        }
        dfs(1, 0);
        for(int i = 1; i < 13; i++){
            for(int j = 1; j + (1 << i) - 1 <= n; j++){
                st[i][j] = dmin(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
            }
        }
        memset(e, 0x3f, sizeof(e));
        for(int i = 1; i <= n; i++){
            if(bel[i]) continue;
            bel[i] = i, len[i] = 1;
            for(int j = p[i]; j != i; j = p[j]) bel[j] = i, len[i]++;
        }
        vec = {1};
        for(int i = 2; i <= n; i++) if(bel[i] == i && len[i] > 1) vec.push_back(i);
        for(int i = 1; i <= n; i++){
            if(bel[i] != 1 && len[bel[i]] > 1){
                for(int j = 1; j <= n; j++){
                    e[bel[i]][bel[j]] = min(e[bel[i]][bel[j]], 2 * dis(i, j, p[j]));
                }
            }
        }
        vis[1] = true;
        for(int i: vec) if(i != 1){
            mn[i] = {1e9, -1};
            for(int j: vec) if(i != j) mn[i] = min(mn[i], make_pair(e[i][j], j));
        }
        iota(bel + 1, bel + n + 1, 1);
        for(int i = 1; i <= n; i++) ans += dis(i, p[i]);
        while(true){
            for(int i: vec) if(i != 1) vis[i] = false, mn[i].second = bel[mn[i].second];
            bool ok = true;
            vector<int> _vec(vec);
            for(int i: _vec){
                if(i == 1) continue;
                vector<int> path;
                int p = i;
                while(!vis[p]){
                    vis[p] = true;
                    path.push_back(p);
                    p = mn[p].second;
                }
                auto it = find(path.begin(), path.end(), p);
                if(it != path.end()){
                    ok = false;
                    int s = *it;
                    while(it != path.end()){
                        for(int j: vec) e[*it][j] -= mn[*it].first;
                        ans += mn[*it].first;
                        if(*it != s) merge(*it, s);
                        it++;
                    }
                    mn[s] = {1e9, -1};
                    for(int j: vec) if(j != s) mn[s] = min(mn[s], make_pair(e[s][j], j));
                }
            }
            if(ok){
                for(int i: vec) if(i != 1) ans += mn[i].first;
                break;
            }
        }
        write(ans), puts("");
        return;
    }
} // namespace Main
int main(){
#ifdef Liuxizai
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif // Liuxizai
    Main::Main();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4664kb

input:

3
1 3 2
1 2
2 3

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 4712kb

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: 4712kb

input:

1
1

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 1ms
memory: 4580kb

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: 4772kb

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: 1ms
memory: 4632kb

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: 1ms
memory: 4852kb

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: 1ms
memory: 4784kb

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: 1ms
memory: 4852kb

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: 1ms
memory: 4696kb

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: 1ms
memory: 4904kb

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: 4692kb

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: 1ms
memory: 4660kb

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: 0
Accepted
time: 1ms
memory: 4916kb

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:

34

result:

ok 1 number(s): "34"

Test #15:

score: 0
Accepted
time: 1ms
memory: 4776kb

input:

20
1 19 3 4 5 2 7 13 9 10 11 14 8 18 15 16 17 12 6 20
14 7
11 17
16 5
20 14
8 14
15 13
18 2
6 18
11 4
9 7
7 4
5 18
12 9
15 18
10 12
11 2
16 1
18 19
1 3

output:

44

result:

ok 1 number(s): "44"

Test #16:

score: 0
Accepted
time: 1ms
memory: 4916kb

input:

20
1 2 3 4 19 6 7 8 9 10 11 12 15 14 13 16 17 18 5 20
11 16
9 18
2 11
19 1
7 12
20 16
13 20
10 17
7 2
16 18
18 15
16 14
2 6
1 7
10 6
8 5
5 13
4 16
3 14

output:

26

result:

ok 1 number(s): "26"

Test #17:

score: 0
Accepted
time: 1ms
memory: 4920kb

input:

20
1 2 12 4 13 7 6 8 17 10 11 14 16 5 15 19 9 18 3 20
4 6
19 14
6 9
8 9
2 19
14 9
6 16
5 9
14 13
10 7
15 20
2 20
12 1
19 3
7 19
17 2
4 11
14 12
18 12

output:

36

result:

ok 1 number(s): "36"

Test #18:

score: 0
Accepted
time: 1ms
memory: 4780kb

input:

20
12 2 3 4 5 18 7 8 17 10 19 1 13 14 15 11 9 6 16 20
5 11
9 15
17 2
2 8
2 20
11 7
19 9
10 11
10 13
14 10
19 11
20 3
20 6
1 10
9 6
18 12
11 16
11 12
1 4

output:

30

result:

ok 1 number(s): "30"

Test #19:

score: 0
Accepted
time: 1ms
memory: 4784kb

input:

20
15 20 17 12 2 6 14 13 4 3 1 7 9 19 10 16 5 8 18 11
3 20
15 6
17 1
6 5
17 11
13 14
12 4
4 14
2 16
12 16
15 1
13 19
12 6
9 1
1 18
1 8
20 8
7 5
5 10

output:

76

result:

ok 1 number(s): "76"

Test #20:

score: 0
Accepted
time: 1ms
memory: 4776kb

input:

20
2 10 12 13 7 1 19 18 17 15 14 5 6 4 16 9 8 11 3 20
15 10
1 4
7 20
9 16
20 15
3 19
17 20
11 7
17 14
4 6
6 11
6 3
15 2
10 12
18 10
8 11
14 13
9 11
5 14

output:

82

result:

ok 1 number(s): "82"

Test #21:

score: 0
Accepted
time: 1ms
memory: 4584kb

input:

20
10 4 1 15 3 9 11 14 13 2 17 6 7 5 20 16 19 8 12 18
8 17
9 15
7 16
9 17
13 14
4 14
12 10
19 17
15 1
15 16
17 5
20 12
10 19
20 3
3 6
19 18
14 10
2 1
16 11

output:

92

result:

ok 1 number(s): "92"

Test #22:

score: 0
Accepted
time: 1ms
memory: 4852kb

input:

20
3 7 6 11 15 16 12 13 19 5 14 2 20 9 10 8 1 17 18 4
9 12
7 2
1 12
16 1
14 15
11 5
2 10
8 5
9 11
3 7
7 5
20 1
11 18
14 16
2 19
13 15
10 17
8 6
10 4

output:

114

result:

ok 1 number(s): "114"

Test #23:

score: 0
Accepted
time: 1ms
memory: 4704kb

input:

20
11 12 6 5 4 18 10 16 2 19 14 1 9 7 3 17 13 8 20 15
13 11
7 3
19 11
12 4
17 15
19 1
16 20
8 16
4 13
8 12
8 6
17 1
13 14
18 11
10 17
17 2
1 5
4 3
9 8

output:

106

result:

ok 1 number(s): "106"

Test #24:

score: 0
Accepted
time: 1ms
memory: 4656kb

input:

50
1 2 3 4 28 6 7 8 9 10 11 12 13 27 38 16 19 15 50 21 33 22 37 24 25 26 14 5 30 34 31 48 20 29 35 36 23 18 39 40 41 42 43 44 45 46 47 32 49 17
39 13
11 26
8 1
45 35
35 3
49 23
31 9
34 21
49 43
3 48
20 36
45 8
44 21
42 11
44 4
33 29
47 46
22 9
46 43
48 5
16 1
20 4
1 10
11 29
41 4
24 15
16 17
15 42
2...

output:

174

result:

ok 1 number(s): "174"

Test #25:

score: 0
Accepted
time: 1ms
memory: 4712kb

input:

50
1 2 43 37 5 40 7 8 26 16 11 12 13 4 15 3 17 47 19 20 6 22 23 24 28 27 25 9 29 10 31 32 33 34 35 36 45 42 39 21 41 38 14 44 30 46 18 48 49 50
2 38
23 5
18 1
29 9
47 48
36 6
45 5
32 24
5 18
32 33
49 16
23 19
50 44
16 47
40 50
48 17
12 1
33 2
33 15
33 29
31 45
11 33
22 50
29 1
1 21
39 10
50 20
26 47...

output:

180

result:

ok 1 number(s): "180"

Test #26:

score: -100
Wrong Answer
time: 1ms
memory: 4700kb

input:

50
22 2 3 4 36 6 7 25 12 10 11 9 13 14 27 16 17 18 34 20 31 1 23 26 8 35 40 28 29 30 21 32 33 19 24 5 37 38 39 15 41 42 43 44 45 46 47 48 49 50
13 8
39 1
30 10
43 26
4 23
13 46
22 47
13 33
41 42
32 6
14 48
39 10
23 3
1 18
11 20
12 44
45 50
6 23
40 37
21 7
25 31
42 26
33 10
28 3
10 42
44 6
44 8
34 14...

output:

136

result:

wrong answer 1st numbers differ - expected: '134', found: '136'