結果

問題 No.1333 Squared Sum
ユーザー anmichianmichi
提出日時 2021-01-08 23:10:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,055 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 210,612 KB
実行使用メモリ 40,560 KB
最終ジャッジ日時 2024-04-28 05:04:47
合計ジャッジ時間 10,064 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,132 KB
testcase_01 AC 4 ms
10,540 KB
testcase_02 AC 4 ms
10,792 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 4 ms
8,192 KB
testcase_17 AC 4 ms
8,192 KB
testcase_18 AC 4 ms
8,064 KB
testcase_19 AC 5 ms
8,320 KB
testcase_20 AC 4 ms
8,064 KB
testcase_21 AC 4 ms
8,064 KB
testcase_22 AC 4 ms
8,192 KB
testcase_23 AC 4 ms
8,192 KB
testcase_24 AC 4 ms
8,064 KB
testcase_25 AC 4 ms
8,192 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 132 ms
22,204 KB
testcase_41 AC 124 ms
22,200 KB
testcase_42 AC 123 ms
22,072 KB
testcase_43 AC 119 ms
22,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define P pair<int,int>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
template<class T,class U>
inline bool chmax(T &a,U b){
    if(a<b){
        a=b;
        return true;
    }
    return false;
}
template<class T,class U>
inline bool chmin(T &a,U b){
    if(a>b){
        a=b;
        return true;
    }
    return false;
}
constexpr int INF=1000000000;
constexpr ll llINF=1000000000000000000;
constexpr int mod=1000000007;
constexpr double eps=1e-8;
const double pi=acos(-1);
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int Random(int mi,int ma){
    random_device rnd;
    mt19937 mt(rnd());//32bit
    //[mi,ma]
    uniform_int_distribution<int>engine(mi,ma);
    return engine(mt);
}
/*
vector<vector<ll>>C,sC;
void init_comb(int n,int m){
    C.resize(n+1,vector<ll>(m+1,0));
    sC.resize(n+1,vector<ll>(m+1,0));
    C[0][0]=1;
    for(int i=1;i<=n;i++){
        C[i][0]=1;
        for(int j=1;j<=m;j++){
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
        }
    }
    rep(i,n+1){
        rep(j,m){
            sC[i][j+1]=(sC[i][j]+C[i][j])%mod;
        }
    }
}*/
ll gcd(ll a,ll b){
    while(a%b){
        a%=b;
        swap(a,b);
    }
    return b;
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
bool prime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
ll modpow(ll a,ll b){
    ll res=1;
    while(b){
        if(b&1){
            res*=a;
            res%=mod;
        }
        a*=a;
        a%=mod;
        b>>=1;
    }
    return res;
}
vector<ll>inv,fact,factinv;
void init_fact(int n){
    inv.resize(n+1);
    fact.resize(n+1);
    factinv.resize(n+1);
    inv[0]=0;
    inv[1]=1;
    fact[0]=1;
    factinv[0]=1;
    for(ll i=1;i<=n;i++){
        if(i>=2)inv[i]=mod-((mod/i)*inv[mod%i]%mod);
        fact[i]=(fact[i-1]*i)%mod;
        factinv[i]=factinv[i-1]*inv[i]%mod;
    }
}
ll _inv(ll a,ll m=mod){
    //gcd(a,m) must be 1
    ll b=m,u=1,v=0;
    while(b){
        ll t=a/b;
        a-=t*b;swap(a,b);
        u-=t*v;swap(u,v);
    }
    u%=m;
    if(u<0)u+=m;
    return u;
}
ll comb(int a,int b){
    if(a<b)return 0;
    if(a<0)return 0;
    return fact[a]*factinv[a-b]%mod*factinv[b]%mod;
}
ll multicomb(int a,int b){
    return comb(a+b-1,b);
}

struct edge{int to;ll cost;};
int n;
ll distsum[200010],dist[200010],siz[200010],dp[200010];
vector<edge>G[200010];
bool used[200010];
void dfs(int x){
    used[x]=true;
    siz[x]=1;
    for(edge e:G[x]){
        if(!used[e.to]){
            dist[e.to]=dist[x]+e.cost;
            dfs(e.to);
            siz[x]+=siz[e.to];
        }
    }
}
void dfs3(int x){
    used[x]=true;
    for(edge e:G[x]){
        if(!used[e.to]){
            dfs3(e.to);
            distsum[x]+=(distsum[e.to]+siz[e.to]*e.cost%mod)%mod;
            distsum[x]%=mod;
        }
    }
}
void dfs2(int x,ll distsum_l=0){
    used[x]=true;
    for(edge e:G[x]){
        if(!used[e.to]){
            ll distsum_to=(distsum[e.to]+siz[e.to]*e.cost%mod)%mod;
            ll new_l=(distsum_l+(distsum[x]+mod-distsum_to))%mod;
            dp[e.to]+=(dp[x]+new_l*2*e.cost%mod)%mod;
            dp[e.to]%=mod;
            dp[e.to]-=distsum_to*2*e.cost%mod;
            dp[e.to]+=mod;
            dp[e.to]%=mod;
            dp[e.to]+=e.cost%mod*e.cost%mod*n%mod;
            dp[e.to]%=mod;
            new_l+=e.cost*(n-siz[e.to])%mod;
            new_l%=mod;
            dfs2(e.to,new_l);
        }
    }
}
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    cin>>n;
    rep(i,n-1){
        int a,b,w;
        cin>>a>>b>>w;
        a--,b--;
        G[a].pb({b,w});
        G[b].pb({a,w});
    }
    dfs(0);
    fill(used,used+n,false);
    dfs3(0);
    fill(used,used+n,false);
    rep(i,n){
        dp[0]+=dist[i]*dist[i]%mod;
        dp[0]%=mod;
    }
    dfs2(0);
    ll ans=0;
    rep(i,n){
        ans+=dp[i];
        ans%=mod;
    }
    cout<<ans*_inv(2)%mod<<endl;
    return 0;
}
0