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
| #include<algorithm> #include<bitset> #include<complex> #include<deque> #include<exception> #include<fstream> #include<functional> #include<iomanip> #include<ios> #include<iosfwd> #include<iostream> #include<istream> #include<iterator> #include<limits> #include<list> #include<locale> #include<map> #include<memory> #include<new> #include<numeric> #include<ostream> #include<queue> #include<set> #include<sstream> #include<stdexcept> #include<streambuf> #include<string> #include<typeinfo> #include<utility> #include<valarray> #include<vector> #include<cctype> #include<cerrno> #include<cfloat> #include<ciso646> #include<climits> #include<clocale> #include<cmath> #include<csetjmp> #include<csignal> #include<cstdarg> #include<cstddef> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #define MAXN 5000010 #define int long long using namespace std; inline int read(){ int res=0,f=1;char ch=getchar(); while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar(); return res*f; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x<10) putchar(x+'0'); else{ write(x/10); putchar(x%10+'0'); } } int n,m,mod,ans=1,sum,las,num; int fa[MAXN],u[MAXN],v[MAXN],g[MAXN],f[MAXN],vis[MAXN],def[MAXN],cnt; int getfa(int x){ return x==f[x]?x:f[x]=getfa(f[x]); } int fir[MAXN],nxt[MAXN],son[MAXN],tot,w[MAXN]; void add(int x,int y,int z){ ++tot; nxt[tot]=fir[x]; fir[x]=tot; son[tot]=y; w[tot]=z; } void dfs(int x){ def[x]=def[fa[x]]+1; f[x]=x; for(int i=fir[x];i;i=nxt[i]){ int to=son[i]; if(to==x) continue if(to==fa[x]) continue ; fa[to]=x; dfs(to); } } signed main(){ n=read();m=read();mod=read();las=0; for(int i=1;i<=n;i++) f[i]=i; memset(fa,0,sizeof(fa)); cnt=0; for(int i=1;i<=m;i++){ u[i]=read();v[i]=read(); if(getfa(u[i])==getfa(v[i])){ vis[i]=0; }else{ vis[i]=1; add(u[i],v[i],0);cnt++; add(v[i],u[i],0);cnt++; f[f[u[i]]]=v[i]; } } for(int i=1;i<=n;i++){ if(def[i]==0){ dfs(i); } } for(int i=1;i<=m;i++){ if(vis[i]==1) sum++; else{ for(int x=getfa(u[i]),y=getfa(v[i]);x!=y;--sum,x=f[x]=getfa(fa[x])){ if(def[x]<def[y]) swap(x,y); } } ans*=sum+1;ans%=mod; } write(ans);putchar('\n'); return 0; }
|