UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#214288#2383. 期望直径nodgd0908ms13536kbC++113.3kb2024-11-16 22:59:582024-11-16 23:15:19

answer

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 100000 + 5;
const int P = 1000000007;

int N, M, Q, Ty;
int elast[MAX_N], ey[MAX_N << 1], enext[MAX_N << 1];
int gr[MAX_N], gsz[MAX_N], gmd[MAX_N];
vector<int> gc[MAX_N];
int dis[MAX_N], dis2[MAX_N];
map<pair<int,int>,int> ans;

void dfs(int u, int fa) {
    for (int j = elast[u], v; j; j = enext[j]) {
        if ((v = ey[j]) != fa) {
            dis2[v] = dis2[u] + 1;
            dfs(v, u);
        }
    }
}

void init(int st) {
    static int q[MAX_N], qh, qt;
    gr[st] = st;
    qh = 0, qt = 1, q[0] = st;
    while (qh != qt) {
        int u = q[qh ++];
        for (int j = elast[u], v; j; j = enext[j]) {
            if (!gr[v = ey[j]]) {
                dis[v] = dis[u] + 1;
                gr[v] = st;
                q[qt ++] = v;
            }
        }
    }
    int A = q[qt - 1];
    for (int i = 0; i < qt; i ++) {
        gr[q[i]] = 0;
        dis[q[i]] = 0;
    }
    gr[A] = st;
    qh = 0, qt = 1, q[0] = A;
    while (qh != qt) {
        int u = q[qh ++];
        for (int j = elast[u], v; j; j = enext[j]) {
            if (!gr[v = ey[j]]) {
                dis[v] = dis[u] + 1;
                gr[v] = st;
                q[qt ++] = v;
            }
        }
    }
    int B = q[qt - 1];
    gsz[st] = qt;
    gmd[st] = dis[B];
    gc[st].resize(dis[B] + 1, 0);
    dfs(B, 0);
    for (int i = 0; i < qt; i ++) {
        int u = q[i];
        int d = max(dis[u], dis2[u]);
        gc[st][d] ++;
    }
    // printf("st=%d, c=", st);
    // for (int d = 0; d <= gmd[st]; d ++) {
    //     printf("%d,", gc[st][d]);
    // }
    // printf("\n");
}

int power_mod(int a, int b) {
    int c = 1;
    for (; b; b >>= 1) {
        if (b & 1) c = 1ll * c * a % P;
        a = 1ll * a * a % P;
    }
    return c;
}

