結果

問題 No.2531 Coloring Vertices on Namori
ユーザー umimelumimel
提出日時 2023-11-03 22:15:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 5,900 bytes
コンパイル時間 2,066 ms
コンパイル使用メモリ 188,832 KB
実行使用メモリ 30,328 KB
最終ジャッジ日時 2023-11-03 22:15:37
合計ジャッジ時間 8,033 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 85 ms
29,836 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 85 ms
29,836 KB
testcase_08 AC 160 ms
30,328 KB
testcase_09 AC 154 ms
30,328 KB
testcase_10 AC 158 ms
30,328 KB
testcase_11 AC 59 ms
21,504 KB
testcase_12 AC 59 ms
21,504 KB
testcase_13 AC 59 ms
21,504 KB
testcase_14 AC 134 ms
29,836 KB
testcase_15 AC 135 ms
29,836 KB
testcase_16 AC 140 ms
29,836 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 214 ms
22,224 KB
testcase_21 AC 198 ms
22,048 KB
testcase_22 AC 199 ms
22,036 KB
testcase_23 AC 200 ms
22,012 KB
testcase_24 AC 207 ms
22,004 KB
testcase_25 AC 221 ms
21,992 KB
testcase_26 AC 208 ms
22,024 KB
testcase_27 AC 206 ms
21,912 KB
testcase_28 AC 209 ms
21,980 KB
testcase_29 AC 206 ms
22,208 KB
testcase_30 AC 201 ms
21,872 KB
testcase_31 AC 253 ms
21,924 KB
testcase_32 AC 206 ms
21,968 KB
testcase_33 AC 209 ms
21,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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();
}
0