ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#214286 | #2383. 期望直径 | one_zero_four_zero | 0 | 1306ms | 19872kb | C++11 | 3.0kb | 2024-11-16 22:58:15 | 2024-11-16 23:15:09 |
answer
#include<bits/stdc++.h>
#pragma GCC optimize(2)
#define mod 1000000007LL
using namespace std;
struct frac{
long long z, m;
friend frac operator + (const frac &a, const frac &b){
long long tmp = __gcd(a.m, b.m);
frac res;
res.m = a.m / tmp * b.m;
res.z = (b.m / tmp * a.z) + (a.m / tmp * b.z);
tmp = __gcd(res.m, res.z);
res.m /= tmp, res.z /= tmp;
return res;
}
friend frac operator * (const frac &a, const frac &b){
frac res;
res.m = a.m * b.m;
res.z = a.z * b.z;
long long tmp = __gcd(res.m, res.z);
res.m /= tmp, res.z /= tmp;
return res;
}
friend frac operator * (const frac &a, const int &b){
frac res;
res.m = a.m;
res.z = a.z * b;
long long tmp = __gcd(res.m, res.z);
res.m /= tmp, res.z /= tmp;
return res;
}
};
int N, M, Q, ty, cid = 0;
int u, v;
int pos1, pos2, maxn;
vector<int> E[100005];
int F[100005];
bool vis[2][100005];
int dis[2][100005];
int in[100005], siz[100005];
frac val[100005];
set<int> st;
int find(int x){
if (F[x] == x) return F[x];
F[x] = find(F[x]);
return F[x];
}
void merge(int x, int y){
int fx = find(x), fy = find(y);
if (fx == fy) return;
F[fx] = fy;
}
void dfs(int u, int typ, int cid){
// cout << u << " " << dis[typ][u] << " " << cid << ";;\n";
st.insert(u);
in[u] = cid;
if (typ == 0 && dis[0][u] > maxn){
maxn = dis[0][u];
pos2 = u;
}
for (auto && v : E[u]){
if (vis[typ][v]) continue;
vis[typ][v] = 1;
dis[typ][v] = min(dis[typ][v], dis[typ][u] + 1);
dfs(v, typ, cid);
}
}
long long qpow(long long a, long long b){
long long res = 1;
while (b){
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("../data.in", "r", stdin);
freopen("../data.out", "w", stdout);
#endif
scanf("%d %d %d %d", &N, &M, &Q, &ty);
for (int i = 1; i <= N; i ++) F[i] = i;
while (M --){
scanf("%d %d", &u, &v);
E[u].push_back(v);
E[v].push_back(u);
merge(u, v);
}
memset(dis, 0x3f, sizeof(dis));
for (int i = 1; i <= N; i ++){
val[i].m = 1;
}
for (int i = 1; i <= N; i ++){
if (vis[0][i]) continue;
pos1 = i, maxn = -1;
vis[0][pos1] = 1, dis[0][pos1] = 0;
dfs(pos1, 0, ++ cid);
pos1 = pos2, maxn = -1;
for (auto && i : st){
vis[0][i] = 0, dis[0][i] = 0x3f3f3f3f;
}
vis[0][pos1] = 1, dis[0][pos1] = 0;
dfs(pos1, 0, cid);
// cout << pos1 << "------" << pos2 << ";;\n";
siz[cid] = st.size();
vis[1][pos2] = 1, dis[1][pos2] = 0;
dfs(pos2, 1, cid);
for (auto && i : st){
val[cid] = val[cid] + (frac){1LL, siz[cid]} * max(dis[0][i], dis[1][i]);
// cout << i << "[]" << max(dis[0][i], dis[1][i]) << ";;\n";
}
st.clear();
}
while (Q --){
scanf("%d %d", &u, &v);
if (find(u) == find(v)){
printf("-1\n");
continue;
}
frac ans = val[in[u]] + val[in[v]] + (frac){1, 1};
// printf("%lld %lld\n", ans.z, ans.m);
printf("%lld\n", (qpow(ans.m, mod - 2) * ans.z) % mod);
}
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 4404kb
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:
666666679 2 666666674 6 666666679 6 666666675 6 7 1 6 666666674 2 7 -1 6 6 6 1 2 2 6 1 6 1 7 6666666...
result:
wrong answer 1st lines differ - expected: '900000014', found: '666666679'
Test #2:
score: 0
Wrong Answer
time: 0ms
memory: 4404kb
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 142857149 200000005 142857149 142857149 342857153 142857150 ...
result:
wrong answer 5th lines differ - expected: '428571437', found: '142857149'
Test #3:
score: 0
Wrong Answer
time: 0ms
memory: 4404kb
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 676470612 676470611 676470611 676470611 1 1 676470611 676470612 676470611 666666675 1 676470611 67...
result:
wrong answer 2nd lines differ - expected: '58823552', found: '676470612'
Test #4:
score: 0
Wrong Answer
time: 0ms
memory: 4400kb
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:
400000007 1 777777790 142857149 511111123 476190488 511111123 111111117 150000007 111111117 86666667...
result:
wrong answer 1st lines differ - expected: '800000010', found: '400000007'
Test #5:
score: 0
Wrong Answer
time: 0ms
memory: 4404kb
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 6 1 2 7 1 666666679 6 333333346 1 166666673 666666680 166666673 3 1 2 833333346 7 666666...
result:
wrong answer 3rd lines differ - expected: '800000012', found: '6'
Test #6:
score: 0
Wrong Answer
time: 0ms
memory: 4400kb
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:
142857149 333333343 563636376 666666675 333333343 142857149 1 200000006 30303038 2 1 2 142857150 333...
result:
wrong answer 1st lines differ - expected: '428571437', found: '142857149'
Test #7:
score: 0
Wrong Answer
time: 4ms
memory: 4412kb
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:
196078442 462745115 8 937500019 937500019 1 666666674 1 937500020 400000013 666666674 1 933333348 2 ...
result:
wrong answer 1st lines differ - expected: '686274524', found: '196078442'
Test #8:
score: 0
Wrong Answer
time: 4ms
memory: 4416kb
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 692307709 750000008 692307724 1 666666674 666666674 17 16 3 2 16 200000006 666...
result:
wrong answer 4th lines differ - expected: '307692324', found: '692307709'
Test #9:
score: 0
Wrong Answer
time: 0ms
memory: 4416kb
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:
947841331 432989843 432989843 432989843 432989843 947841331 432989843 514851641 947841331 514851641 ...
result:
wrong answer 1st lines differ - expected: '29703119', found: '947841331'
Test #10:
score: 0
Wrong Answer
time: 0ms
memory: 4412kb
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:
106796286 226666816 226666816 226666816 226666816 106796286 226666816 333462919 106796286 333462919 ...
result:
wrong answer 1st lines differ - expected: '383097418', found: '106796286'
Test #11:
score: 0
Wrong Answer
time: 4ms
memory: 4496kb
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:
92145423 298928978 298928978 298928978 298928978 92145423 298928978 206783873 92145423 206783873 921...
result:
wrong answer 1st lines differ - expected: '130080784', found: '92145423'
Test #12:
score: 0
Wrong Answer
time: 2ms
memory: 4456kb
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:
845883119 49586800 1 761904777 796296321 533333344 428571444 49586800 666666675 2 500000007 2 1 4285...
result:
wrong answer 1st lines differ - expected: '47291133', found: '845883119'
Test #13:
score: 0
Wrong Answer
time: 0ms
memory: 4448kb
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 363636373 454545464 500000008 2 461111131 142857149 200000005 350000015 1 2 ...
result:
wrong answer 5th lines differ - expected: '181818190', found: '363636373'
Test #14:
score: 0
Wrong Answer
time: 229ms
memory: 12176kb
input:
100000 99597 100000 0 44158 25720 25720 84658 84658 90057 90057 99607 99607 50202 50202 18449 18449 ...
output:
462213773 182643845 407304723 92353059 857814066 222213740 109889220 473918052 882292615 326620541 4...
result:
wrong answer 1st lines differ - expected: '254221579', found: '462213773'
Test #15:
score: 0
Wrong Answer
time: 235ms
memory: 11572kb
input:
100000 99597 100000 0 79104 47592 47592 20172 20172 51655 51655 42698 42698 2208 2208 50026 50026 31...
output:
133452832 16903791 133051487 246619715 593277610 135610371 296933502 822706726 949573639 892474146 5...
result:
wrong answer 1st lines differ - expected: '957552269', found: '133452832'
Test #16:
score: 0
Wrong Answer
time: 184ms
memory: 13688kb
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:
511293668 511293668 711293670 511293668 711293670 511293668 511293668 711293670 511293668 511293668 ...
result:
wrong answer 1st lines differ - expected: '79160750', found: '511293668'
Test #17:
score: 0
Wrong Answer
time: 144ms
memory: 10144kb
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:
wrong answer 24th lines differ - expected: '215700027', found: '930000032'
Test #18:
score: 0
Wrong Answer
time: 145ms
memory: 10140kb
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:
wrong answer 24th lines differ - expected: '215700027', found: '930000032'
Test #19:
score: 0
Wrong Answer
time: 174ms
memory: 19872kb
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:
161841177 661773681 161841177 661773681 661773681 661773681 161841177 661773681 161841177 161841177 ...
result:
wrong answer 1st lines differ - expected: '387890541', found: '161841177'
Test #20:
score: 0
Wrong Answer
time: 181ms
memory: 13680kb
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:
-452822207 631738752 631738752 631738752 631738752 -452822207 631738752 985201504 -452822207 9852015...
result:
wrong answer 1st lines differ - expected: '152181186', found: '-452822207'