結果
問題 | No.1116 Cycles of Dense Graph |
ユーザー |
![]() |
提出日時 | 2020-07-17 23:18:40 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 72 ms / 2,000 ms |
コード長 | 2,761 bytes |
コンパイル時間 | 1,766 ms |
コンパイル使用メモリ | 140,312 KB |
最終ジャッジ日時 | 2025-01-11 23:26:51 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #include <utility> #include <functional> #include <time.h> #include <stack> #include <array> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<int, int> P; const ll MOD=998244353; ll powmod(ll a, ll k){ ll ap=a, ans=1; while(k){ if(k&1){ ans*=ap; ans%=MOD; } ap=ap*ap; ap%=MOD; k>>=1; } return ans; } ll inv(ll a){ return powmod(a, MOD-2); } ll f[1000010], invf[1000010]; void fac(int n){ f[0]=1; for(ll i=1; i<=n; i++) f[i]=f[i-1]*i%MOD; invf[n]=inv(f[n]); for(ll i=n-1; i>=0; i--) invf[i]=invf[i+1]*(i+1)%MOD; } ll comb(int x, int y){ if(!(0<=y && y<=x)) return 0; return f[x]*invf[y]%MOD*invf[x-y]%MOD; } struct unionfind{ vector<int> par, sz; unionfind() {} unionfind(int n):par(n), sz(n, 1){ for(int i=0; i<n; i++) par[i]=i; } int find(int x){ if(par[x]==x) return x; return par[x]=find(par[x]); } void unite(int x, int y){ x=find(x); y=find(y); if(x==y) return; if(sz[x]>sz[y]) swap(x, y); par[x]=y; sz[y]+=sz[x]; } bool same(int x, int y){ return find(x)==find(y); } int size(int x){ return sz[find(x)]; } }; int n, m; int a[20], b[20]; ll dp[1<<15]; map<P, ll> memo; int main() { cin>>n>>m; for(int i=0; i<m; i++){ cin>>a[i]>>b[i]; a[i]--; b[i]--; } fac(n); unionfind uf(n); for(int i=0; i<(1<<m); i++){ map<int, int> deg; int c=0, l=0; int myon=0; for(int j=0; j<m; j++){ if(i&(1<<j)){ c++; deg[a[j]]++; deg[b[j]]++; if(uf.same(a[j], b[j])){ myon++; } uf.unite(a[j], b[j]); } } for(auto p:deg) uf.par[p.first]=p.first, uf.sz[p.first]=1; if(myon>1) continue; if(myon){ bool cyc=1; for(auto p:deg){ if(p.second!=2) cyc=0; } if(cyc) dp[i]=1; continue; } int t=deg.size(); for(auto p:deg){ if(p.second==1) l++; } if(l!=2*(t-c)) continue; if(memo.find({t, c})!=memo.end()){ dp[i]=memo[{t, c}]; continue; } for(int k=max(0, 3-t); k<=n-t; k++){ (dp[i]+=comb(n-t, k)*f[t-c+k-1])%=MOD; } dp[i]=dp[i]*powmod(2, t-c)%MOD*((MOD+1)/2)%MOD; memo[{t, c}]=dp[i]; } for(int i=(1<<m)-1; i>=0; i--){ int x=(1<<m)-1-i; for(int j=x; j>0; j=(j-1)&x){ dp[i]+=MOD-dp[i^j]; } dp[i]%=MOD; } cout<<dp[0]<<endl; return 0; }