結果
| 問題 |
No.1116 Cycles of Dense Graph
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2020-07-17 23:32:48 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,055 bytes |
| コンパイル時間 | 1,442 ms |
| コンパイル使用メモリ | 108,528 KB |
| 実行使用メモリ | 8,164 KB |
| 最終ジャッジ日時 | 2024-11-30 04:11:08 |
| 合計ジャッジ時間 | 3,769 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 WA * 3 |
ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define N_MAX 200002
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll MOD = 998244353;
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) { }
bool unionSet(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool findSet(int x, int y) {
return root(x) == root(y);
}
int root(int x) {
return data[x] < 0 ? x : data[x] = root(data[x]);
}
int size(int x) {
return -data[root(x)];
}
};
ll inv[N_MAX],fac[N_MAX],finv[N_MAX];
void init(){
fac[0]=fac[1]=1;
finv[0]=finv[1]=1;
inv[1]=1;
for(int i=2;i<N_MAX;i++){
inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
fac[i]=fac[i-1]*(ll) i%MOD;
finv[i]=finv[i-1]*inv[i]%MOD;
}
}
ll inv_(ll n){
if(n == 1) return 1;
else return MOD-inv_(MOD%n)*(MOD/n)%MOD;
}
ll comb(ll n, ll r){
ll ans;
if(n < r){
ans = 0;
}else{
ans = (fac[n]*finv[r])%MOD;
ans = (ans*finv[n-r])%MOD;
ans = (ans+MOD)%MOD;
}
return ans;
}
map<P, ll> mp;
ll calc(ll n, ll k){
ll ans = 0;
for(ll i = 0; i <= n-k; i++){
if(k+i <= 1) continue;
if(k+i == 2 && k == 0) continue;
if(k == 1 && i == 1){
ans += comb(n-k, i);
continue;
}
if(k == 2 && i == 0){
ans += 2*comb(n-k, i);
continue;
}
ll tmp = (fac[k+i]*(1<<k))%MOD;
tmp *= inv[2];
tmp %= MOD;
tmp *= inv[k+i];
tmp %= MOD;
tmp *= comb(n-k, i);
tmp %= MOD;
ans += tmp;
ans %= MOD;
}
mp[P(n, k)] = ans;
return ans;
}
int N, M;
int a[15], b[15];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
init();
cin >> N >> M;
for(int i = 0; i < M; i++) {
cin >> a[i] >> b[i];
}
mp[P(2, 2)] = 2;
mp[P(2, 1)] = 1;
ll ans = 0;
for(int i = 0; i < (1<<M); i++){
set<int> st;
int c = 0;
for(int j = 0; j < M; j++){
if(i&(1<<j)){
c++;
st.insert(a[j]); st.insert(b[j]);
}
}
int sz = st.size();
map<int, int> idx;
int cur = 0;
for(int j : st){
idx[j] = cur;
cur++;
}
UnionFind uf(sz);
bool ok = true;
for(int j = 0; j < M; j++){
if(i&(1<<j)){
int a_idx = idx[a[j]];
int b_idx = idx[b[j]];
if(uf.findSet(a_idx, b_idx)){
ok = false;
}else{
uf.unionSet(a_idx, b_idx);
}
}
}
set<int> st_root;
for(int j = 0; j < sz; j++) {
int r = uf.root(j);
st_root.insert(r);
}
if(!ok) {
vector<int> v_cnt(sz);
for(int j = 0; j < M; j++){
if(i&(1<<j)){
int a_idx = idx[a[j]];
int b_idx = idx[b[j]];
v_cnt[a_idx]++;
v_cnt[b_idx]++;
}
}
bool loop = true;
for(int j : v_cnt){
if(j != 2) loop = false;
}
if(loop && st_root.size() == 1) {
ll sgn = (c%2 == 0 ? 1 : -1);
ans += sgn;
}
continue;
}
int rem = N-sz+st_root.size();
int k = st_root.size();
ll sgn = (c%2 == 0 ? 1 : -1);
ll tmp;
if(mp.count(P(rem, k)) == 0){
tmp = calc(rem, k);
}else{
tmp = mp[P(rem, k)];
}
if(k == 1 && uf.size(0) >= 3) tmp++;
ans += (sgn*tmp)%MOD;
ans %= MOD;
}
cout << (ans+MOD)%MOD << endl;
}
milanis48663220