結果
問題 |
No.2531 Coloring Vertices on Namori
|
ユーザー |
![]() |
提出日時 | 2024-10-24 08:48:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 150 ms / 2,000 ms |
コード長 | 2,433 bytes |
コンパイル時間 | 2,344 ms |
コンパイル使用メモリ | 205,184 KB |
最終ジャッジ日時 | 2025-02-24 22:37:15 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 31 |
ソースコード
#include<bits/stdc++.h> using namespace std; #define debug(x) cerr << #x << " = " << (x) << endl #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define mp make_pair #define pb push_back typedef long long ll; typedef long double ld; typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; struct IOSetup { IOSetup() { cin.tie(nullptr)->sync_with_stdio(false); cout << fixed << setprecision(16); cerr << fixed << setprecision(16); } } iosetup; template <typename T1, typename T2> ostream &operator <<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator >>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator <<(ostream &os, const vector<T> &v) { for(int i = 0; i < (int) v.size(); i++) os << v[i] << (i+1 == (int) v.size() ? "" : " "); return os; } template <typename T> istream &operator >>(istream &is, vector<T> &v) { for(int i = 0; i < (int) v.size(); i++) is >> v[i]; return is; } template <typename T> bool chmax(T &a, const T &b) { if(a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if(a > b) { a = b; return true; } return false; } //////////////////////////////////////////////////////// #include<atcoder/modint> using mint = atcoder::modint998244353; istream &operator >>(istream &is, mint &a) { int x; is >> x; a = x; return is; } ostream &operator <<(ostream &os, const mint &a) { return os << a.val(); } int main() { int N, K; cin >> N >> K; vector<vector<int>> G(N); vector<int> deg(N); for(int i = 0; i < N; i++) { int u, v; cin >> u >> v; u--; v--; G[u].pb(v); G[v].pb(u); deg[u]++; deg[v]++; } vector<bool> visited(N, false), cycle(N, true); auto dfs = [&](auto &self, int now) -> void { if(deg[now] != 1) return; cycle[now] = false; if(visited[now]) return; visited[now] = true; for(int nxt : G[now]) { if(visited[nxt]) continue; deg[nxt]--; if(deg[nxt] == 1) self(self, nxt); } }; for(int i = 0; i < N; i++) dfs(dfs, i); int cnt = accumulate(all(cycle), 0), res = N - cnt; auto solve = [](auto &self, int N, int M) -> mint { if(N == 2) return mint(M) * (M-1); else return mint(M) * mint(M-1).pow(N-1) - self(self, N-1, M); }; cout << mint(K-1).pow(res) * solve(solve, cnt, K) << endl; return 0; }