QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#901394#4261. 胡策的小树Crying100 ✓802ms68584kbC++141.7kb2025-02-15 21:31:422025-02-15 21:31:43

Judging History

This is the latest submission verdict.

  • [2025-02-15 21:31:43]
  • Judged
  • Verdict: 100
  • Time: 802ms
  • Memory: 68584kb
  • [2025-02-15 21:31:42]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
const int N = 5e5+10;
int n,p[N],q[N];  db ans;
int dfn[N],ord[N],sz[N],dep[N],dtot;
int rt[N]; vector<int> e[N];

void dfs(int u){
    dfn[u] = ++dtot,ord[dtot] = u,sz[u] = 1;
    for(auto v : e[u]){
        dep[v] = dep[u] + 1;
        dfs(v);
        sz[u] += sz[v];
    }
}
db w[N],vt[N],k[N],y[N];

void calc(int x){
    int rt = ::rt[x],L = dfn[rt],R = dfn[rt] + sz[rt] - 1; 
    w[rt] = 0; for(int i=L+1;i<=R;i++)w[ord[i]] = 1.00 * ((q[ord[i]]+x)%n) / n;
    
    for(int i=L+1;i<=R;i++){
        int u = ord[i];
        vt[u] = sz[u] / (1-w[u]); //xu = vt[u] * (yu - yfa)
    }
    for(int i=R;i>L;i--){
        int u = ord[i];
        db KL = 0,KR = 0; //yu 的系数 和 yfa 的系数
        KL += vt[u],KR += vt[u];
        KL += -1;
        for(auto v : e[u]){
            KL -= w[v]*vt[v]*(k[v]-1);
        }
        k[u] = KR / KL;
    }
    //回代求出每个点是y_rt的多少倍
    //y_rt = 1/sz_rt * x_rt,借此解出y[rt],再回代解出y
    k[rt] = 1; db sum = sz[rt];
    for(int i=L+1;i<=R;i++){
        int u = ord[i];
        k[u] *= k[p[u]];
        sum += vt[u] * (k[u] - k[p[u]]);
    }
    //sum * y_rt = 1
    y[rt] = 1/sum;

    db res = 0;
    for(int i=L+1;i<=R;i++){
        int u = ord[i];
        y[u] = k[u] * y[rt];

        db X = vt[u] * (y[u] - y[p[u]]);
        res += X * w[u];
    } 
    ans = max(ans,res);
}

int main(){
    scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&p[i]),e[p[i]].push_back(i);
    for(int i=1;i<=n;i++)cin>>q[i],rt[(n-q[i])%n] = i;
    dfs(1);
    
    for(int x=0;x<n;x++)calc(x);

    printf("%.9Lf\n",ans);
    return 0;
}

詳細信息


Pretests


Final Tests

Test #1:

score: 5
Accepted
time: 2ms
memory: 32512kb

input:

2
0 1
0 1

output:

0.250000000

result:

ok found '0.250000000', expected '0.250000000', error '0.000000000'

Test #2:

score: 5
Accepted
time: 1ms
memory: 32596kb

input:

2
0 1
1 0

output:

0.250000000

result:

ok found '0.250000000', expected '0.250000000', error '0.000000000'

Test #3:

score: 5
Accepted
time: 2ms
memory: 32596kb

input:

4
0 1 1 1
0 2 1 3

output:

0.264705882

result:

ok found '0.264705882', expected '0.264705882', error '0.000000000'

Test #4:

score: 5
Accepted
time: 1ms
memory: 32728kb

input:

5
0 1 1 2 1
0 1 4 2 3

output:

0.295890411

result:

ok found '0.295890411', expected '0.295890411', error '0.000000000'

Test #5:

score: 5
Accepted
time: 0ms
memory: 34644kb

input:

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

output:

0.398403562

result:

ok found '0.398403562', expected '0.398403562', error '0.000000000'

Test #6:

score: 5
Accepted
time: 1ms
memory: 34640kb

input:

100
0 1 2 3 1 5 1 1 1 2 3 5 1 9 8 3 14 16 7 11 16 1 1 21 1 17 17 9 5 6 13 28 16 29 21 1 32 21 27 7 33 32 4 12 25 1 4 26 10 36 29 44 50 48 34 1 29 46 23 19 51 35 53 43 29 56 58 51 9 46 43 5 46 41 72 22 69 1 56 38 25 75 49 24 58 15 41 68 48 1 3 84 47 82 5 74 7 14 58 12
0 90 91 95 55 76 38 70 74 68 73 ...

output:

0.522265079

result:

ok found '0.522265079', expected '0.522265079', error '0.000000000'

Test #7:

score: 5
Accepted
time: 0ms
memory: 32676kb

input:

500
0 1 1 1 3 1 3 4 1 7 2 5 9 13 7 13 15 1 15 3 1 5 11 3 19 1 18 16 1 26 13 27 13 25 7 33 25 20 30 28 35 34 7 31 17 11 1 47 31 7 17 30 41 29 35 10 37 39 25 31 31 1 31 29 21 14 25 43 39 46 9 66 19 21 33 63 49 10 73 55 1 80 73 62 45 65 39 13 74 9 3 7 84 82 63 78 31 37 47 56 30 43 28 30 66 55 85 11 33 ...

