結果

問題 No.1303 Inconvenient Kingdom
ユーザー 👑 potato167potato167
提出日時 2022-02-27 03:32:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,110 bytes
コンパイル時間 3,162 ms
コンパイル使用メモリ 222,740 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-18 09:44:23
合計ジャッジ時間 6,992 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 RE -
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 8 ms
4,376 KB
testcase_08 AC 9 ms
4,376 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 WA -
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 2 ms
4,380 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 RE -
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using 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なら0
	int 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();
// oddloop
int 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";
}

0