int main() {
    scanf("%d%d%d%d", &N, &M, &Q, &Ty);
    for (int i = 1, j = 1; i <= M; i ++) {
        int x, y;
        scanf("%d%d", &x, &y);
        ey[j] = y, enext[j] = elast[x], elast[x] = j ++;
        ey[j] = x, enext[j] = elast[y], elast[y] = j ++;
    }
    for (int i = 1; i <= N; i ++) {
        if (!gr[i]) init(i);
    }
    for (int i = 1; i <= Q; i ++) {
        int x, y;
        scanf("%d%d", &x, &y);
        if (gr[x] == gr[y]) {
            printf("-1\n");
        } else {
            x = gr[x], y = gr[y];
            if (gmd[x] < gmd[y]) swap(x, y);
            if (ans.count(make_pair(x, y))) {
                printf("%d\n", ans[make_pair(x, y)]);
            } else {
                int Dx = gmd[x], Dy = gmd[y];
                auto &cx = gc[x], &cy = gc[y];
                int res = 0;
                for (int d = Dx + 1; d <= Dx + Dy; d ++) {
                    long long t = 0;
                    for (int d2 = 0; d2 <= d && d2 <= Dy; d2 ++) {
                        if (d - d2 <= Dx) {
                            t += 1ll * cx[d - d2] * cy[d2];
                        }
                    }
                    res = (res + t % P * (d - Dx)) % P;
                }
                int res_ = power_mod(1ll * gsz[x] * gsz[y] % P, P - 2);
                res = 1ll * res * res_ % P;
                res += Dx + 1;
                printf("%d\n", res);
                ans[make_pair(x, y)] = res;
            }
        }
    }
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3616kb

input:

30 15 30 0
2 5
6 25
22 21
7 9
7 22
22 24
27 26
12 22
28 27
27 9
8 1
29 6
20 18
28 16
20 23
23 9
1 10...

output:

66666675
2
3
7
66666675
7
666666675
7
800000013
1
7
3
2
800000013
-1
7
7
7
1
2
2
7
1
7
1
800000013
3...

result:

wrong answer 1st lines differ - expected: '900000014', found: '66666675'

Test #2:

score: 0
Wrong Answer
time: 0ms
memory: 3616kb

input:

30 15 30 0
18 29
3 22
14 2
25 5
19 24
27 9
2 5
30 10
22 10
7 29
28 11
20 16
3 19
5 1
10 15
18 8
27 1...

output:

3
428571438
200000006
342857153
6
4
6
6
342857153
428571438
3
904761918
200000006
4
-1
2
666666675
6...

result:

wrong answer 1st lines differ - expected: '666666674', found: '3'

Test #3:

score: 0
Wrong Answer
time: 0ms
memory: 3616kb

input:

80 40 80 0
50 4
11 44
10 39
62 34
69 67
66 17
67 19
66 45
11 22
62 14
54 31
9 18
49 52
44 57
63 80
1...

output:

1
352941202
23
23
23
1
1
23
352941202
23
666666675
1
23
23
2
352941202
23
1
1
23
23
1
23
1
3
1
2
23
...

result:

wrong answer 2nd lines differ - expected: '58823552', found: '352941202'

Test #4:

score: 0
Wrong Answer
time: 0ms
memory: 3620kb

input:

80 40 80 0
77 43
64 28
18 16
25 47
78 8
48 78
6 31
51 76
45 4
79 50
29 41
9 45
69 21
41 46
70 34
74 ...

output:

5
1
518518529
6
511111123
476190488
511111123
6
6
6
866666678
1
6
1
5
4
416666674
2
3
3
1
6
5
511111...

result:

wrong answer 1st lines differ - expected: '800000010', found: '5'

Test #5:

score: 0
Wrong Answer
time: 0ms
memory: 3616kb

input:

80 40 80 0
56 60
73 50
72 62
53 17
1 60
27 5
75 40
30 10
18 53
19 79
53 28
19 38
40 42
61 6
2 16
1 5...

output:

3
2
7
1
2
9
1
66666675
7
444444458
1
166666673
388888901
166666673
3
1
2
833333346
800000013
3888889...

result:

wrong answer 1st lines differ - expected: '666666674', found: '3'

Test #6:

score: 0
Wrong Answer
time: 1ms
memory: 3620kb

input:

80 40 80 0
53 55
50 22
3 75
77 2
70 48
23 59
54 41
67 60
33 76
40 7
9 5
66 32
49 80
41 70
40 19
57 3...

output:

6
9
90909100
666666675
9
6
1
200000006
606060618
2
1
2
428571438
9
1
7
9
342857153
454545465
1666666...

result:

wrong answer 1st lines differ - expected: '428571437', found: '6'

Test #7:

score: 0
Wrong Answer
time: 0ms
memory: 3640kb

input:

300 150 300 0
283 279
218 235
125 184
262 180
143 229
175 179
138 275
285 182
253 26
143 141
28 30
9...

output:

450980405
462745115
636363650
16
16
1
3
1
562500020
509090923
3
1
933333349
2
666666675
320312519
1
...

result:

wrong answer 1st lines differ - expected: '686274524', found: '450980405'

Test #8:

score: 0
Wrong Answer
time: 0ms
memory: 3636kb

input:

300 150 300 0
259 289
214 108
213 200
11 217
204 152
162 40
25 54
25 115
286 158
53 123
213 30
268 2...

output:

2
4
3
15
3
447667117
1
3
3
377049203
20
3
2
20
200000006
3
377049203
377049203
666666675
2
15
333333...

result:

wrong answer 2nd lines differ - expected: '500000007', found: '4'

Test #9:

score: 0
Wrong Answer
time: 0ms
memory: 3616kb

input:

300 297 300 2
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

439012094
184758590
184758590
184758590
184758590
439012094
184758590
514851641
439012094
514851641
...

result:

wrong answer 1st lines differ - expected: '29703119', found: '439012094'

Test #10:

score: 0
Wrong Answer
time: 0ms
memory: 3620kb

input:

300 297 300 2
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

963393459
55737854
55737854
55737854
55737854
963393459
55737854
927378781
963393459
927378781
96339...

result:

wrong answer 1st lines differ - expected: '383097418', found: '963393459'

Test #11:

score: 0
Wrong Answer
time: 0ms
memory: 3664kb

input:

1000 997 1000 2
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18...

output:

714729936
374860941
374860941
374860941
374860941
714729936
374860941
41576007
714729936
41576007
71...

result:

wrong answer 1st lines differ - expected: '130080784', found: '714729936'

Test #12:

score: 0
Wrong Answer
time: 0ms
memory: 3712kb

input:

1000 500 1000 0
951 746
924 806
141 768
702 327
917 566
465 127
89 896
587 605
10 703
444 931
867 40...

output:

255739252
29
1
12
55555580
533333344
142857158
29
666666675
2
4
2
1
14
979338879
17
750000009
3
1
31...

result:

wrong answer 1st lines differ - expected: '47291133', found: '255739252'

Test #13:

score: 0
Wrong Answer
time: 0ms
memory: 3708kb

input:

1000 500 1000 0
284 587
979 855
443 39
216 126
410 581
591 137
100 376
244 622
277 957
128 341
536 3...

output:

4
2
1
3
454545465
90909098
500000008
2
755555578
285714293
4
16
1
2
6
4
16
4
16
533333348
2
1
327272...

result:

wrong answer 1st lines differ - expected: '500000007', found: '4'

Test #14:

score: 0
Wrong Answer
time: 169ms
memory: 8716kb

input:

100000 99597 100000 0
44158 25720
25720 84658
84658 90057
90057 99607
99607 50202
50202 18449
18449 ...

output:

338967807
902658605
636616919
587411886
346925166
698252781
954220002
428115191
553443444
297454719
...

result:

wrong answer 1st lines differ - expected: '254221579', found: '338967807'

Test #15:

score: 0
Wrong Answer
time: 255ms
memory: 8456kb

input:

100000 99597 100000 0
79104 47592
47592 20172
20172 51655
51655 42698
42698 2208
2208 50026
50026 31...

output:

933326442
27302956
362805993
839770283
49927853
42717261
368728970
40481414
735508627
59179795
97929...

result:

wrong answer 1st lines differ - expected: '957552269', found: '933326442'

Test #16:

score: 0
Wrong Answer
time: 78ms
memory: 8840kb

input:

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

output:

852787174
852787174
45214714
852787174
45214714
852787174
852787174
45214714
852787174
852787174
852...

result:

wrong answer 1st lines differ - expected: '79160750', found: '852787174'

Test #17:

score: 0
Wrong Answer
time: 131ms
memory: 13536kb

input:

100000 99000 100000 1
1 2
1 3
3 4
1 5
2 6
3 7
3 8
5 9
7 10
2 11
6 12
1 13
13 14
10 15
12 16
12 17
17...

output:

-1
300000025
890000029
640000027
420000025
780000029
540000027
545700025
310000026
560000027
1500000...

result:

wrong answer 8th lines differ - expected: '260000023', found: '545700025'

Test #18:

score: 0
Wrong Answer
time: 129ms
memory: 13536kb

input:

100000 99000 100000 1
1 2
1 3
3 4
1 5
2 6
3 7
3 8
5 9
7 10
2 11
6 12
1 13
13 14
10 15
12 16
12 17
17...

output:

-1
300000025
890000029
640000027
420000025
780000029
540000027
545700025
310000026
560000027
1500000...

result:

wrong answer 8th lines differ - expected: '260000023', found: '545700025'

Test #19:

score: 0
Wrong Answer
time: 145ms
memory: 11624kb

input:

100000 99997 100000 2
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17...

output:

685275838
9999
685275838
9999
9999
9999
685275838
9999
685275838
685275838
685275838
9999
685275838
...

result:

wrong answer 1st lines differ - expected: '387890541', found: '685275838'

Test #20:

score: 0
Time Limit Exceeded

input:

100000 99997 100000 2
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17...

output:


result: