This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Copyright © 2022 Youngmin Park. All rights reserved.
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define REP(a) F0R(_, a)
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 998244353;
const ld PI = acos((ld)-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
os << '{';
string sep;
for (const T &x : v)
os << sep << x, sep = ", ";
return os << '}';
}
void dbg_out()
{
cerr << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
cerr << ' ' << H;
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 42
#endif
inline namespace RecursiveLambda{
template <typename Fun>
struct y_combinator_result{
Fun fun_;
template <typename T>
explicit y_combinator_result(T &&fun): fun_(forward<T>(fun)){}
template <typename...Args>
decltype(auto) operator()(Args &&...args){
return fun_(ref(*this), forward<Args>(args)...);
}
};
template <typename Fun>
decltype(auto) y_combinator(Fun &&fun){
return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
}
};
void setIO(string s) // USACO
{
#ifndef LOCAL
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
#endif
}
int dp[1 << 18];
bool g[18][18];
int fact[19];
vi adj[18], comp;
int ID[18];
bool vis[18];
int add(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
int mul(int a, int b) {
return 1LL * a * b;
}
void dfs(int i) {
vis[i] = 1;
comp.pb(i);
for (auto &e : adj[i]) if (!vis[e]) dfs(e);
}
void solve()
{
int n, m;
cin >> n >> m;
REP(m) {
int u, v;
cin >> u >> v;
u--, v--;
g[v][u] = 1;
adj[u].pb(v), adj[v].pb(u);
}
fact[0] = 1;
FOR(i, 1, n + 1) {
fact[i] = mul(fact[i - 1], i);
}
int ans = 1;
F0R(i, n) {
if (!vis[i]) {
comp.clear();
dfs(i);
fill(dp, dp + (1 << sz(comp)), 0);
dbg(comp);
F0R(mask, 1 << sz(comp)) {
F0R(j, sz(comp)) {
if (!(mask & (1 << j))) continue;
int pmask = mask - (1 << j);
int cnt{};
F0R(k, sz(comp)) {
if (!(mask & (1 << k))) continue;
dbg(comp[j], comp[k]);
cnt += g[comp[j]][comp[k]];
}
dbg(cnt);
dp[mask] = add(dp[mask], dp[pmask]);
dp[mask] = add(dp[mask], mul(cnt, fact[__builtin_popcount(pmask)]));
dbg(mask, dp[mask]);
}
}
if (sz(comp) == 1) {
continue;
}
ans = mul(ans, dp[(1 << sz(comp)) - 1]);
dbg(ans);
}
}
cout << ans;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int testcase=1;
// cin >> testcase;
while (testcase--)
{
solve();
}
}
Compilation message (stderr)
amusementpark.cpp: In function 'void solve()':
amusementpark.cpp:71:18: warning: statement has no effect [-Wunused-value]
71 | #define dbg(...) 42
| ^~
amusementpark.cpp:142:4: note: in expansion of macro 'dbg'
142 | dbg(comp);
| ^~~
amusementpark.cpp:71:18: warning: statement has no effect [-Wunused-value]
71 | #define dbg(...) 42
| ^~
amusementpark.cpp:150:7: note: in expansion of macro 'dbg'
150 | dbg(comp[j], comp[k]);
| ^~~
amusementpark.cpp:71:18: warning: statement has no effect [-Wunused-value]
71 | #define dbg(...) 42
| ^~
amusementpark.cpp:154:6: note: in expansion of macro 'dbg'
154 | dbg(cnt);
| ^~~
amusementpark.cpp:71:18: warning: statement has no effect [-Wunused-value]
71 | #define dbg(...) 42
| ^~
amusementpark.cpp:157:6: note: in expansion of macro 'dbg'
157 | dbg(mask, dp[mask]);
| ^~~
amusementpark.cpp:71:18: warning: statement has no effect [-Wunused-value]
71 | #define dbg(...) 42
| ^~
amusementpark.cpp:164:4: note: in expansion of macro 'dbg'
164 | dbg(ans);
| ^~~
amusementpark.cpp: In function 'void setIO(std::string)':
amusementpark.cpp:94:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
amusementpark.cpp:95:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |