ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#214282 | #2383. 期望直径 | 18112606231 | 80 | 1764ms | 40940kb | C++11 | 4.4kb | 2024-11-16 22:46:54 | 2024-11-16 23:14:49 |
answer
#include <bits/stdc++.h>
#define MOD 1000000007
#define int long long
using namespace std;
int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * f;
}
int n, m, q, u, v, st, f[200001], siz[200001], my, res, ress, xx, yy, jc[200001], inv[200001], maxn[200001], deep[200001], ans[200001];
vector<int> e[200001], fa[200001], cnt[200001], sum[200001];
map<pair<int, int>, int> vis;
int find(int x)
{
if (x == f[x])
return x;
return f[x] = find(f[x]);
}
void merge(int x, int y)
{
int fx = find(x), fy = find(y);
if (fx != fy)
{
f[fx] = fy;
siz[fy] += siz[fx];
}
}
void dfs(int u, int dep, int f = -1)
{
maxn[u] = max(maxn[u], dep);
deep[u] = dep;
for (auto v : e[u])
{
if (v != f)
{
dfs(v, dep + 1, u);
}
}
}
void exgcd(int a, int b)
{
if (b == 0)
{
xx = 1;
yy = 0;
return;
}
exgcd(b, a % b);
int t = xx;
xx = yy;
yy = t - a / b * yy;
}
int qpow(int a, int b)
{
int res = 1;
while (b)
{
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
void init()
{
inv[0] = 1;
jc[0] = 1;
for (int i = 1; i <= 200000; i++)
{
jc[i] = jc[i - 1] * i % MOD;
siz[i] = 1;
f[i] = i;
inv[i] = qpow(jc[i], MOD - 2);
}
}
int work(int x, int y)
{
int nowmax = max(ans[x], ans[y]), res1 = siz[x] * siz[y], res2 = 0;
for (auto i : fa[x])
{
if (nowmax - maxn[i] <= siz[y])
{
if (nowmax - maxn[i])
{
int a = cnt[y][siz[y]] - cnt[y][nowmax - maxn[i] - 1];
int b = sum[y][siz[y]] - sum[y][nowmax - maxn[i] - 1];
res1 -= a;
res2 += b + a * maxn[i];
}
else
{
res1 -= cnt[y][siz[y]];
res2 += sum[y][siz[y]];
res2 += maxn[i] * cnt[y][siz[y]];
}
}
}
res = res1 * nowmax + res2;
ress = siz[x] * siz[y];
exgcd(ress, MOD);
xx = (xx % MOD + MOD) % MOD;
// cout << res << ' ' << siz[x] << ' ' << siz[y] << ' ' << inv[siz[x]] << ' ' << inv[siz[y]] << endl;
return res * xx % MOD;
}
signed main()
{
n = read();
m = read();
q = read();
my = read();
init();
for (int i = 1; i <= m; i++)
{
u = read();
v = read();
e[u].push_back(v);
e[v].push_back(u);
merge(u, v);
}
for (int i = 1; i <= n; i++)
{
fa[find(i)].push_back(i);
}
for (int i = 1; i <= n; i++)
{
if (f[i] == i)
{
st = 0;
dfs(i, 0);
for (auto j : fa[i])
{
if (deep[j] > deep[st])
{
st = j;
}
}
dfs(st, 0);
for (auto j : fa[i])
{
if (deep[j] > deep[st])
{
st = j;
}
}
dfs(st, 0);
sum[i].resize(siz[i] + 3);
cnt[i].resize(siz[i] + 3);
for (auto j : fa[i])
{
cnt[i][maxn[j]]++;
sum[i][maxn[j]] = sum[i][maxn[j]] + maxn[j] + 1;
ans[i] = max(ans[i], maxn[j]);
}
for (int j = 1; j <= siz[i]; j++)
{
cnt[i][j] += cnt[i][j - 1];
sum[i][j] += sum[i][j - 1];
}
}
}
while (q--)
{
u = read();
v = read();
u = find(u);
v = find(v);
if (u == v)
{
printf("-1\n");
continue;
}
if (siz[u] > siz[v])
{
swap(u, v);
}
if (vis[{u, v}])
{
printf("%lld\n", vis[{u, v}]);
}
else
{
vis[{u, v}] = work(u, v);
printf("%lld\n", vis[{u, v}]);
}
}
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 5
Accepted
time: 37ms
memory: 26224kb
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: 41ms
memory: 26224kb
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: 34ms
memory: 26232kb
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: 42ms
memory: 26236kb
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: 32ms
memory: 26236kb
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: 37ms
memory: 26236kb
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: 46ms
memory: 26276kb
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: 34ms
memory: 26272kb
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: 43ms
memory: 26240kb
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: 36ms
memory: 26244kb
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: 34ms
memory: 26316kb
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: 41ms
memory: 26396kb
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: 39ms
memory: 26400kb
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: 0
Wrong Answer
time: 256ms
memory: 36260kb
input:
100000 99597 100000 0 44158 25720 25720 84658 84658 90057 90057 99607 99607 50202 50202 18449 18449 ...
output:
671877578 792923657 234683540 419764826 -585073597 400915774 986956575 -86845925 653331679 -18060702...
result:
wrong answer 1st lines differ - expected: '254221579', found: '671877578'
Test #15:
score: 0
Wrong Answer
time: 248ms
memory: 35900kb
input:
100000 99597 100000 0 79104 47592 47592 20172 20172 51655 51655 42698 42698 2208 2208 50026 50026 31...
output:
957552269 -415578252 -568140892 -265215034 317140938 330659610 -493692876 -463116516 422815509 -6977...
result:
wrong answer 2nd lines differ - expected: '166765756', found: '-415578252'
Test #16:
score: 5
Accepted
time: 215ms
memory: 36592kb
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:
79160750 79160750 633846035 79160750 633846035 79160750 79160750 633846035 79160750 79160750 7916075...
result:
ok 100000 lines
Test #17:
score: 5
Accepted
time: 175ms
memory: 40940kb
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: 191ms
memory: 40936kb
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
Wrong Answer
time: 94ms
memory: 37392kb
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:
-273617450 705880590 -273617450 705880590 705880590 705880590 -273617450 705880590 -273617450 -27361...
result:
wrong answer 1st lines differ - expected: '387890541', found: '-273617450'
Test #20:
score: 0
Wrong Answer
time: 89ms
memory: 34924kb
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:
-934915069 695580663 695580663 695580663 695580663 -934915069 695580663 -653073117 -934915069 -65307...
result:
wrong answer 1st lines differ - expected: '152181186', found: '-934915069'