結果

問題 No.1116 Cycles of Dense Graph
ユーザー leaf_1415leaf_1415
提出日時 2020-07-17 22:32:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 3,753 bytes
コンパイル時間 1,003 ms
コンパイル使用メモリ 98,812 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2023-08-20 02:09:39
合計ジャッジ時間 3,781 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
9,068 KB
testcase_01 AC 76 ms
9,144 KB
testcase_02 AC 8 ms
9,084 KB
testcase_03 AC 7 ms
8,996 KB
testcase_04 AC 7 ms
9,108 KB
testcase_05 AC 10 ms
9,004 KB
testcase_06 AC 28 ms
9,148 KB
testcase_07 AC 8 ms
9,000 KB
testcase_08 AC 7 ms
9,064 KB
testcase_09 AC 7 ms
8,976 KB
testcase_10 AC 7 ms
9,012 KB
testcase_11 AC 17 ms
9,000 KB
testcase_12 AC 10 ms
8,992 KB
testcase_13 AC 7 ms
9,060 KB
testcase_14 AC 7 ms
9,108 KB
testcase_15 AC 7 ms
9,092 KB
testcase_16 AC 10 ms
9,136 KB
testcase_17 AC 7 ms
9,020 KB
testcase_18 AC 7 ms
9,004 KB
testcase_19 AC 10 ms
8,984 KB
testcase_20 AC 12 ms
8,980 KB
testcase_21 AC 9 ms
9,068 KB
testcase_22 AC 30 ms
8,988 KB
testcase_23 AC 7 ms
9,024 KB
testcase_24 AC 122 ms
9,320 KB
testcase_25 AC 7 ms
9,112 KB
testcase_26 AC 117 ms
9,328 KB
testcase_27 AC 7 ms
9,008 KB
testcase_28 AC 12 ms
9,148 KB
testcase_29 AC 7 ms
9,068 KB
testcase_30 AC 7 ms
8,996 KB
testcase_31 AC 7 ms
9,016 KB
testcase_32 AC 7 ms
8,984 KB
testcase_33 AC 7 ms
8,832 KB
testcase_34 AC 7 ms
9,012 KB
testcase_35 AC 7 ms
9,000 KB
testcase_36 AC 133 ms
9,152 KB
testcase_37 AC 62 ms
9,092 KB
testcase_38 AC 66 ms
9,092 KB
testcase_39 AC 98 ms
9,344 KB
testcase_40 AC 107 ms
9,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 998244353

using namespace std;
typedef pair<llint, llint> P;

struct UnionFind{
	int size;
	vector<int> parent;
	
	UnionFind(){}
	UnionFind(int size){
		this->size = size;
		parent.resize(size+1);
		init();
	}
	void init(){
		for(int i = 0; i <= size; i++) parent[i] = i;
	}
	int root(int i){
		if(parent[i] == i) return i;
		return parent[i] = root(parent[i]);
	}
	bool same(int i, int j){
		return root(i) == root(j);
	}
	void unite(int i, int j){
		int root_i = root(i), root_j = root(j);
		if(root_i == root_j) return;
		parent[root_i] = root_j;
	}
};

const int FACT_MAX = 200005;
llint fact[FACT_MAX], fact_inv[FACT_MAX];

llint modpow(llint a, llint n)
{
	if(n == 0) return 1;
	if(n % 2){
		return ((a%mod) * (modpow(a, n-1)%mod)) % mod;
	}
	else{
		return modpow((a*a)%mod, n/2) % mod;
	}
}

void make_fact()
{
	llint val = 1;
	fact[0] = 1;
	for(int i = 1; i < FACT_MAX; i++){
		val *= i;
		val %= mod;
		fact[i] = val;
	}
	fact_inv[FACT_MAX-1] = modpow(fact[FACT_MAX-1], mod-2);
	for(int i = FACT_MAX-2; i >= 0; i--){
		fact_inv[i] = fact_inv[i+1] * (i+1) % mod;
	}
}

llint comb(llint n, llint k)
{
	llint ret = 1;
	ret *= fact[n];
	ret *= fact_inv[k], ret %= mod;
	ret *= fact_inv[n-k], ret %= mod;
	return ret;
}

llint n, m;
llint a[20], b[20];
llint num[20][40];
vector<llint> G[100005], vec;
UnionFind uf(100005);
llint pop[1<<16];
llint beki[40];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	make_fact();
	
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		cin >> a[i] >> b[i];
		a[i]--, b[i]--;
		vec.push_back(a[i]);
		vec.push_back(b[i]);
	}
	sort(vec.begin(), vec.end());
	vec.erase(unique(vec.begin(), vec.end()), vec.end());
	
	beki[0] = 1;
	for(int i = 1; i < 40; i++) beki[i] = beki[i-1] * 2 % mod;
	
	for(int i = 0; i <= m; i++){
		for(int j = 0; j <= 2*m; j++){
			for(int k = max(0, 3-j); k <= n-j; k++){
				num[i][j] += comb(n-j, k) * fact[k+i-1] % mod;
				num[i][j] %= mod;
			}
			num[i][j] *= ((mod+1)/2), num[i][j] %= mod;
			num[i][j] *= beki[i], num[i][j] %= mod;
		}
	}
	
	llint M = 1<<m, ans = 0;
	for(int i = 1; i < M; i++) pop[i] = pop[i&(i-1)] + 1;
	for(int i = 0; i < M; i++){
		for(int j = 0; j < vec.size(); j++){
			G[vec[j]].clear();
			uf.parent[vec[j]] = vec[j];
		}
		
		llint cycle = 0;
		set<llint> S;
		for(int j = 0; j < m; j++){
			if(i & (1<<j)){
				G[a[j]].push_back(b[j]);
				G[b[j]].push_back(a[j]);
				S.insert(a[j]);
				S.insert(b[j]);
				if(uf.same(a[j], b[j])) cycle++;
				uf.unite(a[j], b[j]);
			}
		}
		bool flag = true;
		for(int j = 0; j < vec.size(); j++){
			if(G[vec[j]].size() >= 3) flag = false;
		}
		if(cycle >= 2) continue;
		if(cycle){
			for(int j = 0; j < vec.size(); j++){
				if(G[vec[j]].size() == 1) flag = false;
			}
		}
		if(!flag) continue;
		
		map<llint, llint> mp;
		for(int j = 0; j < vec.size(); j++) mp[uf.root(vec[j])]++;
		
		llint c = 0, x = S.size();
		for(auto it = mp.begin(); it != mp.end(); it++){
			if(it->second >= 2) c++;
		}
		
		llint tmp = num[c][x];
		if(cycle) tmp = 1;
		if(pop[i] % 2 == 0) ans += tmp, ans %= mod;
		else ans += mod - tmp, ans %= mod;
		
		//cout << i << " " << c << " " << x << " " << tmp << " " << cycle << endl;
	}
	cout << ans << endl;
	
	return 0;
}
0