QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#39923 | #856. Cactus | Stargazer | AC ✓ | 702ms | 60428kb | C++ | 4.3kb | 2022-07-14 20:56:21 | 2022-07-14 20:56:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define y1 shinkle
#define fi first
#define se second
#define bg begin
namespace IO{
#define gc getchar
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
return f?res:-res;
}
inline ll readll(){
char ch=gc();
ll res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
return f?res:-res;
}
inline char readchar(){
char ch=gc();
while(isspace(ch))ch=gc();
return ch;
}
inline int readstring(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
s[top+1]='\0';return top;
}
}
using IO::read;
using IO::readll;
using IO::readchar;
using IO::readstring;
template<typename tp>inline void chemx(tp &a,tp b){(a<b)?(a=b):0;}
template<typename tp>inline void chemn(tp &a,tp b){(a>b)?(a=b):0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+b)>=mod?(a+b-mod):(a+b);}
inline int dec(int a,int b){return (a<b)?(a-b+mod):(a-b);}
inline int mul(int a,int b){static ll r;r=(ll)a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){a=(a+b)>=mod?(a+b-mod):(a+b);}
inline void Dec(int &a,int b){a=(a<b)?(a-b+mod):(a-b);}
inline void Mul(int &a,int b){static ll r;r=(ll)a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(ll x){x%=mod;return (x<0)?x+mod:x;}
cs int N=300005,M=400005;
int adj[N],nxt[M<<1],to[M<<1],ecnt;
int n,m;
int K;
int pre[N],bel[N],cnt,up[N];
vector<int> nd[N];
int f[N];
int ff[N],g[N][2];
int vis[N];
void dp(int u,int fa){
// cout<<u<<'\n';
f[u]=1;
if(!bel[u]){
for(int e=adj[u],v;e;e=nxt[e])if((e^1)!=fa){
v=to[e];
dp(v,e);
Mul(f[u],mul(f[v],K-1));
}
}
else{
int tp=bel[u];
assert(u==up[tp]);
int pr=1;
for(int i=0;i<nd[tp].size();i++){
int v=nd[tp][i];f[v]=1;
for(int e=adj[v];e;e=nxt[e])if(bel[to[e]]!=bel[v]&&(e^1)!=fa){
dp(to[e],e);
Mul(f[v],mul(f[to[e]],K-1));
}
Mul(pr,f[v]);
ff[i]=f[v];
}
g[0][0]=1;
// g[0][0]=ff[0];
g[0][1]=0;
for(int i=1;i<nd[tp].size();i++){
g[i][0]=g[i-1][1];
g[i][1]=add(mul(K-1,g[i-1][0]),mul(K-2,g[i-1][1]));
// g[i][0]=mul(g[i-1][1],ff[i]);
// g[i][1]=mul(ff[i],add(mul(K-1,g[i-1][0]),mul(K-2,g[i-1][1])));
// cout<<g[i][0]<<" "<<g[i][1]<<'\n';
}
f[u]=mul(g[nd[tp].size()-1][1],pr);
// cout<<u<<" "<<pr<<" "<<f[u]<<" "<<nd[tp].size()<<'\n';
}
}
void dfs1(int u,int fa){
vis[u]=1;
for(int e=adj[u],v;e;e=nxt[e])if((e^1)!=fa){
v=to[e];
// if(bel[u]&&bel[u]==bel[v])continue;
if(vis[v]){
if(bel[v])continue;
cnt++;
up[cnt]=v;
int p=u;
while(p!=v){
bel[p]=cnt;
nd[cnt].pb(p);
p=pre[p];
}
bel[v]=cnt;
nd[cnt].pb(v);
reverse(nd[cnt].bg(),nd[cnt].end());
}
else{
pre[v]=u,dfs1(v,e);
}
}
}
void solve(){
ecnt=1;
n=read(),m=read(),K=read();
for(int i=1;i<=m;i++){
int u=read(),v=read();
nxt[++ecnt]=adj[u],adj[u]=ecnt,to[ecnt]=v;
nxt[++ecnt]=adj[v],adj[v]=ecnt,to[ecnt]=u;
}
dfs1(1,0);
// for(int i=1;i<=n;i++)cout<<bel[i]<<" ";puts("");
// cout<<"ok1\n";
dp(1,0);
//for(int i=1;i<=n;i++)cout<<f[i]<<" ";puts("");
cout<<mul(f[1],K)<<'\n';
memset(adj,0,sizeof(int)*(n+1));
memset(f,0,sizeof(int)*(n+1));
memset(vis,0,sizeof(int)*(n+1));
memset(bel,0,sizeof(int)*(n+1));
memset(pre,0,sizeof(int)*(n+1));
memset(up,0,sizeof(int)*(n+1));
for(int i=1;i<=cnt;i++)nd[i].clear();
cnt=0;
return;
}
int main(){
//freopen("1.in","r",stdin);
int T=read();
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 21968kb
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: 35ms
memory: 24116kb
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: 702ms
memory: 60428kb
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