UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#214271#2383. 期望直径Filberte901628ms34044kbC++113.6kb2024-11-16 22:01:032024-11-16 23:13:56

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
const int mod = 1e9 + 7;
vector<int> d[N], g[N];
int rr[N], iv[N], ss[N];
int calc(int u, int v){
    int Mn = max(rr[u], rr[v]);
    //cerr << "calc " << u << " " << v << " Mn = " << Mn << endl;
    int lu = d[u].size(), lv = d[v].size();
    ss[lu] = 0;for(int i = lu - 1;i >= 0;i--) ss[i] = (ss[i + 1] + d[u][i]) % mod;
    //cerr << u << " ss:";
    //for(int i = 0;i <= lu;i++) cerr << ss[i] << " ";
    //cerr << endl;
    int pu = 0, Sum = 0;
    for(int pv = lv - 1;pv >= 0;pv--){
        while(pu < lu && d[u][pu] + d[v][pv] + 1 < Mn)
            pu++;
        //cerr << pv << " - " << pu << ":" << d[u][pu] + d[v][pv] + 1 << endl;
        int cnt = lu - pu;
        Sum = (Sum + (1ll * pu * Mn % mod + 1ll * cnt * (d[v][pv] + 1) % mod + ss[pu]) % mod) % mod;
    }
    return 1ll * Sum * iv[lu] % mod * iv[lv] % mod;
}
int siz[N], scnt;
int col[N], Mx[N], ft, fat[N], fd[N];
bool ban[N];
void dfs1(int u, int fa){
    col[u] = scnt;
    for(int v : g[u]) if(v != fa){
        Mx[v] = Mx[u] + 1;
        if(Mx[v] > Mx[ft]) ft = v;
        dfs1(v, u);
    }
}
void dfs2(int u, int fa){
    for(int v : g[u]) if(v != fa){
        Mx[v] = Mx[u] + 1;
        fat[v] = u;
        if(Mx[v] > Mx[ft]) ft = v;
        dfs2(v, u);
    }
}
void dfs3(int u, int fa){
    for(int v : g[u]) if(v != fa && !ban[v]){
        //cerr << v << " ";
        fd[v] = fd[u] + 1;
        d[scnt].push_back(fd[v]);
        dfs3(v, u);
    }
}
int ans[N];
struct Qrys{
    int v, id;
};
vector<Qrys> qs[N];
unordered_map<int, int> rs[N];
int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n, m, q, tp;
    cin >> n >> m >> q >> tp;
    iv[1] = 1;for(int i = 2;i <= n;i++) iv[i] = 1ll * (mod - mod / i) * iv[mod % i] % mod;
    for(int i = 1;i <= m;i++){
        int x, y;cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for(int i = 1, u, v;i <= n;i++) if(!col[i]){
        ++scnt;Mx[ft = i] = 0;
        dfs1(i, i);
        Mx[v = ft] = 0;
        fat[v] = v;
        dfs2(v, v);
        u = ft;
        int x = u, len = 0, cur = 0;
        while(1){
            len++, ban[x] = 1;
            if(x == fat[x]) break;
            x = fat[x];
        }
        rr[scnt] = len - 1;
        x = u;
        //cerr << "scc " << scnt << ":r = " << rr[scnt] << endl;
        //cerr << "Members:";
        while(1){
            fd[x] = max(cur, len - cur - 1);
            d[scnt].push_back(fd[x]);
            dfs3(x, x);
            //cerr << x << " ";
            if(x == fat[x]) break;
            x = fat[x];
            cur++;
        }
        //cerr << endl;
        sort(d[scnt].begin(), d[scnt].end());
        //cerr << "Distant:";
       // for(int dd : d[scnt]) cerr << dd << " ";
        //cerr << endl;
    }
    //cerr << endl;
    for(int _t = 1;_t <= q;_t++){
        int u, v;cin >> u >> v;
        if(col[u] == col[v]){
            ans[_t] = -1;
            continue;
        }
        u = col[u], v = col[v];
        //cerr << "Qrys" << _t << ":"; 
        if(d[u].size() < d[v].size()) swap(u, v);
        //cerr << u << " " << v << endl;
        qs[u].push_back({v, _t});
    }
    for(int u = 1;u <= scnt;u++){
        for(Qrys qq : qs[u]){
            int v = qq.v, id = qq.id;
            //cerr << id << ":" << u << " " << v << endl;
            if(rs[u].count(v)) ans[id] = rs[u][v];
            else{
                ans[id] = calc(u, v);
                rs[u][v] = ans[id];
            }
        }
    }
    for(int i = 1;i <= q;i++) cout << ans[i] << endl;
    return 0;
}

