QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#114814 | #856. Cactus | EastKing | AC ✓ | 857ms | 43024kb | C++14 | 1.9kb | 2023-06-23 17:17:32 | 2023-06-23 17:17:35 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int M=4e5+5,P=1e9+7;
int n,m,K;
int head[M],asdf;
struct edge{
int to,nxt,id;
}G[M<<1];
void add_edge(int a,int b,int c){
G[++asdf].to=b;
G[asdf].nxt=head[a];
G[asdf].id=c;
head[a]=asdf;
}
int fast(int a,int b){
int res=1;
while(b>0){
if(b&1)res=1ll*res*a%P;
a=1ll*a*a%P;
b>>=1;
}
return res;
}
bool mark[M],vis[M];
int dfn[M],low[M],cyc[M],tot;
void tarjan(int x,int f){
dfn[x]=low[x]=++tot;
for(int i=head[x];i;i=G[i].nxt){
int y=G[i].to;
if(!dfn[y]){
tarjan(y,G[i].id);
low[x]=min(low[x],low[y]);
if(low[y]>dfn[x]){
mark[G[i].id]=1;
}
}else if(G[i].id!=f){
low[x]=min(low[x],dfn[y]);
}
}
}
int cnt;
void dfs(int x,int f){
if(vis[x])return;
vis[x]=1;
cnt++;
for(int i=head[x];i;i=G[i].nxt){
if(mark[G[i].id])continue;
int y=G[i].to;
dfs(y,x);
}
}
int main(){
int cas;
scanf("%d",&cas);
while(cas--){
scanf("%d %d %d",&n,&m,&K);
asdf=0;
tot=0;
int nik=fast(K,P-2);
int tmp=1;
for(int i=1;i<=n;i++){
head[i]=0;
dfn[i]=low[i]=0;
vis[i]=0;
if(i==1){
cyc[1]=K;
}else if(i==2){
cyc[2]=1ll*K*(K-1)%P;
}else {
cyc[i]=(1ll*tmp*K-cyc[i-1])%P;
}
tmp=1ll*tmp*(K-1)%P;
//if(i<=5)printf("cyc[%d]=%d\n",i,cyc[i]);
}
for(int i=1;i<=m;i++){
mark[i]=0;
int a,b;
scanf("%d %d",&a,&b);
add_edge(a,b,i);
add_edge(b,a,i);
}
tarjan(1,0);
int ans=1;
int tp=0;
for(int i=1;i<=n;i++){
if(!vis[i]){
cnt=0;
dfs(i,0);
if(cnt>1){
ans=1ll*ans*cyc[cnt]%P;
//printf("cyc[%d]=%d\n",cnt,cyc[cnt]);
if(!tp)ans=ans;
else ans=1ll*ans*(K-1)%P*nik%P;
tp++;
}else {
if(!tp)ans=1ll*ans*K%P;
else ans=1ll*ans*(K-1)%P;
tp++;
}
}
}
ans=(ans%P+P)%P;
printf("%d\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 12048kb
input:
2 2 1 100 1 2 6 7 3 1 2 2 3 3 1 4 5 5 6 6 4 1 4
output:
9900 24
result:
ok 2 number(s): "9900 24"
Test #2:
score: 0
Accepted
time: 75ms
memory: 12008kb
input:
50000 9 10 4 4 7 5 2 1 5 7 3 9 6 8 3 3 2 9 1 4 8 6 2 10 11 4 4 1 1 3 5 1 10 9 8 4 1 6 7 9 7 10 8 1 1 9 10 2 10 10 4 7 5 6 9 5 1 9 7 10 9 4 9 5 10 2 3 3 7 3 8 9 10 4 3 9 3 7 5 4 6 2 1 9 6 5 4 2 9 8 5 1 7 8 9 9 4 9 4 4 1 6 3 8 7 2 9 6 7 1 8 6 9 5 2 10 11 4 7 8 6 2 9 10 7 2 2 4 4 7 3 7 3 1 10 6 6 9 5 1...
output:
15120 34992 61236 15876 19764 34992 19692 34992 52488 19440 19764 34992 19692 13608 13608 52488 19692 13608 40824 34992 17496 40824 19656 52488 19764 13176 34992 59040 19692 34992 52488 13176 19656 34992 19680 599760 52488 34992 61236 19440 58320 11664 34992 40824 20412 34992 20412 34992 61236 34992...
result:
ok 50000 numbers
Test #3:
score: 0
Accepted
time: 857ms
memory: 43024kb
input:
10 300000 344504 711589813 136663 59111 262959 256239 220957 296457 132870 53422 167951 237433 252790 102337 18228 30482 162993 268323 127652 185288 133496 174755 122093 241171 165750 24398 4524 236165 261647 83998 127329 221453 263837 257156 263948 122651 142880 167089 203580 26970 4992 84305 11692...
output:
46959312 961402883 2 219976660 721840853 507095342 747233052 107251856 932546015 975072100
result:
ok 10 numbers