結果

問題 No.1207 グラフX
ユーザー tomatoma
提出日時 2020-08-30 15:24:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 408 ms / 2,000 ms
コード長 4,045 bytes
コンパイル時間 2,760 ms
コンパイル使用メモリ 216,636 KB
実行使用メモリ 41,536 KB
最終ジャッジ日時 2024-11-15 08:55:29
合計ジャッジ時間 18,351 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 325 ms
24,236 KB
testcase_01 AC 336 ms
23,724 KB
testcase_02 AC 326 ms
23,704 KB
testcase_03 AC 329 ms
23,572 KB
testcase_04 AC 339 ms
24,616 KB
testcase_05 AC 406 ms
41,536 KB
testcase_06 AC 407 ms
40,388 KB
testcase_07 AC 408 ms
40,236 KB
testcase_08 AC 273 ms
15,932 KB
testcase_09 AC 284 ms
21,048 KB
testcase_10 AC 391 ms
33,788 KB
testcase_11 AC 400 ms
40,508 KB
testcase_12 AC 267 ms
20,640 KB
testcase_13 AC 171 ms
6,816 KB
testcase_14 AC 315 ms
22,992 KB
testcase_15 AC 283 ms
20,496 KB
testcase_16 AC 162 ms
7,928 KB
testcase_17 AC 225 ms
14,796 KB
testcase_18 AC 175 ms
14,768 KB
testcase_19 AC 234 ms
11,796 KB
testcase_20 AC 333 ms
23,172 KB
testcase_21 AC 31 ms
6,816 KB
testcase_22 AC 217 ms
14,948 KB
testcase_23 AC 237 ms
20,988 KB
testcase_24 AC 158 ms
13,588 KB
testcase_25 AC 323 ms
23,044 KB
testcase_26 AC 254 ms
21,088 KB
testcase_27 AC 313 ms
22,584 KB
testcase_28 AC 292 ms
21,336 KB
testcase_29 AC 290 ms
22,664 KB
testcase_30 AC 150 ms
11,788 KB
testcase_31 AC 148 ms
6,820 KB
testcase_32 AC 120 ms
11,596 KB
testcase_33 AC 130 ms
11,648 KB
testcase_34 AC 268 ms
20,408 KB
testcase_35 AC 32 ms
6,816 KB
testcase_36 AC 268 ms
21,372 KB
testcase_37 AC 237 ms
15,544 KB
testcase_38 AC 62 ms
6,816 KB
testcase_39 AC 137 ms
12,252 KB
testcase_40 AC 123 ms
6,820 KB
testcase_41 AC 210 ms
12,488 KB
testcase_42 AC 2 ms
6,816 KB
testcase_43 AC 2 ms
6,820 KB
testcase_44 AC 2 ms
6,816 KB
testcase_45 AC 301 ms
23,840 KB
testcase_46 AC 297 ms
23,836 KB
testcase_47 AC 302 ms
24,568 KB
testcase_48 AC 298 ms
24,244 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