結果
問題 | No.1207 グラフX |
ユーザー | siman |
提出日時 | 2023-03-15 20:35:46 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,632 bytes |
コンパイル時間 | 1,581 ms |
コンパイル使用メモリ | 117,912 KB |
実行使用メモリ | 34,776 KB |
最終ジャッジ日時 | 2023-10-18 12:31:24 |
合計ジャッジ時間 | 18,392 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 317 ms
25,008 KB |
testcase_01 | AC | 320 ms
25,008 KB |
testcase_02 | AC | 320 ms
25,008 KB |
testcase_03 | AC | 318 ms
25,008 KB |
testcase_04 | AC | 319 ms
25,008 KB |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | AC | 274 ms
19,500 KB |
testcase_09 | AC | 278 ms
23,196 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | AC | 270 ms
22,668 KB |
testcase_13 | AC | 188 ms
13,428 KB |
testcase_14 | AC | 315 ms
24,480 KB |
testcase_15 | AC | 280 ms
23,196 KB |
testcase_16 | AC | 171 ms
14,220 KB |
testcase_17 | AC | 215 ms
18,444 KB |
testcase_18 | AC | 171 ms
17,948 KB |
testcase_19 | AC | 244 ms
17,388 KB |
testcase_20 | AC | 314 ms
24,744 KB |
testcase_21 | AC | 39 ms
10,052 KB |
testcase_22 | AC | 219 ms
18,444 KB |
testcase_23 | AC | 237 ms
22,140 KB |
testcase_24 | AC | 152 ms
17,156 KB |
testcase_25 | AC | 323 ms
24,744 KB |
testcase_26 | AC | 249 ms
22,668 KB |
testcase_27 | AC | 295 ms
23,688 KB |
testcase_28 | AC | 284 ms
23,196 KB |
testcase_29 | AC | 280 ms
23,424 KB |
testcase_30 | AC | 157 ms
16,100 KB |
testcase_31 | AC | 165 ms
13,196 KB |
testcase_32 | AC | 126 ms
15,836 KB |
testcase_33 | AC | 137 ms
16,100 KB |
testcase_34 | AC | 277 ms
22,932 KB |
testcase_35 | AC | 38 ms
10,052 KB |
testcase_36 | AC | 266 ms
22,932 KB |
testcase_37 | AC | 241 ms
18,972 KB |
testcase_38 | AC | 68 ms
11,628 KB |
testcase_39 | AC | 141 ms
16,100 KB |
testcase_40 | AC | 147 ms
13,196 KB |
testcase_41 | AC | 227 ms
17,124 KB |
testcase_42 | AC | 5 ms
9,336 KB |
testcase_43 | AC | 4 ms
9,340 KB |
testcase_44 | AC | 5 ms
9,340 KB |
testcase_45 | AC | 299 ms
25,044 KB |
testcase_46 | AC | 304 ms
25,044 KB |
testcase_47 | AC | 303 ms
25,044 KB |
testcase_48 | AC | 298 ms
25,044 KB |
ソースコード
#include <cassert> #include <cmath> #include <algorithm> #include <iostream> #include <iomanip> #include <climits> #include <map> #include <queue> #include <set> #include <cstring> #include <vector> using namespace std; typedef long long ll; const ll MOD = 1000000007; ll mod_pow(ll x, ll n, ll mod = MOD) { ll res = 1; while (n > 0) { if (n & 1) { res = res * x % mod; } x = x * x % mod; n >>= 1; } return res; } class UnionFind { public: vector<int> _parent; vector<int> _rank; vector<int> _size; UnionFind(int n) { for (int i = 0; i < n; ++i) { _parent.push_back(i); _rank.push_back(0); _size.push_back(1); } } int find(int x) { if (_parent[x] == x) { return x; } else { return _parent[x] = find(_parent[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (_rank[x] < _rank[y]) { _parent[x] = y; _size[y] += _size[x]; } else { _parent[y] = x; _size[x] += _size[y]; if (_rank[x] == _rank[y]) ++_rank[x]; } } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return _size[find(x)]; } }; struct Edge { int x; int y; ll z; Edge(int x = -1, int y = -1, ll z = -1) { this->x = x; this->y = y; this->z = z; } bool operator>(const Edge &e) const { return z > e.z; } }; const int MAX_N = 200020; vector<int> G[MAX_N]; int counter[MAX_N]; bool visited[MAX_N]; int dfs(int v) { visited[v] = true; int cnt = 1; for (int u : G[v]) { if (visited[u]) continue; cnt += dfs(u); } visited[v] = false; counter[v] = cnt; return cnt; } int main() { memset(visited, false, sizeof(visited)); memset(counter, 0, sizeof(counter)); ll N, M, X; cin >> N >> M >> X; priority_queue <Edge, vector<Edge>, greater<Edge>> pque; for (int i = 0; i < M; ++i) { ll x, y, z; cin >> x >> y >> z; pque.push(Edge(x, y, z)); } UnionFind uf(N + 1); vector<Edge> edges; while (not pque.empty()) { Edge e = pque.top(); pque.pop(); if (uf.same(e.x, e.y)) continue; uf.unite(e.x, e.y); edges.push_back(e); G[e.x].push_back(e.y); G[e.y].push_back(e.x); } dfs(1); ll ans = 0; for (Edge &e : edges) { if (counter[e.x] < counter[e.y]) { ll a = counter[e.x]; ll b = N - a; ans += mod_pow(X, e.z) * a * b % MOD; } else { ll a = counter[e.y]; ll b = N - a; ans += mod_pow(X, e.z) * a * b % MOD; } ans %= MOD; } cout << ans << endl; return 0; }