output:

0.532967175

result:

ok found '0.532967175', expected '0.532967175', error '0.000000000'

Test #8:

score: 5
Accepted
time: 1ms
memory: 32624kb

input:

1000
0 1 1 1 3 1 6 5 1 9 1 5 1 1 7 8 1 2 16 13 1 8 17 8 1 11 5 16 8 1 1 5 32 1 17 11 19 5 13 23 23 24 15 43 20 40 37 42 25 7 12 16 7 52 23 15 17 6 18 58 37 46 53 36 41 6 34 3 43 37 33 1 60 25 63 45 31 41 49 22 33 55 52 24 33 81 80 80 65 14 31 84 27 1 23 26 75 32 37 34 52 36 1 29 22 17 86 69 29 98 29...

output:

0.620843720

result:

ok found '0.620843720', expected '0.620843720', error '0.000000000'

Test #9:

score: 5
Accepted
time: 2ms
memory: 32756kb

input:

1500
0 1 1 2 2 1 4 1 5 9 8 10 9 9 7 7 13 11 5 12 15 11 1 5 17 22 8 12 8 10 15 19 25 10 34 1 21 31 19 16 1 36 3 37 1 1 11 5 13 47 39 31 50 18 41 33 30 8 39 27 37 11 35 39 21 10 55 10 2 24 29 17 28 54 11 67 72 73 70 79 72 1 55 77 83 39 24 21 77 30 77 79 69 88 1 65 83 33 27 89 41 21 91 36 86 46 1 98 10...

output:

0.554465873

result:

ok found '0.554465873', expected '0.554465873', error '0.000000001'

Test #10:

score: 5
Accepted
time: 1ms
memory: 34656kb

input:

2000
0 1 2 3 3 1 4 4 5 1 3 4 1 4 3 6 11 6 3 18 1 16 7 14 13 1 1 13 25 1 13 27 17 7 29 16 13 4 13 27 13 39 12 20 39 32 12 5 1 29 34 18 11 36 35 13 1 28 44 49 4 29 44 52 53 3 39 57 23 16 1 24 9 57 51 24 33 39 37 75 32 55 34 14 49 43 12 61 25 87 65 77 71 16 29 52 1 46 3 31 88 6 33 41 49 4 91 79 49 10 1...

output:

0.585554985

result:

ok found '0.585554985', expected '0.585554985', error '0.000000000'

Test #11:

score: 5
Accepted
time: 14ms
memory: 35300kb

input:

20000
0 1 1 1 3 4 2 4 6 1 9 5 1 12 2 1 14 5 3 16 8 20 9 18 24 6 9 1 25 15 11 12 17 29 17 6 3 8 7 9 11 14 23 6 21 19 26 16 43 20 46 44 26 32 37 26 15 7 47 20 33 37 13 1 47 31 40 66 57 54 23 57 37 51 15 61 28 19 6 54 17 48 59 10 43 50 14 82 80 29 71 62 19 52 17 29 67 91 92 97 66 63 63 69 23 101 6 91 6...

output:

0.618312159

result:

ok found '0.618312159', expected '0.618312159', error '0.000000000'

Test #12:

score: 5
Accepted
time: 37ms
memory: 40300kb

input:

50000
0 1 1 1 1 1 1 1 7 6 1 8 8 1 1 11 3 17 13 16 19 19 12 14 5 2 22 16 16 25 7 23 1 25 1 33 7 20 31 25 25 17 9 14 15 36 27 16 13 1 20 18 26 40 37 35 55 40 58 11 25 15 27 56 39 64 3 56 13 67 35 29 69 1 5 61 68 50 49 17 65 10 16 68 64 61 43 61 38 77 65 67 9 28 9 31 58 53 27 87 97 72 44 47 81 100 60 3...

output:

0.647595328

result:

ok found '0.647595328', expected '0.647595328', error '0.000000000'

Test #13:

score: 5
Accepted
time: 64ms
memory: 41556kb

input:

80000
0 1 1 1 3 1 4 4 5 2 1 6 1 4 8 11 1 1 13 17 5 12 17 4 24 21 19 25 1 22 16 17 25 33 23 23 1 32 26 24 15 12 29 17 29 8 26 29 1 7 41 43 27 25 1 12 15 34 10 42 36 5 34 52 55 21 53 59 67 31 53 17 25 49 52 61 22 48 13 47 73 51 35 33 15 36 75 74 37 9 23 47 49 38 61 1 27 34 38 61 61 48 49 69 59 11 9 10...

output:

0.660625561

result:

ok found '0.660625561', expected '0.660625561', error '0.000000000'

Test #14:

score: 5
Accepted
time: 86ms
memory: 44416kb

input:

