結果
問題 | No.2531 Coloring Vertices on Namori |
ユーザー | shobonvip |
提出日時 | 2023-11-03 22:05:52 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 267 ms / 2,000 ms |
コード長 | 2,591 bytes |
コンパイル時間 | 4,745 ms |
コンパイル使用メモリ | 268,392 KB |
実行使用メモリ | 27,608 KB |
最終ジャッジ日時 | 2023-11-03 22:06:04 |
合計ジャッジ時間 | 11,190 ms |
ジャッジサーバーID (参考情報) |
judge10 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 5 ms
11,488 KB |
testcase_01 | AC | 5 ms
11,488 KB |
testcase_02 | AC | 5 ms
11,488 KB |
testcase_03 | AC | 5 ms
11,488 KB |
testcase_04 | AC | 6 ms
11,488 KB |
testcase_05 | AC | 150 ms
26,812 KB |
testcase_06 | AC | 5 ms
11,488 KB |
testcase_07 | AC | 154 ms
26,812 KB |
testcase_08 | AC | 195 ms
26,812 KB |
testcase_09 | AC | 196 ms
26,812 KB |
testcase_10 | AC | 194 ms
26,812 KB |
testcase_11 | AC | 125 ms
27,608 KB |
testcase_12 | AC | 125 ms
27,608 KB |
testcase_13 | AC | 123 ms
27,608 KB |
testcase_14 | AC | 181 ms
26,812 KB |
testcase_15 | AC | 177 ms
26,812 KB |
testcase_16 | AC | 180 ms
26,812 KB |
testcase_17 | AC | 5 ms
11,488 KB |
testcase_18 | AC | 4 ms
11,488 KB |
testcase_19 | AC | 5 ms
11,488 KB |
testcase_20 | AC | 241 ms
26,624 KB |
testcase_21 | AC | 214 ms
26,624 KB |
testcase_22 | AC | 252 ms
26,624 KB |
testcase_23 | AC | 232 ms
26,620 KB |
testcase_24 | AC | 247 ms
26,620 KB |
testcase_25 | AC | 213 ms
26,624 KB |
testcase_26 | AC | 257 ms
26,624 KB |
testcase_27 | AC | 228 ms
26,624 KB |
testcase_28 | AC | 255 ms
26,624 KB |
testcase_29 | AC | 256 ms
26,624 KB |
testcase_30 | AC | 251 ms
26,624 KB |
testcase_31 | AC | 264 ms
26,624 KB |
testcase_32 | AC | 231 ms
26,620 KB |
testcase_33 | AC | 267 ms
26,624 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; //* ATCODER #include<atcoder/all> using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include<boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template <typename T> bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template <typename T> bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template <typename T> T max(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template <typename T> T min(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template <typename T> T sum(vector<T> &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } //defmodfact const int COMinitMAX = 998244; mint fact[COMinitMAX+1], factinv[COMinitMAX+1]; void modfact(){ fact[0] = 1; for (int i=1; i<=COMinitMAX; i++){ fact[i] = fact[i-1] * i; } factinv[COMinitMAX] = fact[COMinitMAX].inv(); for (int i=COMinitMAX-1; i>=0; i--){ factinv[i] = factinv[i+1] * (i+1); } } mint cmb(int a, int b){ if (a<b || b<0) return mint(0); return fact[a]*factinv[b]*factinv[a-b]; } //-------- int main(){ int n, k; cin >> n >> k; vector<vector<int>> ikeru(n, vector<int>(0)); vector<int> deg(n); dsu uf(n); rep(i,0,n){ int u, v; cin >> u >> v; u--; v--; ikeru[u].push_back(v); ikeru[v].push_back(u); deg[u]++; deg[v]++; uf.merge(u, v); } vector<int> tansaku(n); vector<bool> is_heiro(n, true); vector<int> mada = {}; rep(i,0,n){ if (deg[i] == 1){ mada.push_back(i); } } while(!mada.empty()){ int i = mada.back(); mada.pop_back(); is_heiro[i] = false; for (int j: ikeru[i]){ deg[j]--; if (deg[j] == 1){ mada.push_back(j); } } } vector<int> ks(n); rep(i,0,n){ if (is_heiro[i]){ ks[uf.leader(i)]++; } } mint ans = 1; rep(i,0,n){ if (ks[i] >= 1){ ans *= mint(k-1).pow(ks[i]) + mint(-1).pow(ks[i]) * mint(k-1); } } rep(i,0,n){ if (is_heiro[i]){ tansaku[i] = 1; mada.push_back(i); } } vector<mint> dp(n); while(!mada.empty()){ int i = mada.back(); mada.pop_back(); for (int j: ikeru[i]){ if (tansaku[j] == 1) continue; tansaku[j] = 1; ans *= k-1; mada.push_back(j); } } cout << ans.val() << '\n'; }