結果

問題 No.1207 グラフX
ユーザー tomatoma
提出日時 2020-08-30 15:24:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 401 ms / 2,000 ms
コード長 4,045 bytes
コンパイル時間 2,565 ms
コンパイル使用メモリ 217,420 KB
実行使用メモリ 40,364 KB
最終ジャッジ日時 2024-04-27 08:13:40
合計ジャッジ時間 16,947 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 342 ms
23,576 KB
testcase_01 AC 338 ms
23,496 KB
testcase_02 AC 323 ms
23,700 KB
testcase_03 AC 322 ms
23,700 KB
testcase_04 AC 330 ms
23,820 KB
testcase_05 AC 398 ms
40,364 KB
testcase_06 AC 401 ms
40,232 KB
testcase_07 AC 394 ms
40,236 KB
testcase_08 AC 274 ms
15,928 KB
testcase_09 AC 286 ms
20,780 KB
testcase_10 AC 393 ms
33,068 KB
testcase_11 AC 396 ms
40,240 KB
testcase_12 AC 266 ms
19,792 KB
testcase_13 AC 173 ms
6,832 KB
testcase_14 AC 315 ms
22,852 KB
testcase_15 AC 282 ms
20,148 KB
testcase_16 AC 160 ms
7,928 KB
testcase_17 AC 220 ms
14,860 KB
testcase_18 AC 175 ms
14,904 KB
testcase_19 AC 235 ms
11,796 KB
testcase_20 AC 332 ms
23,344 KB
testcase_21 AC 31 ms
5,376 KB
testcase_22 AC 211 ms
14,816 KB
testcase_23 AC 232 ms
19,732 KB
testcase_24 AC 151 ms
13,696 KB
testcase_25 AC 320 ms
23,180 KB
testcase_26 AC 251 ms
20,088 KB
testcase_27 AC 303 ms
21,904 KB
testcase_28 AC 289 ms
20,956 KB
testcase_29 AC 285 ms
21,688 KB
testcase_30 AC 154 ms
11,668 KB
testcase_31 AC 148 ms
5,556 KB
testcase_32 AC 120 ms
11,640 KB
testcase_33 AC 135 ms
11,652 KB
testcase_34 AC 267 ms
20,192 KB
testcase_35 AC 32 ms
5,376 KB
testcase_36 AC 262 ms
20,600 KB
testcase_37 AC 236 ms
15,400 KB
testcase_38 AC 63 ms
6,580 KB
testcase_39 AC 137 ms
12,324 KB
testcase_40 AC 124 ms
5,376 KB
testcase_41 AC 216 ms
11,848 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 295 ms
23,696 KB
testcase_46 AC 295 ms
23,572 KB
testcase_47 AC 291 ms
23,568 KB
testcase_48 AC 294 ms
23,564 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