100000
0 1 1 1 3 5 3 2 8 7 1 4 7 2 1 6 13 17 1 1 9 4 1 19 9 6 1 8 13 22 13 1 12 8 1 7 26 2 35 31 3 23 32 39 41 13 19 35 37 40 11 42 45 14 20 41 43 47 43 16 10 54 21 1 5 9 55 15 1 45 49 59 46 17 35 70 21 56 17 18 29 1 73 25 29 2 43 67 37 40 21 15 50 66 43 8 81 66 21 70 94 99 5 64 49 25 71 34 97 50 53...

output:

0.662235018

result:

ok found '0.662235018', expected '0.662235018', error '0.000000000'

Test #15:

score: 5
Accepted
time: 322ms
memory: 56368kb

input:

250000
0 1 1 1 2 1 1 2 1 4 7 3 4 11 1 4 1 12 1 19 17 18 20 23 1 18 4 19 8 20 25 29 5 14 34 16 13 33 11 11 11 7 19 20 25 17 10 29 5 38 49 26 34 28 53 6 9 31 30 38 1 28 53 1 22 65 57 41 49 34 13 60 62 33 27 1 49 71 13 1 25 79 71 39 67 36 79 43 9 58 78 33 17 13 50 43 39 4 85 46 27 4 99 1 73 1 49 18 50 ...

output:

0.690962766

result:

ok found '0.690962766', expected '0.690962766', error '0.000000001'

Test #16:

score: 5
Accepted
time: 421ms
memory: 62496kb

input:

300000
0 1 1 2 1 1 1 7 1 4 7 7 5 8 7 11 8 6 1 16 10 10 4 6 3 1 15 10 5 10 1 12 9 28 24 1 35 11 7 18 23 8 1 24 21 37 45 42 29 24 41 19 11 10 33 7 35 39 29 55 37 30 9 19 21 49 1 63 15 16 11 70 9 61 18 40 31 9 13 43 48 62 79 66 5 23 15 39 25 61 31 70 88 79 23 56 61 4 93 39 6 74 81 13 11 36 36 37 37 9 7...

output:

0.654738673

result:

ok found '0.654738673', expected '0.654738673', error '0.000000000'

Test #17:

score: 5
Accepted
time: 545ms
memory: 63376kb

input:

350000
0 1 2 1 1 1 3 6 3 7 1 5 3 9 3 15 2 3 1 17 13 2 3 9 23 22 12 10 17 4 21 1 25 15 17 6 25 1 1 4 33 11 31 8 17 29 7 38 33 19 11 10 15 32 46 41 15 21 47 20 46 48 7 13 42 14 37 35 33 28 51 46 1 4 33 26 46 8 1 74 37 20 5 26 32 37 64 31 70 54 85 28 29 68 49 84 13 88 39 92 51 87 75 9 55 26 50 95 83 70...

output:

0.676273091

result:

ok found '0.676273091', expected '0.676273091', error '0.000000000'

Test #18:

score: 5
Accepted
time: 530ms
memory: 66588kb

input:

400000
0 1 1 1 1 4 1 5 1 5 1 1 3 4 3 13 3 11 7 3 1 7 1 2 13 1 1 23 23 6 9 12 1 16 10 33 33 29 32 10 11 12 23 4 19 13 13 44 25 43 39 20 14 9 39 1 49 11 29 10 49 14 9 37 44 22 45 36 47 7 22 61 1 32 43 67 37 57 39 72 1 13 41 63 37 11 75 86 66 73 61 66 36 34 24 31 79 35 75 46 17 53 67 36 15 7 35 22 61 4...

output:

0.670448024

result:

ok found '0.670448024', expected '0.670448024', error '0.000000000'

Test #19:

score: 5
Accepted
time: 686ms
memory: 67456kb

input:

450000
0 1 2 2 1 1 5 7 1 4 3 11 7 5 7 10 4 10 7 19 4 18 15 20 4 12 6 7 28 13 21 16 3 25 17 13 19 16 27 25 10 38 31 26 23 6 24 18 23 47 42 43 34 1 5 38 12 11 51 53 26 53 2 5 45 54 19 26 30 31 19 46 62 44 45 12 25 73 37 50 17 46 23 58 57 71 77 17 28 81 60 40 73 55 70 49 61 76 21 86 71 28 62 82 18 85 2...

output:

0.671510079

result:

ok found '0.671510079', expected '0.671510079', error '0.000000000'

Test #20:

score: 5
Accepted
time: 802ms
memory: 68584kb

input:

500000
0 1 1 1 1 1 1 5 7 2 9 5 9 6 2 1 9 1 11 19 1 8 14 16 24 25 12 25 24 19 16 1 11 27 19 32 30 16 1 14 3 32 37 40 21 2 25 34 37 39 4 19 19 29 37 23 29 15 21 22 46 42 1 23 29 62 45 38 15 15 43 60 65 65 52 36 45 10 54 15 65 22 4 61 1 36 77 22 67 75 13 1 21 60 5 3 41 28 41 58 56 42 73 86 79 78 49 60 ...

output:

0.656659702

result:

ok found '0.656659702', expected '0.656659702', error '0.000000000'

Extra Test:

score: 0
Extra Test Passed