結果
問題 | No.1207 グラフX |
ユーザー |
![]() |
提出日時 | 2022-08-12 17:51:44 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 639 ms / 2,000 ms |
コード長 | 1,652 bytes |
コンパイル時間 | 2,411 ms |
コンパイル使用メモリ | 211,420 KB |
最終ジャッジ日時 | 2025-01-30 20:31:34 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
#define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) v.begin(), v.end() typedef long long ll; #include <bits/stdc++.h> using namespace std; vector<int> G[200200]; class Dis{ public: vector<ll> rank,p,siz; Dis(int s){ rank.resize(s,0); p.resize(s,0); siz.resize(s,1); rep(i,s) makeSet(i); } void makeSet(int x){ p[x]=x; rank[x]=0; } 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; siz[x]+=siz[y]; } else{ p[x]=y; siz[y]+=siz[x]; if(rank[x]==rank[y]) rank[y]++; } } int findSet(int x){ if(x != p[x]) p[x]=findSet(p[x]); return p[x]; } }; const int MOD=1e9+7; ll modpow(ll x,ll n){ x%=MOD; ll ans=1; while(n){ if(n&1) ans=ans*x%MOD; x=x*x%MOD; n/=2; } return ans; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); ll n,m,x; cin>>n>>m>>x; Dis ds=Dis(n); map<pair<int,int>,ll> M; rep(i,m){ ll x,y,z; cin>>x>>y>>z; x--,y--; if(!ds.same(x,y)){ G[x].push_back(y); G[y].push_back(x); M[{x,y}]=z; M[{y,x}]=z; ds.unite(x,y); } } ll ans=0; vector<ll> chi(n); auto dfs=[&](auto dfs,int v,int p)->void{ ll tmp=0; for(auto nv:G[v]){ if(nv==p) continue; dfs(dfs,nv,v); tmp+=chi[nv]; ans=(ans+chi[nv]*(n-chi[nv])%MOD*modpow(x,M[{v,nv}])%MOD)%MOD; } chi[v]=tmp+1; }; dfs(dfs,0,-1); cout<<ans<<endl; return 0; }