結果
問題 | No.1333 Squared Sum |
ユーザー | 沙耶花 |
提出日時 | 2021-03-04 20:52:36 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,176 bytes |
コンパイル時間 | 3,059 ms |
コンパイル使用メモリ | 237,760 KB |
実行使用メモリ | 145,416 KB |
最終ジャッジ日時 | 2024-10-05 09:25:34 |
合計ジャッジ時間 | 26,390 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 2 ms
6,820 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 | 2 ms
6,816 KB |
testcase_17 | AC | 2 ms
6,816 KB |
testcase_18 | AC | 2 ms
6,820 KB |
testcase_19 | AC | 1 ms
6,816 KB |
testcase_20 | AC | 2 ms
6,820 KB |
testcase_21 | AC | 2 ms
6,820 KB |
testcase_22 | AC | 1 ms
6,820 KB |
testcase_23 | AC | 1 ms
6,820 KB |
testcase_24 | AC | 2 ms
6,820 KB |
testcase_25 | AC | 1 ms
6,816 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 | WA | - |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | WA | - |
ソースコード
#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<int> depth; vector<vector<int>> pos; vector<vector<int>> bind; vector<vector<int>> childs; vector<vector<int>> gs; centroid_tree(vector<vector<int>> argE){ int n = argE.size(); E.resize(n,vector<int>()); cnt.resize(n); r = go(0,argE); depth.resize(n); pos.resize(n); bind.resize(n); childs.resize(n); gs.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){ depth[cur] = 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; if(gs[x].size()<=d)gs[x].resize(d+1,-1); gs[x][d] = cur; } 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(); if(gs[x].size()<=d)gs[x].resize(d+1,-1); gs[x][d] = cur; bind[cur].push_back(childs[cur].size()); } }; struct HLD{ vector<int> sz,parent,depth,root,pos; vector<int> arr; HLD(vector<vector<int>> &E){ sz.resize(E.size(),1); parent.resize(E.size(),0); depth.resize(E.size(),0); root.resize(E.size(),0); pos.resize(E.size(),0); dfs(0,-1,E); dfs2(0,-1,E,0); } void dfs(int now,int p,vector<vector<int>> &E){ parent[now] = p; if(p==-1){ depth[now] = 0; } else{ depth[now] = depth[p]+1; } for(int i=0;i<E[now].size();i++){ int to = E[now][i]; if(to==p)continue; dfs(to,now,E); sz[now] += sz[to]; } } void dfs2(int now,int p,vector<vector<int>> &E,int r){ pos[now] = arr.size(); arr.push_back(now); root[now] = r; int maxi = 0; int ind = -1; for(int i=0;i<E[now].size();i++){ int to = E[now][i]; if(to==p)continue; if(maxi<sz[to]){ maxi = sz[to]; ind = to; } } if(ind==-1)return; dfs2(ind,now,E,r); for(int i=0;i<E[now].size();i++){ int to = E[now][i]; if(to==p||to==ind)continue; dfs2(to,now,E,to); } } vector<pair<int,int>> query(int u,int v){ vector<pair<int,int>> ret; int t = 0; while(root[u]!=root[v]){ if(depth[root[u]] <= depth[root[v]]){ ret.insert(ret.begin()+t,{pos[root[v]], pos[v]}); v = parent[root[v]]; } else{ ret.insert(ret.begin()+t,{pos[u],pos[root[u]]}); u = parent[root[u]]; t++; } } ret.insert(ret.begin()+t,{pos[u],pos[v]}); return ret; } int lca(int u,int v){ for(;;v=parent[root[v]]){ if(pos[u]>pos[v])swap(u,v); if(root[u]==root[v])return u; } } int get_distance(int u,int v){ return depth[u] + depth[v] - 2 * depth[lca(u,v)]; } }; mint get_dis(HLD &H,vector<mint> &S,int u,int v){ auto t = H.query(u,v); mint ret = 0; rep(i,t.size()){ int l = t[i].first,r = t[i].second; if(l>r)swap(l,r); ret += S[r+1] - S[l]; } return ret; } int main(){ int n; cin>>n; vector<vector<int>> E(n); vector<vector<int>> E2(n*2-1); vector<mint> S(2*n,0); 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].push_back(n+i); E2[v].push_back(n+i); E2[n+i].push_back(u); E2[n+i].push_back(v); S[n+i] = w; } centroid_tree C(E); if(n>100)return 0; HLD H(E2); { vector<mint> SS(2*n,0); rep(i,2*n-1){ SS[H.pos[i]+1] = S[i]; } rep(i,2*n-1){ SS[i+1] += SS[i]; } swap(S,SS); } vector<vector<mint>> sum(n); rep(i,n){ sum[i].push_back(0); rep(j,C.childs[i].size()){ int u = C.childs[i][j]; mint d = get_dis(H,S,i,u); sum[i].push_back(d); } rep(j,sum[i].size()){ if(j==0)continue; sum[i][j] += sum[i][j-1]; } } mint ans = 0; rep(i,n){ rep(j,C.gs[i].size()){ int x = C.gs[i][j]; int l = C.bind[x][C.pos[i][j]]; int r = C.bind[x][C.pos[i][j]+1]; mint d = get_dis(H,S,i,x); ans += d*d * (C.bind[x].back() - (r-l)); mint s = 0; s += sum[x].back() - sum[x][r]; s += sum[x][l]; ans += s*d; } } cout<<ans.val()<<endl; return 0; }