結果

問題 No.1207 グラフX
ユーザー tomatoma
提出日時 2020-08-30 15:24:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 391 ms / 2,000 ms
コード長 4,045 bytes
コンパイル時間 2,579 ms
コンパイル使用メモリ 214,240 KB
実行使用メモリ 41,028 KB
最終ジャッジ日時 2023-08-09 13:14:33
合計ジャッジ時間 17,306 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 316 ms
24,616 KB
testcase_01 AC 318 ms
23,564 KB
testcase_02 AC 303 ms
23,904 KB
testcase_03 AC 312 ms
23,752 KB
testcase_04 AC 315 ms
23,700 KB
testcase_05 AC 391 ms
40,256 KB
testcase_06 AC 375 ms
40,860 KB
testcase_07 AC 383 ms
41,028 KB
testcase_08 AC 256 ms
15,764 KB
testcase_09 AC 260 ms
21,496 KB
testcase_10 AC 377 ms
32,944 KB
testcase_11 AC 387 ms
40,184 KB
testcase_12 AC 246 ms
20,496 KB
testcase_13 AC 156 ms
6,444 KB
testcase_14 AC 302 ms
22,956 KB
testcase_15 AC 268 ms
21,316 KB
testcase_16 AC 149 ms
7,636 KB
testcase_17 AC 205 ms
14,524 KB
testcase_18 AC 163 ms
14,708 KB
testcase_19 AC 212 ms
11,492 KB
testcase_20 AC 315 ms
22,964 KB
testcase_21 AC 28 ms
4,384 KB
testcase_22 AC 202 ms
14,732 KB
testcase_23 AC 221 ms
20,656 KB
testcase_24 AC 151 ms
13,576 KB
testcase_25 AC 306 ms
22,840 KB
testcase_26 AC 235 ms
20,688 KB
testcase_27 AC 279 ms
21,988 KB
testcase_28 AC 267 ms
21,280 KB
testcase_29 AC 264 ms
22,128 KB
testcase_30 AC 142 ms
11,644 KB
testcase_31 AC 135 ms
5,368 KB
testcase_32 AC 117 ms
11,540 KB
testcase_33 AC 124 ms
11,476 KB
testcase_34 AC 254 ms
21,392 KB
testcase_35 AC 29 ms
4,384 KB
testcase_36 AC 250 ms
21,472 KB
testcase_37 AC 217 ms
15,172 KB
testcase_38 AC 56 ms
6,048 KB
testcase_39 AC 127 ms
11,948 KB
testcase_40 AC 117 ms
4,380 KB
testcase_41 AC 197 ms
11,804 KB
testcase_42 AC 2 ms
4,384 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 289 ms
24,140 KB
testcase_46 AC 276 ms
24,780 KB
testcase_47 AC 285 ms
24,536 KB
testcase_48 AC 282 ms
24,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using tp3 = tuple<ll, ll, ll>;
using Mat = vector<vector<ll>>;
constexpr int INF = 1 << 28;
constexpr ll INFL = 1ll << 60;
constexpr int dh[4] = { 0,1,0,-1 };
constexpr int dw[4] = { -1,0,1,0 };
bool isin(const int H, const int W, const int h, const int w) {
    return 0 <= h && h < H && 0 <= w && w < W;
}
class UnionFind {
public:
    vector<int>rank, parent;
    //初期化
    UnionFind(int size) {
        rank.resize(size, 0);
        parent.resize(size, 0);
        rep(i, size)parent[i] = i;
    }
    //木の根を求める
    int find(int x) {
        if (parent[x] == x)return x;
        else return parent[x] = find(parent[x]);
    }
    //xとyの属する集合を併合
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)return;
        if (rank[x] < rank[y])
            parent[x] = y;
        else {
            parent[y] = x;
            if (rank[x] == rank[y])rank[x]++;
        }
    }
    //xとyが同じ集合に属するか否か
    bool same(int x, int y) {
        return (find(x) == find(y));
    }
};
struct ModInt {
    static const ll MOD = 1000000007;

    // constructors etc
    ModInt() :num(1ll) {}
    ModInt(ll num_) :num(num_%MOD) {}
    ModInt(const ModInt& modint) :num(modint.num%MOD) {}
    ll get()const { return num; }

    // operator etc
    // operator ll() const { return num; }
    // ll operator*() { return num; }
    ModInt& operator+=(const ModInt& r) { (num += r.num) %= MOD; return *this; }
    ModInt& operator-=(const ModInt& r) { (num += -r.num + MOD) %= MOD; return *this; }
    ModInt& operator*=(const ModInt& r) { (num *= r.num) %= MOD; return *this; }
    ModInt& operator/=(const ModInt& r) { (num *= r.inv().num) %= MOD; return *this; }
    ModInt pow(const ModInt& r)const {
        ll res = 1;
        ll x = num;
        ll n = r.num;
        while (n > 0) {
            if (n & 1)res = (res*x) % MOD;
            x = (x*x) % MOD;
            n >>= 1;
        }
        return res;
    }
    ModInt inv()const { return this->pow(MOD - 2); }

    ModInt operator+(const ModInt& r)const { return ModInt(*this) += r; }
    ModInt operator-(const ModInt& r)const { return ModInt(*this) -= r; }
    ModInt operator*(const ModInt& r)const { return ModInt(*this) *= r; }
    ModInt operator/(const ModInt& r)const { return ModInt(*this) /= r; }
    ModInt operator+(const ll& r)const { return *this + ModInt(r); }
    ModInt operator-(const ll& r)const { return *this - ModInt(r); }
    ModInt operator*(const ll& r)const { return *this * ModInt(r); }
    ModInt operator/(const ll& r)const { return *this / ModInt(r); }

private:
    ll num;
};
ostream& operator<<(ostream& stream, const ModInt& val) { stream << val.get(); return stream; }

// ============ template finished ============

int dfs(const vector<vector<pll>>& edges, vector<ll>& childs, const int now, const int par) {
    childs[now] = 1;
    for (auto[nextIdx, _] : edges[now])if (nextIdx != par) {
        childs[now] += dfs(edges, childs, nextIdx, now);
    }
    return childs[now];
}

int main()
{
    ll N, M, X;
    cin >> N >> M >> X;
    vector<vector<pll>> edges(N);
    vector<tp3> pairs;

    {
        UnionFind uf(N);
        rep(i, M) {
            ll x, y, z;
            cin >> x >> y >> z;
            x--; y--;
            if (uf.same(x, y))continue;
            uf.unite(x, y);
            edges[x].push_back({ y,z });
            edges[y].push_back({ x,z });
            pairs.push_back(make_tuple(x, y, z));
        }
    }
    vector<ll> childs(N, 0);
    dfs(edges, childs, 0, -1);

    ModInt res(0);
    for (auto[x, y, z] : pairs) {
        ll small = min(childs[x], childs[y]);
        auto way = ModInt(small) * (N - small);
        auto value = ModInt(X).pow(z);
        res += value * way;
    }
    cout << res << endl;
    return 0;
}
0