結果

問題 No.1333 Squared Sum
ユーザー noya2noya2
提出日時 2022-11-03 04:17:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 425 ms / 2,000 ms
コード長 6,326 bytes
コンパイル時間 6,878 ms
コンパイル使用メモリ 279,332 KB
実行使用メモリ 91,124 KB
最終ジャッジ日時 2023-09-24 14:31:42
合計ジャッジ時間 21,482 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,356 KB
testcase_03 AC 314 ms
35,956 KB
testcase_04 AC 315 ms
35,700 KB
testcase_05 AC 317 ms
35,904 KB
testcase_06 AC 318 ms
35,640 KB
testcase_07 AC 321 ms
35,656 KB
testcase_08 AC 318 ms
35,652 KB
testcase_09 AC 321 ms
35,952 KB
testcase_10 AC 320 ms
35,648 KB
testcase_11 AC 322 ms
35,948 KB
testcase_12 AC 314 ms
35,632 KB
testcase_13 AC 314 ms
91,124 KB
testcase_14 AC 413 ms
63,380 KB
testcase_15 AC 421 ms
84,476 KB
testcase_16 AC 2 ms
4,356 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 1 ms
4,360 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 1 ms
4,352 KB
testcase_22 AC 2 ms
4,352 KB
testcase_23 AC 2 ms
4,356 KB
testcase_24 AC 2 ms
4,352 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 413 ms
89,808 KB
testcase_27 AC 391 ms
65,000 KB
testcase_28 AC 425 ms
89,284 KB
testcase_29 AC 322 ms
91,100 KB
testcase_30 AC 109 ms
16,848 KB
testcase_31 AC 50 ms
11,072 KB
testcase_32 AC 174 ms
22,904 KB
testcase_33 AC 128 ms
18,940 KB
testcase_34 AC 246 ms
29,436 KB
testcase_35 AC 176 ms
22,880 KB
testcase_36 AC 82 ms
15,040 KB
testcase_37 AC 100 ms
15,568 KB
testcase_38 AC 116 ms
17,376 KB
testcase_39 AC 209 ms
25,752 KB
testcase_40 AC 181 ms
36,356 KB
testcase_41 AC 178 ms
36,140 KB
testcase_42 AC 179 ms
36,172 KB
testcase_43 AC 171 ms
35,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "c.cpp"
#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define repp(i,n,m) for (int i = m; i < int(n); ++i)
#define reb(i,n) for (int i = int(n)-1; i >= 0; --i)
#define all(v) v.begin(),v.end()
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using pdd = pair<long double, long double>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
template<class T>istream &operator>>(istream &is,vector<T> &v){for(auto &e:v)is>>e;return is;}
template<typename T>bool range(T a,T b,T x){return (a<=x&&x<b);}
template<typename T>bool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));}
template<typename T>void rev(vector<T> &v){reverse(v.begin(),v.end());}
void revs(string &s) {reverse(s.begin(),s.end());}
template<typename T>void sor(vector<T> &v, int f=0){sort(v.begin(),v.end());if(f!=0) rev(v);}
template<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
template<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<typename T>void uniq(vector<T> &v){sor(v);v.erase(unique(v.begin(),v.end()),v.end());}
template<typename T1, typename T2>void print(pair<T1,T2> a);
template<typename T>void print(vector<T> v);
template<typename T>void print(vector<vector<T>> v);
void print(){ putchar(' '); }
void print(bool a){ printf("%d", a); }
void print(int a){ printf("%d", a); }
void print(long a){ printf("%ld", a); }
void print(long long a){ printf("%lld", a); }
void print(char a){ printf("%c", a); }
void print(char a[]){ printf("%s", a); }
void print(const char a[]){ printf("%s", a); }
void print(long double a){ printf("%.15Lf", a); }
void print(const string& a){ for(auto&& i : a) print(i); }
void print(unsigned int a){ printf("%u", a); }
void print(unsigned long long a) { printf("%llu", a); }
template<class T> void print(const T& a){ cout << a; }
int out(){ putchar('\n'); return 0; }
template<class T> int out(const T& t){ print(t); putchar('\n'); return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }
template<typename T1,typename T2>void print(pair<T1,T2> a){print(a.first);print(),print(a.second);}
template<typename T>void print(vector<T> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}}
template<typename T>void print(vector<vector<T>> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}}
void yes(){out("Yes");}
void no (){out("No");}
void yn (bool t){if(t)yes();else no();}
void fast_io(){cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);}
void o(){out("!?");}

