QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#809174#7216. Boxes on treeKellyWLJWA 1ms4308kbC++175.3kb2024-12-11 12:49:052024-12-11 12:49:05

Judging History

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

  • [2024-12-11 12:49:05]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4308kb
  • [2024-12-11 12:49:05]
  • 提交

answer

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;

using pii = pair<int, int>;
const int N = 5010, SZ = (1 << 18) + 5;
static char buf[SZ], *bgn = buf, *til = buf;
char getc() {
    if(bgn == til)  bgn = buf, til = buf + fread(buf, 1, SZ, stdin);
    return bgn == til ? EOF : *bgn++;
}
template<typename T>
void read(T &x) {
    char ch = getc();  T fh = 0;   x = 0;
    while(ch < '0' || ch > '9')    fh |= (ch == '-'), ch = getc();
    while(ch >= '0' && ch <= '9')   x = x * 10 + ch - '0', ch = getc();
    x = fh ? -x : x;
}
template<typename Type, typename... argc>
void read(Type &x, argc &...args)   {read(x), read(args...);}
template<typename T>
void print(T x) {
    if(x < 0)  putchar('-'), x = -x;
    if(x / 10)  print(x / 10);
    putchar(x % 10 + '0');
}
int n, u, v, a[N], cnt, fr[N], vis[N], fa[N], bk[N], tmp[N], ans, sum, book[N], nxt[N], val[N], dis[N], que[N], hh, tt, idx;
vector<int> edge[N], cir[N], has[N], in[N];
void clear() {
    for(int i = 1; i <= cnt; ++i)   cir[i].clear(), has[i].clear(), in[i].clear();
    for(int i = 1; i <= n; ++i) fr[i] = vis[i] = book[i] = 0, edge[i].clear();
    cnt = ans = sum = idx = 0;
}
struct DSU {
    int sum[N], pa[N];
    void init() {for(int i = 1; i <= n; ++i)    sum[i] = 0, pa[i] = i;}
    pii find(int x) {
        if(pa[x] == x)  return {x, 0};
        pii res = find(pa[x]);  pa[x] = res.fi, sum[x] += res.se;   return {res.fi, sum[x]};
    }
    void merge(int x, int y, int v) {
        if(!x || !y)    return;
        x = find(x).fi, y = find(y).fi;
        if(x != y)  pa[x] = y, sum[x] += v;
    }
}dsu, pnt;
void dfs(int x, int fath) {
    fa[x] = fath;
    for(int y : edge[x])    if(y != fath)   dfs(y, x);
}
void bfs(int str) {
    hh = 1, tt = 0;
    memset(dis, 0x3f, sizeof(dis)); 
    for(int st : in[str])
        for(int x : has[st])    if(dis[x])   que[++tt] = x, dis[x] = 0;
    while(hh <= tt) {
        int x = que[hh++];
        auto [t, v] = dsu.find(fr[x]);  v = pnt.find(fr[x]).se;
        if(book[fr[x]] && t != str) {
            if(val[t] > dis[x] - v) val[t] = dis[x] - v, nxt[t] = str;
        }
        for(int y : edge[x])    
            if(dis[y] > dis[x] + 1) dis[y] = dis[x] + 1, que[++tt] = y;
    }
}
void mst() {
    dsu.init(), pnt.init(), idx = cnt;
    int num = cnt;
    for(int i = 1; i <= cnt; ++i)   book[i] = 1, in[i].pb(i);
    book[1] = 0;
    while(num > 1) {
        for(int i = 1; i <= idx; ++i)   val[i] = N, nxt[i] = 0;
        for(int i = 1; i <= idx; ++i) if(dsu.find(i).fi == i)   bfs(i);
        memset(vis, 0, sizeof(vis)), memset(bk, 0, sizeof(bk));
        // for(int i = 1; i <= idx; ++i)   
        //     if(dsu.find(i).fi == i)    cerr << nxt[i] << " " << val[i] << " " << "\n";
        int ct = 0, pre = idx;
        num = 0;
        for(int i = 1; i <= idx; ++i)   tmp[i] = dsu.find(i).fi;
        for(int i = 2; i <= pre; ++i)   if(dsu.find(i).fi == i && !vis[i]) {
            int x = i, las = x;  ++ct;
            while(x && !vis[x]) dsu.merge(x, nxt[x], 0), sum += nxt[x] ? val[x] : 0, vis[x] = ct, las = x, x = nxt[x];
            if(x && vis[x] == ct) {
                int nw = nxt[x];    ++idx, tmp[idx] = idx, ++num;
                while(nw != x)  dsu.merge(nw, idx, 0), bk[nw] = 1, pnt.merge(nw, idx, val[nw]), nw = nxt[nw];
                dsu.merge(x, idx, 0), bk[x] = 1, pnt.merge(x, idx, val[x]);
            }
            else if(!x) bk[las] = 1, ++num;
        }
        for(int i = 1; i <= idx; ++i)   in[i].clear();
        for(int i = 1; i <= cnt; ++i)   in[dsu.find(i).fi].pb(i);
        // for(int i = 1; i <= idx; ++i)   if(dsu.find(i).fi == i) ++num;
        // for(int i = 1; i <= idx; ++i)   cerr << bk[i] << " ";
        // cerr << "\n";
        for(int i = 2; i <= cnt; ++i)   if(book[i] && !bk[tmp[i]])   book[i] = 0;
        // for(int i = 1; i <= idx; ++i)   cerr << dsu.find(i).fi << " ";
        // cerr << "\n" << num << "\n";
        // for(int i = 1; i <= cnt; ++i)   cerr << book[i] << " ";
        // cerr << "\n";  
    }
}
void mian() {
    read(n), clear();
    for(int i = 1; i <= n; ++i) read(a[i]);
    for(int i = 1; i < n; ++i)  read(u, v), edge[u].pb(v), edge[v].pb(u);
    for(int i = 1; i <= n; ++i) if((i == 1 || i != a[i]) && !vis[i]) {
        ++cnt;  int x = i;
        while(a[x] != i)    cir[cnt].pb(x), x = a[x];
        cir[cnt].pb(x);
        for(int j : cir[cnt])   vis[j] = 1, fr[j] = cnt;
    }
    for(int i = 1; i <= cnt; ++i) {
        cir[i].pb(cir[i][0]);
        memset(vis, 0, sizeof(vis));
        for(int j = 0; j + 1 < cir[i].size(); ++j) {
            dfs(cir[i][j + 1], 0);
            for(int x = cir[i][j]; x; x = fa[x]) {
                ++ans;
                if(!vis[x]) vis[x] = 1, has[i].pb(x);
            }
            --ans;
        }
    }
    // cerr << cnt << "\n";
    mst();
    // cerr << ans << " " << sum << "\n";
    print(ans + sum * 2), putchar('\n');
}
int main() {
    #ifdef Kelly
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        freopen("err.txt", "w", stderr);
    #else
        // freopen("classic.in", "r", stdin);
        // freopen("classic.out", "w", stdout);
    #endif
    int T = 1;  // read(T);
    while(T--)  mian();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

input:

1
1

output:

0

result:

ok 1 number(s): "0"

Test #4:

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

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

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

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

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

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

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

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

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

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

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

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:

30

result:

wrong answer 1st numbers differ - expected: '34', found: '30'