結果

問題 No.1207 グラフX
ユーザー carrot46carrot46
提出日時 2020-08-30 13:19:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 355 ms / 2,000 ms
コード長 3,843 bytes
コンパイル時間 1,698 ms
コンパイル使用メモリ 174,560 KB
実行使用メモリ 37,536 KB
最終ジャッジ日時 2023-08-09 11:42:31
合計ジャッジ時間 15,702 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 287 ms
20,396 KB
testcase_01 AC 288 ms
20,260 KB
testcase_02 AC 288 ms
20,308 KB
testcase_03 AC 286 ms
20,312 KB
testcase_04 AC 291 ms
20,312 KB
testcase_05 AC 355 ms
37,312 KB
testcase_06 AC 350 ms
37,332 KB
testcase_07 AC 345 ms
37,396 KB
testcase_08 AC 254 ms
13,320 KB
testcase_09 AC 255 ms
15,936 KB
testcase_10 AC 340 ms
28,876 KB
testcase_11 AC 351 ms
37,536 KB
testcase_12 AC 246 ms
14,356 KB
testcase_13 AC 153 ms
5,752 KB
testcase_14 AC 283 ms
19,304 KB
testcase_15 AC 257 ms
15,636 KB
testcase_16 AC 147 ms
6,716 KB
testcase_17 AC 203 ms
12,380 KB
testcase_18 AC 159 ms
12,916 KB
testcase_19 AC 216 ms
9,224 KB
testcase_20 AC 284 ms
19,844 KB
testcase_21 AC 29 ms
4,376 KB
testcase_22 AC 192 ms
12,912 KB
testcase_23 AC 211 ms
14,256 KB
testcase_24 AC 140 ms
11,452 KB
testcase_25 AC 280 ms
19,912 KB
testcase_26 AC 226 ms
15,008 KB
testcase_27 AC 269 ms
17,712 KB
testcase_28 AC 253 ms
16,216 KB
testcase_29 AC 243 ms
17,612 KB
testcase_30 AC 137 ms
8,932 KB
testcase_31 AC 135 ms
4,848 KB
testcase_32 AC 107 ms
9,244 KB
testcase_33 AC 117 ms
8,844 KB
testcase_34 AC 246 ms
15,420 KB
testcase_35 AC 29 ms
4,376 KB
testcase_36 AC 240 ms
15,996 KB
testcase_37 AC 217 ms
12,984 KB
testcase_38 AC 57 ms
5,804 KB
testcase_39 AC 128 ms
9,556 KB
testcase_40 AC 115 ms
4,380 KB
testcase_41 AC 187 ms
9,156 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 258 ms
20,244 KB
testcase_46 AC 264 ms
20,360 KB
testcase_47 AC 260 ms
20,224 KB
testcase_48 AC 254 ms
20,372 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