結果

問題 No.1207 グラフX
ユーザー carrot46carrot46
提出日時 2020-08-30 13:19:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 368 ms / 2,000 ms
コード長 3,843 bytes
コンパイル時間 1,729 ms
コンパイル使用メモリ 176,984 KB
実行使用メモリ 37,700 KB
最終ジャッジ日時 2024-04-27 06:49:22
合計ジャッジ時間 15,844 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 302 ms
20,580 KB
testcase_01 AC 308 ms
20,456 KB
testcase_02 AC 304 ms
20,472 KB
testcase_03 AC 304 ms
20,452 KB
testcase_04 AC 306 ms
20,448 KB
testcase_05 AC 368 ms
37,700 KB
testcase_06 AC 360 ms
37,608 KB
testcase_07 AC 367 ms
37,592 KB
testcase_08 AC 274 ms
13,324 KB
testcase_09 AC 273 ms
16,304 KB
testcase_10 AC 360 ms
29,076 KB
testcase_11 AC 367 ms
37,684 KB
testcase_12 AC 266 ms
14,392 KB
testcase_13 AC 172 ms
6,944 KB
testcase_14 AC 297 ms
19,596 KB
testcase_15 AC 275 ms
15,724 KB
testcase_16 AC 162 ms
6,940 KB
testcase_17 AC 219 ms
12,544 KB
testcase_18 AC 171 ms
13,116 KB
testcase_19 AC 237 ms
9,344 KB
testcase_20 AC 306 ms
20,076 KB
testcase_21 AC 32 ms
6,940 KB
testcase_22 AC 214 ms
12,832 KB
testcase_23 AC 228 ms
14,436 KB
testcase_24 AC 148 ms
11,976 KB
testcase_25 AC 296 ms
19,872 KB
testcase_26 AC 243 ms
15,376 KB
testcase_27 AC 288 ms
17,912 KB
testcase_28 AC 278 ms
16,372 KB
testcase_29 AC 265 ms
17,744 KB
testcase_30 AC 152 ms
8,832 KB
testcase_31 AC 149 ms
6,940 KB
testcase_32 AC 120 ms
9,472 KB
testcase_33 AC 135 ms
9,216 KB
testcase_34 AC 269 ms
15,524 KB
testcase_35 AC 32 ms
6,940 KB
testcase_36 AC 258 ms
16,200 KB
testcase_37 AC 239 ms
13,212 KB
testcase_38 AC 62 ms
6,944 KB
testcase_39 AC 137 ms
9,748 KB
testcase_40 AC 128 ms
6,944 KB
testcase_41 AC 210 ms
9,344 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 2 ms
6,940 KB
testcase_45 AC 278 ms
20,440 KB
testcase_46 AC 272 ms
20,432 KB
testcase_47 AC 277 ms
20,444 KB
testcase_48 AC 281 ms
20,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<58);

template <unsigned long long mod > class modint{
public:
    ll x;
    constexpr modint(){x = 0;}
    constexpr modint(ll _x) : x((_x < 0 ? ((_x += (LLONG_MAX / mod) * mod) < 0 ? _x + (LLONG_MAX / mod) * mod : _x) : _x)%mod){}
    constexpr modint operator-(){
        return x == 0 ? 0 : mod - x;
    }
    constexpr modint& operator+=(const modint& a){
        if((x += a.x) >= mod) x -= mod;
        return *this;
    }
    constexpr modint operator+(const modint& a) const{
        return modint(*this) += a;
    }
    constexpr modint& operator-=(const modint& a){
        if((x -= a.x) < 0) x += mod;
        return *this;
    }
    constexpr modint operator-(const modint& a) const{
        return modint(*this) -= a;
    }
    constexpr modint& operator*=(const modint& a){
        (x *= a.x)%=mod;
        return *this;
    }
    constexpr modint operator*(const modint& a) const{
        return modint(*this) *= a;
    }
    constexpr modint pow(unsigned long long pw) const{
        modint res(1), comp(*this);
        while(pw){
            if(pw&1) res *= comp;
            comp *= comp;
            pw >>= 1;
        }
        return res;
    }
    //以下、modが素数のときのみ
    constexpr modint inv() const{
        return modint(*this).pow(mod - 2);
    }
    constexpr modint& operator/=(const modint &a){
        (x *= a.inv().x)%=mod;
        return *this;
    }
    constexpr modint operator/(const modint &a) const{
        return modint(*this) /= a;
    }
};
#define mod1 998244353
#define mod2 1000000007
using mint = modint<mod2>;

ostream& operator<<(ostream& os, const mint& a){
    os << a.x;
    return os;
}
using vm = vector<mint>;

struct edge{
    int to, cost;
    edge(){}
    edge(int a, int c){
        to = a;
        cost = c;
    }
};
using ve = vector<edge>;

void dfs(int v, mint &ans, vector<ve> &G, vec &size){
    size[v] = 1;
    for(edge e : G[v]){
        if(size[e.to] == -1){
            dfs(e.to, ans, G, size);
            ans += mint(K).pow(e.cost) * size[e.to] * (N - size[e.to]);
            size[v] += size[e.to];
        }
    }
}

int main() {
    class union_find{
            vector<long> parent, tree_size;
        public:
            union_find(unsigned long _n) : parent(_n), tree_size(_n, 1){
                for(unsigned long i = 0; i < _n; ++i)parent[i] = i;
            }
            ll find(ll x){
                while(parent[x] != x)x = parent[x] = parent[parent[x]];
                return x;
            }
            bool merge(ll a, ll b){
                a = find(a);
                b = find(b);
    	        if(a==b) return false;
                if(tree_size[a] < tree_size[b])swap(a, b);
                tree_size[a] += tree_size[b];
                parent[b] = a;
                return true;
            }
    	bool same(ll a, ll b){
    	    a = find(a);
    	    b = find(b);
    	    return a == b;
    	}
        };
    //uf.mergeのように使う
    //ufはMAX_N+1でとるか、代入時に0-indexedにする
    cin>>N>>M>>K;
    union_find uf(N + 10);
    vector<ve> G(N, ve(0));
    rep(i, M){
        int x, y, z;
        cin>>x>>y>>z;
        --x; --y;
        if(uf.merge(x, y)){
            G[x].emplace_back(y, z);
            G[y].emplace_back(x, z);
        }
    }
    mint ans(0);
    vec size(N, -1);
    dfs(0, ans, G, size);
    cout<<ans<<endl;
}
0