結果
問題 | No.1207 グラフX |
ユーザー |
![]() |
提出日時 | 2023-03-15 20:27:52 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,667 bytes |
コンパイル時間 | 1,780 ms |
コンパイル使用メモリ | 144,116 KB |
実行使用メモリ | 34,728 KB |
最終ジャッジ日時 | 2024-09-18 08:51:43 |
合計ジャッジ時間 | 16,255 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 WA * 5 |
ソースコード
#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]; int dfs(int v, bool *visited) { visited[v] = true; int cnt = 1; for (int u : G[v]) { if (visited[u]) continue; cnt += dfs(u, visited); } visited[v] = false; counter[v] = cnt; return cnt; } int main() { 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); } bool visited[N + 1]; memset(visited, false, sizeof(visited)); dfs(1, visited); 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; }