結果
問題 | No.1207 グラフX |
ユーザー | Example0911 |
提出日時 | 2020-09-01 19:06:18 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 590 ms / 2,000 ms |
コード長 | 3,140 bytes |
コンパイル時間 | 2,197 ms |
コンパイル使用メモリ | 188,344 KB |
実行使用メモリ | 49,572 KB |
最終ジャッジ日時 | 2024-04-30 15:21:00 |
合計ジャッジ時間 | 24,381 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 531 ms
31,388 KB |
testcase_01 | AC | 534 ms
31,264 KB |
testcase_02 | AC | 513 ms
31,420 KB |
testcase_03 | AC | 518 ms
31,400 KB |
testcase_04 | AC | 514 ms
31,392 KB |
testcase_05 | AC | 586 ms
49,564 KB |
testcase_06 | AC | 582 ms
49,568 KB |
testcase_07 | AC | 590 ms
49,572 KB |
testcase_08 | AC | 350 ms
23,640 KB |
testcase_09 | AC | 416 ms
27,016 KB |
testcase_10 | AC | 581 ms
40,868 KB |
testcase_11 | AC | 590 ms
49,572 KB |
testcase_12 | AC | 367 ms
25,096 KB |
testcase_13 | AC | 130 ms
13,852 KB |
testcase_14 | AC | 493 ms
30,352 KB |
testcase_15 | AC | 402 ms
25,920 KB |
testcase_16 | AC | 147 ms
14,004 KB |
testcase_17 | AC | 300 ms
20,472 KB |
testcase_18 | AC | 283 ms
19,212 KB |
testcase_19 | AC | 234 ms
19,412 KB |
testcase_20 | AC | 499 ms
30,872 KB |
testcase_21 | AC | 16 ms
6,944 KB |
testcase_22 | AC | 304 ms
20,828 KB |
testcase_23 | AC | 343 ms
23,756 KB |
testcase_24 | AC | 247 ms
17,324 KB |
testcase_25 | AC | 501 ms
30,552 KB |
testcase_26 | AC | 373 ms
24,668 KB |
testcase_27 | AC | 450 ms
28,188 KB |
testcase_28 | AC | 417 ms
27,388 KB |
testcase_29 | AC | 440 ms
27,372 KB |
testcase_30 | AC | 190 ms
15,360 KB |
testcase_31 | AC | 97 ms
12,232 KB |
testcase_32 | AC | 185 ms
14,136 KB |
testcase_33 | AC | 188 ms
14,504 KB |
testcase_34 | AC | 390 ms
25,668 KB |
testcase_35 | AC | 19 ms
6,940 KB |
testcase_36 | AC | 398 ms
26,360 KB |
testcase_37 | AC | 319 ms
22,052 KB |
testcase_38 | AC | 79 ms
8,448 KB |
testcase_39 | AC | 199 ms
15,396 KB |
testcase_40 | AC | 58 ms
10,332 KB |
testcase_41 | AC | 226 ms
18,456 KB |
testcase_42 | AC | 2 ms
6,940 KB |
testcase_43 | AC | 2 ms
6,944 KB |
testcase_44 | AC | 2 ms
6,940 KB |
testcase_45 | AC | 519 ms
31,324 KB |
testcase_46 | AC | 501 ms
31,192 KB |
testcase_47 | AC | 499 ms
31,412 KB |
testcase_48 | AC | 501 ms
31,324 KB |
ソースコード
#include "bits/stdc++.h" using namespace std; //#define int long long #define ll long long #define rep(i,n) for(ll i = 0; i < (n); i++) #define P pair<ll,ll> #define ld long double ll INF = (1LL << 60); int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) :x((x%mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; istream& operator>>(istream& is, mint& a) { return is >> a.x; } ostream& operator<<(ostream& os, const mint& a) { return os << a.x; } struct UnionFind { vector<ll>p; UnionFind() {} UnionFind(ll n) { p.resize(n, -1); } ll find(ll x) { if (p[x] == -1)return x; else return p[x] = find(p[x]); } void unite(ll x, ll y) { x = find(x), y = find(y); if (x == y)return; p[x] = y; } }; struct Edge { ll a, b, cost; }; bool comp_e(const Edge &e1, const Edge &e2) { return e1.cost < e2.cost; } vector<vector<P>>AfterGraph; struct Kruskal { UnionFind uf; ll sum; vector<Edge>edges; ll n; Kruskal(const vector<Edge> &edges_, ll n_) : edges(edges_), n(n_) { sort(edges.begin(), edges.end(), comp_e); uf = UnionFind(n); sum = 0; vector<ll> costs; for (auto e : edges) { if (uf.find(e.a) != uf.find(e.b)) { uf.unite(e.a, e.b); AfterGraph[e.a].push_back({ e.b, e.cost }); AfterGraph[e.b].push_back({ e.a,e.cost }); costs.push_back(e.cost); sum += e.cost; } } } }; vector<ll>subtree_size; void dfs(const vector<vector<P>> &G, ll v, ll p, ll now) { for (auto nv : G[v]) { if (nv.first == p)continue; dfs(G, nv.first, v, now + 1); } subtree_size[v] = 1; for (auto c : G[v]) { if (c.first == p)continue; subtree_size[v] += subtree_size[c.first]; } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N, M, X; cin >> N >> M >> X; AfterGraph.resize(N); subtree_size.resize(N); vector<Edge>G(M); rep(i, M) { ll x, y, z; cin >> x >> y >> z; x--; y--; Edge e = { x,y,z }; G[i] = e; } Kruskal tmp(G, N); dfs(AfterGraph, 0, -1, 0); mint ans = 0; rep(i, N) { for (auto v : AfterGraph[i]) { mint now = mint(X).pow(v.second); ll idx = 0; if (subtree_size[i] > subtree_size[v.first]) { idx = v.first; } else idx = i; now *= subtree_size[idx]; now *= (N - subtree_size[idx]); ans += now; } } ans /= 2; cout << ans << endl; return 0; }