详细

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

Test #1:

score: 5
Accepted
time: 8ms
memory: 22436kb

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:

900000014
2
666666674
800000012
900000014
800000012
666666675
800000012
700000012
1
800000012
666666...

result:

ok 30 lines

Test #2:

score: 5
Accepted
time: 7ms
memory: 22440kb

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:

666666674
142857150
200000006
342857153
428571437
200000005
428571437
428571437
342857153
142857150
...

result:

ok 30 lines

Test #3:

score: 5
Accepted
time: 7ms
memory: 22444kb

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
58823552
352941201
352941201
352941201
1
1
352941201
58823552
352941201
666666675
1
352941201
3529...

result:

ok 80 lines

Test #4:

score: 5
Accepted
time: 10ms
memory: 22444kb

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:

800000010
1
777777790
428571437
511111123
476190488
511111123
333333341
150000007
333333341
86666667...

result:

ok 80 lines

Test #5:

score: 5
Accepted
time: 15ms
memory: 22440kb

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:

666666674
2
800000012
1
2
166666676
1
900000014
800000012
347222235
1
166666673
888888904
166666673
...

result:

ok 80 lines

Test #6:

score: 5
Accepted
time: 8ms
memory: 22440kb

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:

428571437
166666676
563636376
666666675
166666676
428571437
1
200000006
969696984
2
1
2
142857150
16...

result:

ok 80 lines

Test #7:

score: 5
Accepted
time: 16ms
memory: 22468kb

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:

686274524
462745115
909090924
562500019
562500019
1
666666674
1
250000017
163636375
666666674
1
2666...

result:

ok 300 lines

Test #8:

score: 5
Accepted
time: 13ms
memory: 22468kb

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
500000007
750000008
307692324
750000008
514501922
1
666666674
666666674
508196744
377049202
3
2
37...

result:

ok 300 lines

Test #9:

score: 5
Accepted
time: 18ms
memory: 22468kb

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:

29703119
183343591
183343591
183343591
183343591
29703119
183343591
514851641
29703119
514851641
297...

result:

ok 300 lines

Test #10:

score: 5
Accepted
time: 11ms
memory: 22472kb

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:

383097418
66011078
66011078
66011078
66011078
383097418
66011078
606343180
383097418
606343180
38309...

result:

ok 300 lines

Test #11:

score: 5
Accepted
time: 3ms
memory: 22540kb

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:

130080784
67893788
67893788
67893788
67893788
130080784
67893788
185996139
130080784
185996139
13008...

result:

ok 1000 lines

Test #12:

score: 5
Accepted
time: 13ms
memory: 22544kb

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:

47291133
314049617
1
142857155
166666691
533333344
523809541
314049617
666666675
2
500000007
2
1
142...

result:

ok 1000 lines

Test #13:

score: 5
Accepted
time: 10ms
memory: 22548kb

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:

500000007
2
1
666666674
181818190
454545464
500000008
2
272222241
142857149
200000005
850000021
1
2
...

result:

ok 1000 lines

Test #14:

score: 5
Accepted
time: 358ms
memory: 30756kb

input:

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

output:

254221579
792923657
404547859
419764826
912647414
983259782
986956575
146035136
653331679
984080994
...

result:

ok 100000 lines

Test #15:

score: 5
Accepted
time: 353ms
memory: 30440kb

input:

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

output:

957552269
166765756
14203116
317128974
317140938
352884183
88651132
119227492
422815509
884628637
58...

result:

ok 100000 lines

Test #16:

score: 0
Time Limit Exceeded

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:


result:


Test #17:

score: 5
Accepted
time: 289ms
memory: 34044kb

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
260000023
310000026
560000027
1500000...

result:

ok 100000 lines

Test #18:

score: 5
Accepted
time: 295ms
memory: 34044kb

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
260000023
310000026
560000027
1500000...

result:

ok 100000 lines

Test #19:

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:

387890541
705880590
387890541
705880590
705880590
705880590
387890541
705880590
387890541
387890541
...

result:


Test #20:

score: 5
Accepted
time: 194ms
memory: 30920kb

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:

152181186
789372554
789372554
789372554
789372554
152181186
789372554
797495682
152181186
797495682
...

result:

ok 100000 lines