結果

問題 No.1207 グラフX
ユーザー auauaauaua
提出日時 2020-08-30 14:54:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 418 ms / 2,000 ms
コード長 1,969 bytes
コンパイル時間 1,678 ms
コンパイル使用メモリ 174,448 KB
実行使用メモリ 43,444 KB
最終ジャッジ日時 2024-04-27 07:47:43
合計ジャッジ時間 16,061 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 318 ms
23,700 KB
testcase_01 AC 317 ms
23,576 KB
testcase_02 AC 312 ms
23,600 KB
testcase_03 AC 313 ms
23,704 KB
testcase_04 AC 319 ms
23,696 KB
testcase_05 AC 418 ms
43,364 KB
testcase_06 AC 414 ms
43,316 KB
testcase_07 AC 411 ms
43,320 KB
testcase_08 AC 272 ms
18,732 KB
testcase_09 AC 279 ms
21,552 KB
testcase_10 AC 409 ms
34,868 KB
testcase_11 AC 411 ms
43,444 KB
testcase_12 AC 258 ms
19,808 KB
testcase_13 AC 176 ms
13,492 KB
testcase_14 AC 305 ms
22,992 KB
testcase_15 AC 284 ms
20,416 KB
testcase_16 AC 165 ms
14,256 KB
testcase_17 AC 216 ms
18,432 KB
testcase_18 AC 173 ms
18,532 KB
testcase_19 AC 235 ms
16,180 KB
testcase_20 AC 314 ms
23,324 KB
testcase_21 AC 35 ms
11,568 KB
testcase_22 AC 217 ms
18,388 KB
testcase_23 AC 231 ms
19,724 KB
testcase_24 AC 150 ms
17,616 KB
testcase_25 AC 317 ms
23,204 KB
testcase_26 AC 250 ms
20,080 KB
testcase_27 AC 300 ms
21,744 KB
testcase_28 AC 285 ms
21,752 KB
testcase_29 AC 272 ms
21,760 KB
testcase_30 AC 154 ms
15,732 KB
testcase_31 AC 151 ms
12,980 KB
testcase_32 AC 119 ms
15,976 KB
testcase_33 AC 133 ms
15,840 KB
testcase_34 AC 269 ms
20,288 KB
testcase_35 AC 34 ms
11,756 KB
testcase_36 AC 264 ms
21,924 KB
testcase_37 AC 233 ms
18,716 KB
testcase_38 AC 67 ms
15,208 KB
testcase_39 AC 138 ms
16,908 KB
testcase_40 AC 128 ms
11,828 KB
testcase_41 AC 210 ms
17,528 KB
testcase_42 AC 5 ms
11,576 KB
testcase_43 AC 6 ms
11,704 KB
testcase_44 AC 6 ms
11,572 KB
testcase_45 AC 291 ms
23,556 KB
testcase_46 AC 292 ms
23,648 KB
testcase_47 AC 292 ms
23,620 KB
testcase_48 AC 291 ms
23,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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[MAX];
int par[MAX];
int depth[MAX];
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;
}
0