結果
問題 | No.1303 Inconvenient Kingdom |
ユーザー |
👑 ![]() |
提出日時 | 2022-02-27 03:32:24 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 4,110 bytes |
コンパイル時間 | 3,090 ms |
コンパイル使用メモリ | 218,180 KB |
最終ジャッジ日時 | 2025-01-28 03:37:18 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 RE * 1 |
other | AC * 8 WA * 8 RE * 18 |
ソースコード
#include <bits/stdc++.h>#pragma GCC optimize("Ofast")#define _GLIBCXX_DEBUGusing namespace std;using std::cout;using std::cin;using std::endl;using ll=long long;using ld=long double;ll ILL=1167167167167167167;const int INF=2100000000;const ll mod=998244353;#define rep(i,a) for (ll i=0;i<a;i++)template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}namespace po167{long long rev(long long a,long long mod){a%=mod;long long ans=1;long long H=mod-2;while(H){if(H&1) ans=(ans*a)%mod;a=(a*a)%mod;H>>=1;}return ans;}long long Determinant_Matrix(std::vector<std::vector<long long>> G,long long MOD){int N=G.size();long long ans=1;for(int i=0;i<N;i++){for(int j=i;j<N;j++){G[j][i]=(G[j][i]%MOD+MOD)%MOD;if(G[j][i]!=0){if(j!=i){for(int k=i;k<N;k++){std::swap(G[i][k],G[j][k]);}ans*=-1;}break;}}if(G[i][i]==0){return 0;}ans=(ans*G[i][i])%MOD;long long R=rev(G[i][i],MOD);for(int j=i+1;j<N;j++){long long D=(R*G[j][i])%MOD;for(int k=i;k<N;k++){G[j][k]=(G[j][k]-(D*G[i][k])%MOD+MOD)%MOD;}}}return (ans+MOD)%MOD;}//行列木定理long long Kirchhoffs_theorem(std::vector<std::vector<long long>> G,long long MOD){int N=G.size();std::vector<std::vector<long long>> H(N-1,std::vector<long long>(N-1));for(int i=0;i<N-1;i++){for(int j=0;j<N;j++){if(i==j) continue;H[i][i]=(H[i][i]+G[i][j])%MOD;if(j!=N-1){H[i][j]=(MOD-G[i][j])%MOD;}}}return Determinant_Matrix(H,MOD);}}using po167::Determinant_Matrix;using po167::Kirchhoffs_theorem;namespace po167{struct UFtree{std::vector<int> wei;std::vector<int> q;int component;UFtree(int n):wei(n),component(n),par(n){for(int i=0;i<n;i++){wei[i]=1,par[i]=i;}}void intialize(){for(auto x:q){wei[root(x)]=1;par[x]=x;}component=(int)par.size();q={};}//根っこを返すint root(int a){if(a==par[a]) return a;return par[a]=root(par[a]);}//trueなら1,falseなら0int same(int a,int b){if(root(a)==root(b)) return 1;else return 0;}//a,bが違う根っこの元なら結合する,結合したらtrueを返すbool unite(int a,int b){a=root(a),b=root(b);if(a==b) return false;if(wei[a]<wei[b]) std::swap(a,b);par[b]=a;q.push_back(b);wei[a]+=wei[b];component--;return true;}private:std::vector<int> par;};}using po167::UFtree;void solve();// oddloopint main() {ios::sync_with_stdio(false);cin.tie(nullptr);int t=1;//cin>>t;rep(i,t) solve();}void solve(){int N,M;cin>>N>>M;int L=N*(N-1)/2;vector<vector<int>> G(N,vector<int>(N));UFtree T(N);rep(i,M){int a,b;cin>>a>>b;a--,b--;G[a][b]=1;G[b][a]=1;if(T.same(a,b)) continue;L-=T.wei[T.root(a)]*T.wei[T.root(b)];T.unite(a,b);}vector<vector<int>> H(N);rep(i,N){H[T.root(i)].push_back(i);}long long A=0,B=0,ans=1;vector<long long> D;rep(i,N){long long C=H[i].size();if(C==0) continue;D.push_back(C);if(chmax(B,C)){if(A<B) swap(A,B);}if(C<=1) continue;vector<vector<long long>> I(C,vector<long long>(C));rep(j,C) rep(k,C){I[j][k]=G[H[i][j]][H[i][k]];}ans=(ans*Kirchhoffs_theorem(I,mod))%mod;}Sore(D);long long E=1;if(D.size()!=1){if(D[0]==D[1]){while(E<(int)D.size()&&D[E]==D[0]) E++;}else{while(E<(int)D.size()&&D[E]==D[1]) E++;E--;}}cout<<2*(L-A*B)<<"\n";assert(D.size()!=1);cout<<(ans*A*B*E)%mod<<"\n";}