結果

問題 No.2435 Order All Company
ユーザー 沙耶花
提出日時 2023-08-18 22:20:30
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,543 bytes
コンパイル時間 4,231 ms
コンパイル使用メモリ 258,940 KB
最終ジャッジ日時 2025-02-16 10:15:43
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001
mint tp = 1;
void Gauss(vector<vector<mint>> &A){
	if(A.size()==0)return;
	int last = 0;
	rep(i,A[0].size()){
		int ind = -1;
		for(int j=last;j<A.size();j++){
			if(A[j][i]!=0){
				ind = j;
				break;
			}
		}
		if(ind==-1)continue;
		if(last!=ind)tp *= -1;
		swap(A[last],A[ind]);
		
		mint x = A[last][i].inv();
		tp *= x;
		rep(j,A[last].size()){
			A[last][j] *= x;
			
		}
		
		rep(j,A.size()){
			if(j==last)continue;
			x = A[j][i];
			if(x==0)continue;
			rep(k,A[j].size()){
				A[j][k] -= A[last][k] * x;
			}
		}
		
		last++;
	}
}

int main(){
	
	int N,K;
	cin>>N>>K;
	
	vector<vector<pair<int,int>>> t(K);
	rep(i,K){
		int tt;
		cin>>tt;
		t[i].resize(tt);
		rep(j,tt){
			cin>>t[i][j].first>>t[i][j].second;
			t[i][j].first--;
			t[i][j].second--;
		}
	}
	mint ans = 0;
	rep(i,1<<K){
		
		vector<vector<mint>> a(N,vector<mint>(N,0));
		rep(j,K){
			if((i>>j)&1){
				rep(k,t[j].size()){
					int u = t[j][k].first,v = t[j][k].second;
					a[u][u]++;
					a[v][v]++;
					a[u][v]--;
					a[v][u]--;
				}
			}
		}
		a.pop_back();
		rep(j,N-1)a[j].pop_back();
		tp = 1;
		Gauss(a);
		tp = tp.inv();
		if(a.size()!=N-1)tp = 0;
		else{
			rep(j,N-1){
				tp *= a[j][j];
			}
		}
		if(K%2 != __builtin_popcount(i)%2)tp *= -1;
		ans += tp;
	}
	
	cout<<ans.val()<<endl;
	
	return 0;
}
0