QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#604274 | #7040. Connected Subgraphs | tarjen | AC ✓ | 6777ms | 23304kb | C++20 | 2.8kb | 2024-10-02 04:40:54 | 2024-10-02 04:40:56 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
void add(int &x,int y){
if((x+=y)>=mod)x-=mod;
}
void del(int &x,int y){
if((x-=y)<0)x+=mod;
}
const int Ny24=41666667;
ll C4(int n){
if(n<=3)return 0;
return (ll)n*(n-1)%mod*(n-2)%mod*(n-3)%mod*Ny24%mod;
}
const bool test=false;
ll solve(){
int n,m;cin>>n>>m;
vector<pair<int,int>> edges(m);
vector<int>d(n+1),flg(n+1),id(n+1),rk(n+1),c(n+1);
vector<vector<int>> ve(n+1),f(n+1),g(n+1);
int ans=0,ans3=0,ans4=0,circle3=0;
// ans 链 3 三元环 4四元环
for(int i=0;i<m;i++){
int u,v;cin>>u>>v;
edges[i]=make_pair(u,v);
ve[u].push_back(v);
ve[v].push_back(u);
}
for(int i=1;i<=n;i++)
d[id[i]=i]=(int)ve[i].size();
{//菊花
for(int i=1;i<=n;i++)add(ans,C4(d[i]));
if(test)cout<<"flower = "<<ans<<endl;
}
for(auto [u,v]:edges){
if(d[u]>d[v]||(d[u]==d[v]&&u>v))g[u].push_back(v);
else g[v].push_back(u);
}
sort(id.begin()+1,id.end(),[&](int x,int y){
return d[x]<d[y];
});
for(int i=1;i<=n;i++)rk[id[i]]=i;
for(int u=1;u<=n;u++)for(auto v:ve[u])
if(rk[v]>rk[u])f[u].push_back(v);
for(int u=1,C=0;u<=n;u++){
for(auto v:ve[u])for(auto w:f[v])if(rk[w]>rk[u])add(ans4,c[w]),++c[w];
for(auto v:ve[u])for(auto w:f[v])if(rk[w]>rk[u])c[w]=0;
++C;
for(auto v:g[u])flg[v]=C;
for(auto v:g[u])
for(auto w:g[v])if(flg[w]==C){
add(ans3,d[u]-2+d[v]-2+d[w]-2);
add(circle3,1);
}
}
if(test)cout<<"ans3 = "<<ans3<<endl;
if(test)cout<<"ans4 = "<<ans4<<endl;
{//分叉
int res=0;
for(auto [x,y]:edges){
if(d[x]>=2&&d[y]>=3)add(res,(ll)(d[x]-1)*(((ll)(d[y]-1)*(d[y]-2)/2)%mod)%mod);
if(d[y]>=2&&d[x]>=3)add(res,(ll)(d[y]-1)*(((ll)(d[x]-1)*(d[x]-2)/2)%mod)%mod);
}
del(res,2ll*ans3%mod);
add(ans,res);
if(test)cout<<"one-two = "<<res<<endl;
}
{//链
int res=0;
for(int i=1;i<=n;i++)if(d[i]>=2){
int all=0;
for(auto it:ve[i])if(d[it]>=2){
del(res,((ll)(d[it]-1)*(d[it]-2)/2)%mod);
add(all,d[it]-1);
}
add(res,((ll)all*(all-1)/2)%mod);
}
del(res,2ll*ans3%mod);
del(res,4ll*ans4%mod);
del(res,3ll*circle3%mod);
add(ans,res);
if(test)cout<<"chain = "<<res<<endl;
}
add(ans,ans3);
add(ans,ans4);
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;while(T--)cout<<solve()<<"\n";
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3852kb
input:
2 4 4 1 2 2 3 3 4 4 1 4 6 1 2 1 3 1 4 2 3 2 4 3 4
output:
1 15
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 1072ms
memory: 23116kb
input:
10 3296 174079 484 736 80 3275 914 1609 611 2069 1111 3027 749 1443 568 1878 1180 2788 1072 2156 171 797 106 1226 1749 2331 758 3080 772 2017 1381 2393 1945 2943 269 1431 1640 1805 1924 2161 1720 2123 1551 2940 286 1226 460 1462 1807 1823 40 638 18 707 589 849 1857 1987 914 2360 2446 3129 24 910 113...
output:
192810800 284251171 771081766 673113026 227774506 23760227 44050220 47307815 414063001 939220437
result:
ok 10 lines
Test #3:
score: 0
Accepted
time: 6777ms
memory: 10392kb
input:
10 693 200000 442 134 485 423 447 184 544 230 46 401 224 156 684 285 227 141 321 647 126 62 644 253 246 509 278 318 104 340 171 116 187 434 554 574 344 285 619 640 240 634 132 193 415 441 130 427 132 134 206 53 102 460 560 312 530 291 479 113 231 455 501 238 666 288 10 422 330 415 240 2 163 302 96 5...
output:
262595832 325062112 160521108 950636215 426755446 561024458 49082359 743032532 582488571 687278985
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 754ms
memory: 23304kb
input:
10 100000 200000 1 50000 2 50000 3 50000 4 50000 5 50000 6 50000 7 50000 8 50000 9 50000 10 50000 11 50000 12 50000 13 50000 14 50000 15 50000 16 50000 17 50000 18 50000 19 50000 20 50000 21 50000 22 50000 23 50000 24 50000 25 50000 26 50000 27 50000 28 50000 29 50000 30 50000 31 50000 32 50000 33 5...
output:
427268331 405969056 287464978 480992480 583899102 346872966 503167381 491791864 482789441 414972531
result:
ok 10 lines
Test #5:
score: 0
Accepted
time: 765ms
memory: 23164kb
input:
10 100000 200000 50000 50001 1 50001 2 50001 3 50001 4 50001 5 50001 6 50001 7 50001 8 50001 9 50001 10 50001 11 50001 12 50001 13 50001 14 50001 15 50001 16 50001 17 50001 18 50001 19 50001 20 50001 21 50001 22 50001 23 50001 24 50001 25 50001 26 50001 27 50001 28 50001 29 50001 30 50001 31 50001 3...
output:
318946949 387994406 758360868 579574960 687516111 966394852 864355459 29275977 871937345 134220714
result:
ok 10 lines