ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#214277 | #2383. 期望直径 | nullptr_qwq | 100 | 1242ms | 37220kb | C++11 | 7.4kb | 2024-11-16 22:30:53 | 2024-11-16 23:14:38 |
answer
// 私は猫です
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define inf 1000000000
#define infll 1000000000000000000ll
#define pii pair<int,int>
#define rep(i,a,b,c) for(int i=(a);i<=(b);i+=(c))
#define per(i,a,b,c) for(int i=(a);i>=(b);i-=(c))
#define F(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define dF(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define cmh(sjy) while(sjy--)
#define lowbit(x) (x&(-x))
#define HH printf("\n")
#define eb emplace_back
#define poly vector<int>
#define SZ(x) ((int)x.size())
using namespace std;
ll read(){
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
return x*f;
}
namespace Fastio{struct Reader{template<typename T>Reader&operator>>(T&x){x=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();x*=f;return*this;}Reader&operator>>(double&x){x=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(long double&x){x=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(__float128&x){x=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(char&c){c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();return*this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}Reader&operator>>(string&str){str.clear();char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';struct Writer{const int Setprecision=6;typedef int mxdouble;template<typename T>Writer&operator<<(T x){if(x==0){putchar('0');return*this;}if(x<0)putchar('-'),x=-x;static short sta[40];short top=0;while(x>0)sta[++top]=x%10,x/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(long double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(__float128 x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(char c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}}cout;}using namespace Fastio;
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
template<typename T>inline void chkmax(T &x,const T &y){ x=std::max(x,y); }
template<typename T>inline void chkmin(T &x,const T &y){ x=std::min(x,y); }
const int mod=1e9+7,maxn=200005;
inline int qpow(int x,ll y){
int rt=1;
for(;y;y>>=1,x=1ll*x*x%mod) if(y&1) rt=1ll*rt*x%mod;
return rt;
}
inline void inc(int &x,const int y){ x=(x+y>=mod)?(x+y-mod):(x+y); }
inline void dec(int &x,const int y){ x=(x>=y)?(x-y):(x+mod-y); }
inline void mul(int &x,const int y){ x=1ll*x*y%mod; }
inline int add(const int x,const int y){ return (x+y>=mod)?(x+y-mod):(x+y); }
inline int sub(const int x,const int y){ return (x>=y)?(x-y):(x+mod-y); }
inline int prod(const int x,const int y){ return 1ll*x*y%mod; }
const int B=2000;
int n,m,zsy,typ,fa[maxn],siz[maxn];
int find(int x){ return fa[x]==x?x:fa[x]=find(fa[x]); }
vector<int>g[maxn],bel[maxn];
unordered_map<ll,int>mp;
ll gethash(int x,int y){ return 100003ll*x+y; }
int up[maxn],mx[maxn],sc[maxn],fr[maxn],f[maxn],dist[maxn],inv[maxn];
vector<int>qwq[maxn],pre[maxn];
void dfs(int u,int fa=0){
vector<pii>tmp;
for(auto v:g[u])if(v^fa)dfs(v,u),tmp.push_back(mkp(mx[v]+1,v));
sort(tmp.begin(),tmp.end());
if(!tmp.empty()){
mx[u]=tmp.back().fi,fr[u]=tmp.back().se,tmp.pop_back();
if(!tmp.empty())sc[u]=tmp.back().fi;
}
}
void dfs1(int u,int fa=0){
f[u]=max(up[u],mx[u]);
for(auto v:g[u])if(v^fa)up[v]=max(up[u],fr[u]==v?sc[u]:mx[u])+1,dfs1(v,u);
}
void Solve(int x,int y){
if(x==y)return cout<<"-1"<<endl,void();
if(mp.count(gethash(x,y)))return cout<<mp[gethash(x,y)]<<endl,void();
if(siz[x]>siz[y])std::swap(x,y);
int tmp=max(dist[x],dist[y]),res=0;
F(i,0,siz[x]-1){
const int p=upper_bound(qwq[y].begin(),qwq[y].end(),tmp-qwq[x][i]-1)-qwq[y].begin()-1;
inc(res,1ll*tmp*(p+1)%mod);
inc(res,sub(pre[y].back(),p<0?0:pre[y][p]));
inc(res,1ll*(qwq[x][i]+1)*(siz[y]-1-p)%mod);
}
mul(res,inv[siz[x]]),mul(res,inv[siz[y]]),mp[gethash(x,y)]=mp[gethash(x,y)]=res;
cout<<res<<endl;
}
void solve(){
cin>>n>>m>>zsy>>typ;
F(i,1,n)siz[fa[i]=i]=1,inv[i]=qpow(i,mod-2);
F(i,1,m){
int u,v; cin>>u>>v,g[u].push_back(v),g[v].push_back(u),u=find(u),v=find(v);
if(u^v)siz[fa[u]=v]+=siz[u];
}
F(i,1,n)bel[find(i)].push_back(i);
vector<int>vec,Roots;
F(i,1,n)if(find(i)==i)Roots.push_back(i);
for(auto i:Roots)if(siz[i]>=B)vec.push_back(i);
for(auto R:Roots){
dfs(R),dfs1(R);
for(auto i:bel[R])qwq[R].push_back(f[i]);
sort(qwq[R].begin(),qwq[R].end());
int sum=0;
F(i,0,siz[R]-1)inc(sum,qwq[R][i]),pre[R].push_back(sum);
dist[R]=qwq[R].back();
}
for(auto x:vec)for(auto y:vec)if(x<y){
int res=0,tmp=max(dist[x],dist[y]),p=siz[y]-1;
F(i,0,siz[x]-1){
for(;p>=0&&qwq[x][i]+qwq[y][p]+1>=tmp;--p);
inc(res,1ll*tmp*(p+1)%mod);
inc(res,sub(pre[y].back(),p<0?0:pre[y][p]));
inc(res,1ll*(qwq[x][i]+1)*(siz[y]-1-p)%mod);
}
mul(res,inv[siz[x]]),mul(res,inv[siz[y]]),mp[gethash(x,y)]=mp[gethash(y,x)]=res;
}
F(i,1,zsy){
int u,v; cin>>u>>v;
Solve(find(u),find(v));
}
}
signed main(){
// ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int zsy=1;
F(____,1,zsy)solve();
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 5
Accepted
time: 3ms
memory: 19976kb
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: 4ms
memory: 19976kb
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: 4ms
memory: 19984kb
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: 3ms
memory: 19984kb
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: 4ms
memory: 19984kb
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: 4ms
memory: 19984kb
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: 8ms
memory: 20024kb
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: 0ms
memory: 20024kb
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: 0ms
memory: 20008kb
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: 3ms
memory: 20012kb
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: 0ms
memory: 20096kb
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: 3ms
memory: 20128kb
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: 9ms
memory: 20124kb
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: 278ms
memory: 29840kb
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: 259ms
memory: 29424kb
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: 5
Accepted
time: 95ms
memory: 30908kb
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: 208ms
memory: 32216kb
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: 213ms
memory: 32220kb
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: 5
Accepted
time: 77ms
memory: 37220kb
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:
ok 100000 lines
Test #20:
score: 5
Accepted
time: 67ms
memory: 30772kb
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
Extra Test:
score: 0
Extra Test Passed