namespace noya2{

const int INF = 1001001007;
const long long mod1 = 998244353;
const long long mod2 = 1000000007;
const long long inf = 2e18;
const long double pi = 3.14159265358979323;
const long double eps = 1e-7;
const vector<int> dx = {0,1,0,-1,1,1,-1,-1};
const vector<int> dy = {1,0,-1,0,1,-1,-1,1};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string NUM = "0123456789";

} // namespace noya2
using namespace noya2;

using mint = modint1000000007;
void out(mint a){out(a.val());}
void out(vector<mint> a){vector<ll> b(a.size()); rep(i,a.size()) b[i] = a[i].val(); out(b);}
void out(vector<vector<mint>> a){for (auto v : a) out(v);}
istream &operator>>(istream &is,vector<mint> &v){for(auto &e:v){ll _x;is>>_x;e=_x;}return is;}

#line 2 "Rerooting.hpp"

#line 4 "Rerooting.hpp"

namespace noya2 {

using namespace std;

template<class E, class V, E (*merge)(E, E), E (*e)(), E (*put_edge)(V, int), V (*put_vertex)(E, int)>
struct Rerooting {
    struct edge{
        int from, to, idx, rev_idx;
    };
    Rerooting (int _n = 0) : n(_n) { es.resize(n);}
    void add_edge(int u, int v, int idx1, int idx2){
        es[u].push_back({u,v,idx1,idx2});
        es[v].push_back({v,u,idx2,idx1});
    }
    V build(int v = 0){
        root = v;
        vis.resize(n,0);
        outs.resize(n);
        return dfs(root);
    }
    vector<V> reroot(){
        reverse_edge.resize(n);
        reverse_edge[root] = e();
        answers.resize(n);
        bfs(root);
        return answers;
    }
  private:
    int n, root;
    vector<vector<edge>> es;
    vector<int> vis;
    vector<vector<E>> outs;
    vector<E> reverse_edge;
    vector<V> answers;
    V dfs(int v){
        vis[v]++;
        E val = e();
        for (auto &p : es[v]){
            if (vis[p.to] > 0 && p.to != es[v].back().to) swap(p,es[v].back());
            if (vis[p.to] > 0) continue;
            E nval = put_edge(dfs(p.to),p.idx);
            outs[v].emplace_back(nval);
            val = merge(val,nval);
        }
        return put_vertex(val,v);
    }
    void bfs(int v){
        int siz = outs[v].size();
        vector<E> lui(siz+1), rui(siz+1);
        lui[0] = e(), rui[siz] = e();
        for (int i = 0; i < siz; i++) lui[i+1] = merge(lui[i],outs[v][i]);
        for (int i = siz-1; i >= 0; i--) rui[i] = merge(outs[v][i],rui[i+1]);
        for (int i = 0; i < siz; i++){
            reverse_edge[es[v][i].to] = put_edge(put_vertex(merge(merge(lui[i],rui[i+1]),reverse_edge[v]),v),es[v][i].rev_idx);
            bfs(es[v][i].to);
        }
        answers[v] = put_vertex(merge(lui[siz],reverse_edge[v]), v);
    }
};

} // namespace noya2
#line 78 "c.cpp"

vector<mint> a;

struct S{
    mint s, s2, siz;
};
S op(S a, S b){
    S c;
    c.siz = a.siz + b.siz;
    c.s = a.s + b.s;
    c.s2 = a.s2 + b.s2;
    return c;
}
S e(){return S{0,0,0};}
S pute(S x, int i){
    S y;
    y.siz = x.siz;
    y.s = x.s + a[i] * x.siz;
    y.s2 = x.s2 + 2 * a[i] * x.s + a[i] * a[i] * x.siz;
    return y;
}
S putv(S x, int v){
    x.siz += 1;
    return x;
}

void solve(){
    int n; cin >> n;
    a.resize(n-1);
    Rerooting<S,S,op,e,pute,putv> g(n);
    rep(i,n-1){
        int u, v; cin >> u >> v;
        ll c; cin >> c; a[i] = c;
        g.add_edge(u-1,v-1,i,i);
    }
    g.build();
    mint ans = 0;
    for (auto x : g.reroot()) ans += x.s2;
    ans /= 2;
    out(ans.val());
}

int main(){
    fast_io();
    int t = 1; //cin >> t;
    while(t--) solve();
}
0