QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#454712 | #856. Cactus | Nana7 | AC ✓ | 745ms | 96416kb | C++14 | 2.3kb | 2024-06-25 10:44:25 | 2024-06-25 10:44:25 |
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],f[N],g[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);
g[1]=0; f[1]=k; f[2]=k*(k-1)%mod; f[3]=k*(k-1)%mod*(k-2)%mod; g[3]=f[3];
for(int i=4;i<=n;++i) {
f[i]=(k-1)*f[i-2]%mod+g[i-1]*(k-2)%mod;
f[i]=(f[i]>=mod?f[i]-mod:f[i]);
g[i]=(k-1)*f[i-2]%mod+(k-2)*g[i-1]%mod;
g[i]=(g[i]>=mod?g[i]-mod:g[i]);
}
}
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); //cout<<"sasa"; check1();
calc();
// for(int i=1;i<=n;++i) cout<<f[i]<<' '; cout<<endl;
int ans=1,sum=0;
for(int i=1;i<=cnt;++i) {
sum+=v[i].size();
ans=ans*f[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;
}
}
/*
2
9 10 4
4 7
5 2
1 5
7 3
9 6
8 3
3 2
9 1
4 8
6 2
8 9 2
1 2
2 3
3 4
4 1
5 6
6 7
7 8
8 5
1 8
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 30832kb
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: 68ms
memory: 30676kb
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: 745ms
memory: 96416kb
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