結果
| 問題 |
No.1207 グラフX
|
| コンテスト | |
| ユーザー |
snow39
|
| 提出日時 | 2020-08-30 14:46:10 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 252 ms / 2,000 ms |
| コード長 | 2,439 bytes |
| コンパイル時間 | 1,494 ms |
| コンパイル使用メモリ | 109,200 KB |
| 実行使用メモリ | 38,660 KB |
| 最終ジャッジ日時 | 2024-11-15 07:54:10 |
| 合計ジャッジ時間 | 10,030 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 46 |
ソースコード
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
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;}
class DisjointSet{
public:
vector<int> rank,p;
vector<int> sz;
DisjointSet(){}
DisjointSet(int size){
rank.resize(size,0);
p.resize(size,0);
sz.resize(size,0);
for(int i=0;i<size;i++) makeSet(i);
}
void makeSet(int x){
p[x]=x;
rank[x]=0;
sz[x]=1;
}
bool same(int x,int y){
return findSet(x)==findSet(y);
}
void unite(int x,int y){
if(same(x,y)) return;
link(findSet(x),findSet(y));
}
void link(int x,int y){
if(rank[x]>rank[y]){
p[y]=x;
sz[x]+=sz[y];
}else{
p[x]=y;
sz[y]+=sz[x];
if(rank[x]==rank[y]){
rank[y]++;
}
}
}
int findSet(int x){
if(x!=p[x]){
p[x]=findSet(p[x]);
}
return p[x];
}
int findSize(int x){
return sz[findSet(x)];
}
};
ll mod_pow(ll x,ll n){
x%=MOD;
ll res=1;
while(n>0){
if(n&1) res=res*x%MOD;
x=x*x%MOD;
n>>=1;
}
return res;
}
void add(ll &a,ll b){
a=(a+b)%MOD;
}
void mul(ll &a,ll b){
a%=MOD;b%=MOD;
a=a*b%MOD;
}
vector<vector<pair<int,ll>>> g;
vector<ll> sz;
void szdfs(int now,int par){
sz[now]=1;
for(auto p:g[now]){
int nex=p.first;
if(nex==par) continue;
szdfs(nex,now);
sz[now]+=sz[nex];
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int N,M;
ll X;
cin>>N>>M>>X;
vector<int> A(M),B(M);
vector<ll> C(M);
rep(i,M) cin>>A[i]>>B[i]>>C[i];
rep(i,M){
A[i]--;B[i]--;
}
g.resize(N);
{
DisjointSet us(N);
rep(i,M){
if(us.same(A[i],B[i])) continue;
us.unite(A[i],B[i]);
g[A[i]].push_back(mkp(B[i],C[i]));
g[B[i]].push_back(mkp(A[i],C[i]));
}
}
sz.resize(N,0);
szdfs(0,-1);
ll ans=0;
rep(i,N) for(auto p:g[i]){
int to=p.first;
ll z=p.second;
if(i>to) continue;
ll num=min(sz[i],sz[to])*(N-min(sz[i],sz[to]));
ll val=mod_pow(X,z);
mul(val,num);
add(ans,val);
}
cout<<ans<<endl;
return 0;
}
snow39