結果
| 問題 |
No.1207 グラフX
|
| コンテスト | |
| ユーザー |
Example0911
|
| 提出日時 | 2020-09-01 19:06:18 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 578 ms / 2,000 ms |
| コード長 | 3,140 bytes |
| コンパイル時間 | 2,114 ms |
| コンパイル使用メモリ | 187,552 KB |
| 実行使用メモリ | 49,572 KB |
| 最終ジャッジ日時 | 2024-11-20 11:19:50 |
| 合計ジャッジ時間 | 24,764 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 46 |
ソースコード
#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;
}
Example0911