結果
問題 | No.1333 Squared Sum |
ユーザー | 沙耶花 |
提出日時 | 2021-03-04 21:44:02 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,676 ms / 2,000 ms |
コード長 | 4,383 bytes |
コンパイル時間 | 2,806 ms |
コンパイル使用メモリ | 234,972 KB |
実行使用メモリ | 122,256 KB |
最終ジャッジ日時 | 2024-10-05 10:35:22 |
合計ジャッジ時間 | 33,485 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 1,171 ms
106,156 KB |
testcase_04 | AC | 1,234 ms
106,304 KB |
testcase_05 | AC | 1,189 ms
106,316 KB |
testcase_06 | AC | 1,266 ms
106,076 KB |
testcase_07 | AC | 1,144 ms
105,888 KB |
testcase_08 | AC | 1,213 ms
106,008 KB |
testcase_09 | AC | 1,120 ms
105,676 KB |
testcase_10 | AC | 1,195 ms
105,856 KB |
testcase_11 | AC | 1,214 ms
106,124 KB |
testcase_12 | AC | 1,169 ms
105,704 KB |
testcase_13 | AC | 738 ms
122,256 KB |
testcase_14 | AC | 1,676 ms
117,508 KB |
testcase_15 | AC | 1,581 ms
121,016 KB |
testcase_16 | AC | 2 ms
5,248 KB |
testcase_17 | AC | 1 ms
5,248 KB |
testcase_18 | AC | 1 ms
5,248 KB |
testcase_19 | AC | 2 ms
5,248 KB |
testcase_20 | AC | 1 ms
5,248 KB |
testcase_21 | AC | 1 ms
5,248 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 1 ms
5,248 KB |
testcase_24 | AC | 2 ms
5,248 KB |
testcase_25 | AC | 1 ms
5,248 KB |
testcase_26 | AC | 1,556 ms
121,868 KB |
testcase_27 | AC | 1,563 ms
117,776 KB |
testcase_28 | AC | 1,657 ms
121,816 KB |
testcase_29 | AC | 742 ms
122,112 KB |
testcase_30 | AC | 349 ms
45,892 KB |
testcase_31 | AC | 165 ms
27,256 KB |
testcase_32 | AC | 566 ms
65,008 KB |
testcase_33 | AC | 408 ms
51,884 KB |
testcase_34 | AC | 866 ms
85,756 KB |
testcase_35 | AC | 621 ms
65,256 KB |
testcase_36 | AC | 284 ms
40,096 KB |
testcase_37 | AC | 292 ms
41,412 KB |
testcase_38 | AC | 385 ms
48,036 KB |
testcase_39 | AC | 707 ms
73,912 KB |
testcase_40 | AC | 549 ms
90,912 KB |
testcase_41 | AC | 500 ms
90,780 KB |
testcase_42 | AC | 519 ms
90,788 KB |
testcase_43 | AC | 517 ms
90,788 KB |
ソースコード
#include <stdio.h> #include <bits/stdc++.h> #include <atcoder/modint> using namespace atcoder; using mint = modint1000000007; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000000000000000 struct centroid_tree{ vector<vector<int>> E; vector<int> cnt; int r; vector<vector<int>> pos; vector<vector<int>> bind; vector<vector<int>> childs; centroid_tree(vector<vector<int>> argE){ int n = argE.size(); E.resize(n,vector<int>()); cnt.resize(n); r = go(0,argE); pos.resize(n); bind.resize(n); childs.resize(n); dfs(r,-1,E); build(r,0); } int go(int a,vector<vector<int>> &argE){ dfs(a,-1,argE); int sz = cnt[a]; int ret = dfs2(a,-1,argE,sz); rep(i,argE[ret].size()){ int to = argE[ret][i]; argE[to].erase(find(argE[to].begin(),argE[to].end(),ret)); int g = go(to,argE); E[ret].push_back(g); } argE[ret].clear(); return ret; } void dfs(int cur,int p,vector<vector<int>> &argE){ cnt[cur] = 1; rep(i,argE[cur].size()){ int to = argE[cur][i]; if(to==p)continue; dfs(to,cur,argE); cnt[cur] += cnt[to]; } } int dfs2(int cur,int p,vector<vector<int>> &argE,int sz){ int ret = -1; int m = 0; rep(i,argE[cur].size()){ int to = argE[cur][i]; if(to==p)continue; ret = dfs2(to,cur,argE,sz); if(ret!=-1)return ret; m = max(m,cnt[to]); } if(sz-cnt[cur]<=sz/2 && m<=sz/2){ ret = cur; } return ret; } void build(int cur,int d){ bind[cur].push_back(0); rep(i,E[cur].size()){ int to = E[cur][i]; build(to,d+1); rep(j,childs[to].size()){ int x = childs[to][j]; childs[cur].push_back(x); if(pos[x].size()<=d)pos[x].resize(d+1,-1); pos[x][d] = i; } bind[cur].push_back(childs[cur].size()); } int x = cur; childs[cur].push_back(x); if(pos[x].size()<=d)pos[x].resize(d+1,-1); pos[x][d] = E[cur].size(); bind[cur].push_back(childs[cur].size()); } }; struct lca{ vector<int> depth;//depth[i] 頂点iの深さ vector<vector<int>> parents;//parents[i][j] 頂点iから2^j個上の親 int max_j = 18; lca(int n,vector<vector<int>> &E){ depth.assign(E.size(),-1); parents.assign(E.size(),vector<int>(max_j,-1)); stack<int> s; s.push(n); depth[n] = 0; while(s.size()!=0){ int k = s.top(); s.pop(); for(int i=0;i<E[k].size();i++){ int x = E[k][i]; if(depth[x]!=-1)continue; s.push(x); depth[x] = depth[k]+1; for(int j=0;j<max_j;j++){ if(j==0){ parents[x][j] = k; } else{ parents[x][j] = parents[parents[x][j-1]][j-1]; } if(parents[x][j] == -1)break; } } } } //頂点uのk番目の親 int kth_parent(int u,int k){ for(int i=0;i<max_j;i++){ if(k==0)break; if(u==-1)break; if(k%2){ u = parents[u][i]; } k/=2; } return u; } int query(int u,int v){ if(depth[u]<depth[v])swap(u,v); u = kth_parent(u,depth[u]-depth[v]); if(u==v){ return u; } for(int i=max_j-1;i>=0;i--){ if(parents[u][i]!=parents[v][i]){ u = parents[u][i]; v = parents[v][i]; } } return parents[u][0]; } int get_distance(int u,int v){ return depth[u]+depth[v]-2*depth[query(u,v)]; } }; mint get_dis(lca &H,vector<mint> &S,int u,int v){ int l = H.query(u,v); mint ret = S[u] + S[v] - S[l]*2; return ret; } int main(){ int n; cin>>n; vector<vector<int>> E(n); vector<vector<pair<int,mint>>> E2(n); rep(i,n-1){ int u,v,w; scanf("%d %d %d",&u,&v,&w); u--;v--; E[u].push_back(v); E[v].push_back(u); E2[u].emplace_back(v,w); E2[v].emplace_back(u,w); } centroid_tree C(E); lca H(0,E); vector<mint> dis(n,0); vector<bool> visited(n,false); queue<int> Q; Q.push(0); visited[0] = true; while(Q.size()>0){ int u = Q.front(); Q.pop(); rep(i,E2[u].size()){ int v = E2[u][i].first; mint w = E2[u][i].second; if(visited[v])continue; visited[v] = true; dis[v] = dis[u] + w; Q.push(v); } } mint ans = 0; rep(i,n){ mint sum = 0; mint temp = 0; int cur = 0; rep(j,C.childs[i].size()){ mint d = get_dis(H,dis,i,C.childs[i][j]); ans += d*d * (C.bind[i].back() - (C.bind[i][cur+1] - C.bind[i][cur])); ans += d*2*sum; temp += d; if(j+1 == C.bind[i][cur+1]){ cur++; sum += temp; temp = 0; } } } cout<<ans.val()<<endl; return 0; }