結果
| 問題 |
No.1207 グラフX
|
| ユーザー |
|
| 提出日時 | 2020-09-06 17:39:22 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 259 ms / 2,000 ms |
| コード長 | 3,376 bytes |
| コンパイル時間 | 1,890 ms |
| コンパイル使用メモリ | 177,916 KB |
| 実行使用メモリ | 33,852 KB |
| 最終ジャッジ日時 | 2024-11-29 07:24:03 |
| 合計ジャッジ時間 | 11,215 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 46 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}
#ifdef LOCAL
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cerr<<(*i)<<" ";cerr<<endl;}
#else
template<class T>void pv(T a,T b){}
#endif
ll nextLong() { ll x; scanf("%lld", &x); return x;}
const ll MOD = 1e9 + 7;
struct mint {
ll x;
mint(ll x=0):x((x%MOD+MOD)%MOD){}
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 b) const {
mint res(1), a(*this);
while (b) {
if (b & 1) res *= a;
a *= a;
b >>= 1;
}
return res;
}
// 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;}
};
ostream& operator<<(ostream& os, const mint& a) {os << a.x; return os;}
struct UnionFind {
vector<int> data; // 根の場合は集合の大きさ*-1、子の場合は親の番号が入る
UnionFind(int size) : data(size, -1) { }
bool link(int x, int y) { //新たな併合を行うとtrue
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
int root(int x) { // 代表元を返す
return data[x] < 0 ? x : data[x] = root(data[x]);
}
int size(int x) { // 要素xが含まれる集合の大きさ
return -data[root(x)];
}
bool same(int x, int y) { // 同じ集合ならtrue
return root(x) == root(y);
}
int num() { // 異なる集合の数
int res = 0;
REP(i, data.size()) res += root(i) == i;
return res;
}
};
struct Edge {
int a, b, z;
};
vector<Edge> es;
vector< vector<int> > g;
const int MAX_N = 212345;
int sz[MAX_N];
int dfs(int cur, int prev) {
int res = 1;
for (int eid : g[cur]) {
int nxt = es[eid].a ^ es[eid].b ^ cur;
if (nxt == prev) continue;
int c = dfs(nxt, cur);
sz[eid] = c;
res += c;
}
return res;
}
int main2() {
int N = nextLong();
int M = nextLong();
ll X = nextLong();
UnionFind uf(N);
es.clear();
g = vector< vector<int> >(N);
REP(i, M) {
int a = nextLong() - 1;
int b = nextLong() - 1;
int z = nextLong();
if (uf.link(a, b)) {
g[a].push_back(es.size());
g[b].push_back(es.size());
es.push_back((Edge){a, b, z});
}
}
CLR(sz, 0);
dfs(0, -1);
mint ans = 0;
for (int i = 0; i < (int)es.size(); i++) {
ans += mint(sz[i]) * mint(N - sz[i]) * mint(X).pow(es[i].z);
}
cout << ans << endl;
return 0;
}
int main() {
#ifdef LOCAL
for (;!cin.eof();cin>>ws)
#endif
main2();
return 0;
}