1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include<cstdio> #include<algorithm> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) using namespace std;
typedef long long LL;
const int maxn=5e5+5, maxm=1e6+5; const LL mo=998244353;
int n,m;
int ReadInt() { char ch=getchar(); int data=0; while (ch<'0' || ch>'9') ch=getchar(); do{ data=data*10+ch-'0'; ch=getchar(); } while (ch>='0' && ch<='9'); return data; }
int tot,fro[2*maxm],go[2*maxm],next[2*maxm],f1[maxn]; bool bt[2*maxm]; void ins(int x,int y) { fro[++tot]=x; go[tot]=y; bt[tot]=1; next[tot]=f1[x]; f1[x]=tot; }
int dfn[maxn],low[maxn],sum,z[maxn],z0,rt[maxn]; bool bz[maxn]; bool tarjan(int k,int last) { dfn[k]=low[k]=++sum; bz[k]=1; z[++z0]=k; bool pd=0; for(int p=f1[k]; p; p=next[p]) if (go[p]!=last) { if (!bz[go[p]]) { if (!tarjan(go[p],k)) return 0; low[k]=min(low[k],low[go[p]]); if (low[go[p]]<dfn[k]) { if (pd) return 0; pd=1; } } else { low[k]=min(low[k],dfn[go[p]]); if (dfn[go[p]]<dfn[k]) { if (pd) return 0; pd=1; } } } if (dfn[k]==low[k]) { for(; z[z0]!=k; z0--) rt[z[z0]]=k; rt[k]=k; z0--; } return 1; }
LL f[maxn],g[maxn],F[maxn]; void dfs(int k,int last) { rt[k]=0; LL sum=1; int num=0; for(int p=f1[k]; p; p=next[p]) if (bt[p] && go[p]!=last) { dfs(go[p],k); num++; sum=sum*g[go[p]]%mo; } f[k]=F[num]*sum%mo; g[k]=(num==0) ?1 :(f[k]+sum*F[num-1]%mo*num)%mo ; }
int T; int main() { F[0]=F[1]=1; fo(i,2,500000) F[i]=(F[i-1]+F[i-2]*(i-1))%mo; scanf("%d",&T); while (T--) { n=ReadInt(), m=ReadInt(); tot=0; fo(i,1,n) f1[i]=bz[i]=0; fo(i,1,m) { int x=ReadInt(), y=ReadInt(); ins(x,y), ins(y,x); } sum=0; if (!tarjan(1,0)) {printf("0\n"); continue;} fo(p,1,tot) if (rt[fro[p]]==rt[go[p]]) bt[p]=0; LL ans=1; fo(i,1,n) if (rt[i]) { dfs(i,0); ans=ans*f[i]%mo; } printf("%lld\n",ans); } }
|