結果
| 問題 |
No.872 All Tree Path
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-04 17:03:58 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 289 ms / 3,000 ms |
| コード長 | 5,201 bytes |
| コンパイル時間 | 1,757 ms |
| コンパイル使用メモリ | 183,136 KB |
| 実行使用メモリ | 44,288 KB |
| 最終ジャッジ日時 | 2024-07-03 07:32:12 |
| 合計ジャッジ時間 | 6,177 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(obj) (obj).begin(),(obj).end()
#define SPEED cin.tie(0);ios::sync_with_stdio(false);
template<class Operator> class Tree {
Operator Op;
using typeDist = decltype(Op.unitDist);
size_t num;
size_t ord;
public:
vector<vector<pair<size_t,typeDist>>> edge;
vector<size_t> depth;
vector<size_t> order;
vector<typeDist> dist;
vector<pair<size_t,typeDist>> parent;
vector<vector<pair<size_t,typeDist>>> child;
vector<array<pair<size_t,typeDist>,Operator::bit>> ancestor;
vector<size_t> size;
vector<vector<size_t>> descendant;
Tree(const int num):num(num),edge(num),depth(num,-1),order(num),dist(num){}
//O(1) anytime
void makeEdge(const int& from, const int& to, const typeDist w = 1) {
edge[from].push_back({to,w});
}
//O(N) anytime
void makeDepth(const int root) {
depth[root] = 0;
dist[root] = Op.unitDist;
ord = 0;
dfs1(root);
order[ord++] = root;
}
//O(N) anytime
void makeDepth(void) {
ord = 0;
for(size_t root = 0; root < num; ++root) {
if(depth[root] != -1) continue;
depth[root] = 0;
dist[root] = Op.unitDist;
dfs1(root);
order[ord++] = root;
}
}
//for makeDepth
void dfs1(int curr, int prev = -1){
for(auto& e:edge[curr]){
int next = e.first;
if(next==prev) continue;
depth[next] = depth[curr] + 1;
dist[next] = Op.funcDist(dist[curr],e.second);
dfs1(next,curr);
order[ord++] = next;
}
}
//O(N) after makeDepth
void makeParent(void) {
parent.resize(num,make_pair(num,Op.unitDist));
for (size_t i = 0; i < num; ++i) for (auto& e : edge[i]) if (depth[i] > depth[e.first]) parent[i] = e;
}
//O(N) after makeDepth
void makeChild(void) {
child.resize(num);
for (size_t i = 0; i < num; ++i) for (auto& e : edge[i]) if (depth[i] < depth[e.first]) child[i].push_back(e);
}
//O(NlogN) after makeDepth and makeParent
void makeAncestor(void) {
ancestor.resize(num);
for (size_t i = 0; i < num; ++i) ancestor[i][0] = (parent[i].first!=num?parent[i]:make_pair(i,Op.unitLca));
for (size_t j = 1; j < Operator::bit; ++j) {
for (size_t i = 0; i < num; ++i) {
size_t k = ancestor[i][j - 1].first;
ancestor[i][j] = Op.funcLca(ancestor[k][j - 1],ancestor[i][j - 1]);
}
}
}
//O(logN) after makeAncestor
//return {lca,lca_dist} l and r must be connected
pair<size_t,typeDist> lca(size_t l, size_t r) {
if (depth[l] < depth[r]) swap(l, r);
int diff = depth[l] - depth[r];
auto ancl = make_pair(l,Op.unitLca);
auto ancr = make_pair(r,Op.unitLca);
for (int j = 0; j < Operator::bit; ++j) {
if (diff & (1 << j)) {
ancl = Op.funcLca(ancestor[ancl.first][j],ancl);
}
}
if(ancl.first==ancr.first) return ancl;
for (int j = Operator::bit - 1; 0 <= j; --j) {
if(ancestor[ancl.first][j].first!=ancestor[ancr.first][j].first) {
ancl = Op.funcLca(ancestor[ancl.first][j],ancl);
ancr = Op.funcLca(ancestor[ancr.first][j],ancr);
}
}
ancl = Op.funcLca(ancestor[ancl.first][0],ancl);
ancr = Op.funcLca(ancestor[ancr.first][0],ancr);
return Op.funcLca(ancl,ancr);
}
//O(N) anytime
int diameter(void){
makeDepth(0);
int tmp = max_element(depth.begin(), depth.end()) - depth.begin();
makeDepth(tmp);
return *max_element(depth.begin(), depth.end());
}
//O(N^2) after makeDepth (include self)
void makeDescendant(void) {
descendant.resize(num);
for (size_t i = 0; i < num; ++i) descendant[i].push_back(i);
for (size_t i = 0; i < num; ++i) for (auto& e : edge[order[i]]) if (depth[order[i]] < depth[e.first]) for(auto k: descendant[e.first]) descendant[order[i]].push_back(k);
}
//O(N) after makeChild
void makeSize(void) {
size.resize(num,1);
for (size_t i:order) for (auto e : child[i]) size[i] += size[e.first];
}
};
template<class typeDist> struct treeOperator{
static const size_t bit = 20;
typeDist unitDist = 0;
typeDist unitLca = 0;
typeDist funcDist(const typeDist& parent,const typeDist& w){return parent+w;}
pair<size_t,typeDist> funcLca(const pair<size_t,typeDist>& l,const pair<size_t,typeDist>& r){return make_pair(l.first,max(l.second,r.second));}
};
//depth,dist
//https://atcoder.jp/contests/abc126/tasks/abc126_d
//child
//https://atcoder.jp/contests/abc133/tasks/abc133_e
//lca
//https://atcoder.jp/contests/abc014/tasks/abc014_4
//weighted lca
//https://atcoder.jp/contests/code-thanks-festival-2017-open/tasks/code_thanks_festival_2017_h
//https://atcoder.jp/contests/cf16-tournament-round1-open/tasks/asaporo_c
//diameter
//https://atcoder.jp/contests/agc033/tasks/agc033_c
//descendant
//https://atcoder.jp/contests/code-thanks-festival-2018/tasks/code_thanks_festival_2018_f
//size
//
//eulerTour
//https://yukicoder.me/problems/no/900
// Tree<treeOperator<int>> tree(N);
int main() {
int N;
cin >> N;
Tree<treeOperator<ll>> tree(N);
for(int i = 0; i < N-1; ++i){
int u,v,w;
cin >> u >> v >> w;
u--,v--;
tree.makeEdge(u,v,w);
tree.makeEdge(v,u,w);
}
tree.makeDepth(0);
tree.makeChild();
tree.makeSize();
ll ans = 0;
for(int pa:tree.order) for(auto e:tree.child[pa]) ans += e.second*tree.size[e.first]*(N-tree.size[e.first])*2LL;
cout << ans << endl;
return 0; return 0;
}