結果

問題 No.1303 Inconvenient Kingdom
ユーザー 👑 potato167potato167
提出日時 2022-02-27 03:31:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,110 bytes
コンパイル時間 3,068 ms
コンパイル使用メモリ 223,352 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-18 09:43:41
合計ジャッジ時間 7,457 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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