結果

問題 No.3079 Unite Japanese Prefectures
ユーザー Iroha_3856
提出日時 2025-03-19 17:11:33
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 2,341 bytes
コンパイル時間 3,634 ms
コンパイル使用メモリ 300,196 KB
実行使用メモリ 358,656 KB
最終ジャッジ日時 2025-03-19 17:11:45
合計ジャッジ時間 10,913 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 TLE * 1 -- * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/dsu>

#define rep(i, l, r) for (int i = (int)(l); i<(int)(r); i++)
#define ll long long

struct PolynomialHash {
    static const int P = 31;
    static const int MOD = 1'000'000'007;
    ll power[6];
    PolynomialHash() {
        power[0] = 1;
        rep(i, 1, 6) power[i] = power[i-1]*P;
    }

    size_t operator()(const array<int, 6>& seq) const {
        ll ret = 0;
        rep(i, 0, 6) {
            ret += power[i]*seq[i];
            ret %= MOD;
        }
        return ret;
    }
};
struct ArrayEqual {
    bool operator()(const array<int, 6>& a, const array<int, 6>& b) const {
        return a == b;
    }
};

int main() {
    int N, M; cin >> N >> M;

    vector<tuple<int, int, int>> Edges(M);
    rep(i, 0, M) {
        int u, v, c; cin >> u >> v >> c;
        u--; v--; c--;
        Edges[i] = {c, u, v};
    }

    array<int, 6> cnt = {0, 0, 0, 0, 0, 0};
    atcoder::dsu d(N);

    sort(Edges.begin(), Edges.end());
    for (int i = 0; i < M; i++) {
        auto[c, u, v] = Edges[i];
        if (!d.same(u, v)) {
            d.merge(u, v);
            cnt[c]++;
        }
    }

    //累積
    for (int i = 4; i >= 0; i--) cnt[i] += cnt[i+1];

    //dp[t] : 今まででたのがtとなるようにするまでにかかるコストの期待値
    unordered_map<array<int, 6>, double, PolynomialHash, ArrayEqual> dp;
    dp[cnt] = 0;
    //6, 5, 4, 4, 3, 2 -> 7, 6, 4, 4, 3, 2 : 辞書順で後にいく
    array<int, 6> A;
    for (A[0] = cnt[0]; A[0] >= 0; A[0]--) for (A[1] = min(A[0], cnt[1]); A[1] >= 0; A[1]--) for (A[2] = min(A[1], cnt[2]); A[2] >= 0; A[2]--) {
        for (A[3] = min(A[2], cnt[3]); A[3] >= 0; A[3]--) for (A[4] = min(A[3], cnt[4]); A[4] >= 0; A[4]--) for (A[5] = min(A[4], cnt[5]); A[5] >= 0; A[5]--) {

            double t = 0;
            int loop = 0;
            array<int, 6> B = A;
            bool f = false;
            rep(i, 0, 6) {
            	if (B[i] != cnt[i]) {
                    f = true;
            	    B[i]++;
                }
            	if (f) t += dp[B];
                else loop++;
            }
            if (loop == 6) continue;
            t += 6;
            t /= (6 - loop);
            dp[A] = t;
        }
    }
    cout << fixed << setprecision(15) << dp[{0, 0, 0, 0, 0, 0}] << endl;
}
0