結果
| 問題 |
No.1207 グラフX
|
| コンテスト | |
| ユーザー |
auaua
|
| 提出日時 | 2020-08-30 14:53:59 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,978 bytes |
| コンパイル時間 | 1,947 ms |
| コンパイル使用メモリ | 174,340 KB |
| 実行使用メモリ | 16,544 KB |
| 最終ジャッジ日時 | 2024-11-15 08:00:45 |
| 合計ジャッジ時間 | 11,188 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 13 WA * 1 RE * 32 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll inf=1e9+7;
const ll mod=1e9+7;
const int MAX=200010;
//Union-Find木
int siz[100010];
int par[100010];
int depth[100010];
void init(int n){
for(int i=0;i<=n;i++){
par[i]=i;
depth[i]=0;
}
rep(i,n+1){
siz[i]=1;
}
}
int root(int x){
return par[x]==x?x:par[x]=root(par[x]);
}
bool same(int x,int y){
return root(x)==root(y);
}
void unite(int x,int y){
x=root(x);
y=root(y);
ll sx=siz[x];
ll sy=siz[y];
if(x==y)return;
if(depth[x]<depth[y]){
par[x]=y;
}else{
par[y]=x;
if(depth[x]==depth[y])depth[x]++;
}
siz[root(x)]=sx+sy;
}
ll rui(ll a,ll b){
ll res=1;
ll x=a;
while(b){
if(b&1)res=res*x%mod;
b/=2;
x=x*x%mod;
}
return res;
}
vector<vector<pll> >G(MAX);
vector<ll>sz(MAX);
void dfs(ll i,ll p){
ll res=1;
for(auto e:G[i]){
if(e.fi==p)continue;
dfs(e.fi,i);
res+=sz[e.fi];
}
sz[i]=res;
}
ll n;
ll DFS(ll i,ll p){
ll res=0;
for(auto e:G[i]){
if(e.fi==p)continue;
res=(res+DFS(e.fi,i))%mod;
res=(res+e.se*sz[e.fi]%mod*(n-sz[e.fi])%mod)%mod;
}
return res;
}
signed main(){
ll m,x;cin>>n>>m>>x;
init(n);
rep(i,m){
ll a,b,z;cin>>a>>b>>z;
a--;b--;
if(same(a,b))continue;
ll v=rui(x,z);
G[a].pb(mp(b,v));
G[b].pb(mp(a,v));
unite(a,b);
}
dfs(0,-1);
ll ans=DFS(0,-1);
if(ans<0)ans+=mod;
cout<<ans<<endl;
}
auaua