結果
問題 | No.2531 Coloring Vertices on Namori |
ユーザー | umimel |
提出日時 | 2023-11-03 22:15:29 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 197 ms / 2,000 ms |
コード長 | 5,900 bytes |
コンパイル時間 | 2,189 ms |
コンパイル使用メモリ | 187,704 KB |
実行使用メモリ | 30,320 KB |
最終ジャッジ日時 | 2024-09-25 20:39:19 |
合計ジャッジ時間 | 7,359 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 83 ms
29,680 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 84 ms
29,812 KB |
testcase_08 | AC | 145 ms
30,320 KB |
testcase_09 | AC | 145 ms
30,316 KB |
testcase_10 | AC | 143 ms
30,188 KB |
testcase_11 | AC | 60 ms
21,176 KB |
testcase_12 | AC | 60 ms
21,176 KB |
testcase_13 | AC | 60 ms
21,176 KB |
testcase_14 | AC | 127 ms
29,808 KB |
testcase_15 | AC | 128 ms
29,684 KB |
testcase_16 | AC | 129 ms
29,680 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 2 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 194 ms
22,020 KB |
testcase_21 | AC | 197 ms
22,240 KB |
testcase_22 | AC | 189 ms
22,156 KB |
testcase_23 | AC | 178 ms
22,076 KB |
testcase_24 | AC | 187 ms
21,940 KB |
testcase_25 | AC | 183 ms
22,056 KB |
testcase_26 | AC | 173 ms
22,216 KB |
testcase_27 | AC | 185 ms
21,972 KB |
testcase_28 | AC | 173 ms
22,044 KB |
testcase_29 | AC | 180 ms
22,136 KB |
testcase_30 | AC | 179 ms
21,936 KB |
testcase_31 | AC | 183 ms
21,984 KB |
testcase_32 | AC | 183 ms
22,032 KB |
testcase_33 | AC | 179 ms
22,020 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; template<typename T> struct edge{ int from, to; T cost; edge(){} edge(int to, T cost = 1) : from(-1), to(to), cost(cost){} edge(int from, int to, T cost) : from(from), to(to), cost(cost){} }; template<typename T> struct redge{ int from, to; T cap, cost; int rev; redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){} redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){} }; template<typename T> using Edges = vector<edge<T>>; template<typename T> using weighted_graph = vector<Edges<T>>; template<typename T> using tree = vector<Edges<T>>; using unweighted_graph = vector<vector<int>>; template<typename T> using residual_graph = vector<vector<redge<T>>>; template<typename T> struct namori{ int n; vector<int> root; //root[閉路上の頂点]=自身の頂点, root[閉路外の頂点]=根頂点 vector<int> par; //par[閉路上の頂点]=-1, par[閉路外の頂点]=親頂点 namori(weighted_graph<T> &graph){ n = (int)graph.size(); root.resize(n, 0); for(int v=0; v<n; v++) root[v]=v; par.resize(n, -1); init(graph); } void init(weighted_graph<T> &graph){ //閉路上の頂点を決定 vector<int> deg(n, 0); for(int v=0; v<n; v++) deg[v] = (int)graph[v].size(); queue<int> Q; for(int v=0; v<n; v++) if(deg[v]==1) Q.push(v); while(!Q.empty()){ int v = Q.front(); Q.pop(); root[v] = -1; for(edge<T> e : graph[v]){ deg[e.to]--; if(deg[e.to]==1) Q.push(e.to); } } function<void(int, int)> dfs = [&](int v, int p){ root[v] = root[p]; par[v] = p; for(edge<T> e : graph[v]) if(e.to != p) dfs(e.to, v); }; for(ll r=0; r<n; r++){ if(root[r]!=r) continue; for(edge<T> e : graph[r]){ if(root[e.to]==e.to) continue; dfs(e.to, r); } } } int get_root(int v){return root[v];} int get_par(int v){return par[v];} }; template<long long mod> class modint{ long long x; public: modint(long long x=0) : x((x%mod+mod)%mod) {} modint operator-() const { return modint(-x); } bool operator==(const modint& a){ if(x == a) return true; else return false; } bool operator==(long long a){ if(x == a) return true; else return false; } bool operator!=(const modint& a){ if(x != a) return true; else return false; } bool operator!=(long long a){ if(x != a) return true; else return false; } modint& operator+=(const modint& a) { if ((x += a.x) >= mod) x -= mod; return *this; } modint& operator-=(const modint& a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } modint& operator*=(const modint& a) { (x *= a.x) %= mod; return *this; } modint operator+(const modint& a) const { modint res(*this); return res+=a; } modint operator-(const modint& a) const { modint res(*this); return res-=a; } modint operator*(const modint& a) const { modint res(*this); return res*=a; } modint pow(long long t) const { if (!t) return 1; modint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod modint inv() const { return pow(mod-2); } modint& operator/=(const modint& a) { return (*this) *= a.inv(); } modint operator/(const modint& a) const { modint res(*this); return res/=a; } friend std::istream& operator>>(std::istream& is, modint& m) noexcept { is >> m.x; m.x %= mod; if (m.x < 0) m.x += mod; return is; } friend ostream& operator<<(ostream& os, const modint& m){ os << m.x; return os; } }; using mint = modint<MOD998244353>; void solve(){ ll n, k; cin >> n >> k; weighted_graph<ll> G(n); rep(i, n){ ll u, v; cin >> u >> v; u--; v--; G[u].pb(edge<ll>(v)); G[v].pb(edge<ll>(u)); } namori<ll> nam(G); mint cans = 1; //サイクルの処理 { ll siz = 0; for(ll i=0; i<n; i++) if(nam.get_par(i)==-1) siz++; vector<vector<mint>> dp(siz, vector<mint>(2, 0)); dp[0][1] = k; for(ll i=1; i<siz; i++){ dp[i][0] = dp[i-1][0]*(k-2) + dp[i-1][1]*(k-1); dp[i][1] = dp[i-1][0]; } cans = dp[siz-1][0]; } vector<ll> siz(n, 1); function<void(ll, ll)> dfs = [&](ll v, ll p){ for(edge<ll> e : G[v]) if(e.to!=p){ dfs(e.to, v); siz[v]+=siz[e.to]; } }; mint sa = 1; for(ll i=0; i<n; i++) if(nam.get_par(i)==-1){ for(edge<ll> e : G[i]){ if(nam.get_par(e.to)==-1) continue; dfs(e.to, i); siz[i]+=siz[e.to]; } cans *= mint(k-1).pow(siz[i]-1); } cout << cans << endl; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }