QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#453811 | #856. Cactus | Nana7 | WA | 62ms | 30152kb | C++14 | 2.1kb | 2024-06-24 12:09:21 | 2024-06-24 12:09:21 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#define I inline
#define int long long
using namespace std;
const int mod = 1e9+7;
const int N = 400010;
int n,m,k,cnt;
int dep[N],pre[N],pw[N],c[N];
vector<int> q[N],v[N];
I void get(int x,int y) {
if(dep[x]<dep[y]) return ;
int mk=x; cnt++;
while(1) {
//cout<<mk<<' '<<pre[m]<<' '<<x<<' '<<y<<endl;
v[cnt].push_back(mk);
if(mk==y) return ;
mk=pre[mk];
}
}
void dfs1(int x,int fa) { //cout<<x<<' '<<fa<<endl;
pre[x]=fa;
dep[x]=dep[fa]+1;
for(auto &t:q[x]) {
if(t==fa) continue;
if(dep[t]) get(x,t);
else dfs1(t,x);
}
}
int qpow(int x,int y) {
if(y==0) return 1;
if(y==1) return x;
int mk=qpow(x,y/2);
return y%2?mk*mk%mod*x%mod:mk*mk%mod;
}
I void ycl() {
pw[0]=1; cnt=0;
for(int i=1;i<=n;++i) pw[i]=pw[i-1]*(k-1)%mod;
for(int i=1;i<=n;++i) dep[i]=pre[i]=0,q[i].clear(),v[i].clear();
}
I void calc() {
int inv=qpow(k,mod-2);
c[3]=k*(k-1)%mod*(k-2)%mod;
for(int i=4;i<=n;++i) {
c[i]=k*pw[i-2]%mod*(k-2)%mod+k*(k-1)%mod*c[i-2]%mod*inv%mod;
c[i]%=mod;
}
}
I void check1() {
cout<<cnt<<endl;
for(int i=1;i<=cnt;++i) {
for(auto &t:v[i]) {
cout<<t<<' ';
} cout<<endl;
}
}
I int read() {
int ret=0,w=1; char ch;
while((ch=getchar())>'9'||ch<'0'&&ch!='-'); if(ch=='-') w=-1; else ret=ch-'0';
while((ch=getchar())>='0'&&ch<='9') ret=ret*10+ch-'0';
return ret*w;
}
signed main()
{
int T; cin>>T;
while(T--) {
cin>>n>>m>>k; ycl();
for(int i=1;i<=m;++i) {
int x,y; x=read(); y=read();
q[x].push_back(y);
q[y].push_back(x);
}
dfs1(1,0); //check1();
calc();
int ans=1,sum=0;
for(int i=1;i<=cnt;++i) {
sum+=v[i].size();
ans=ans*c[v[i].size()];
ans%=mod;
} //cout<<ans<<' '<<sum<<endl;
if(!cnt) {
ans=pw[n-1]*k%mod;
cout<<ans<<endl;
continue;
}
int inv=qpow(k,mod-2)*(k-1)%mod; //cout<<inv*
ans=ans*qpow(inv,cnt-1)%mod;
ans=ans*qpow(k-1,n-sum)%mod;
cout<<ans<<endl;
}
}
/*
1
6 7 3
1 2
2 3
3 1
4 5
5 6
6 4
1 4
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 28824kb
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: -100
Wrong Answer
time: 62ms
memory: 30152kb
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:
15552 34992 52488 11664 23328 34992 25272 34992 52488 23328 23328 34992 25272 11664 11664 52488 25272 11664 34992 34992 17496 34992 25272 52488 23328 15552 34992 64800 25272 34992 52488 15552 25272 34992 25920 599760 52488 34992 52488 23328 69984 11664 34992 34992 17496 34992 17496 34992 52488 34992...
result:
wrong answer 1st numbers differ - expected: '15120', found: '15552'