結果
| 問題 | No.1843 Tree ANDistance |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-02-16 22:54:54 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 917 ms / 2,000 ms |
| コード長 | 8,375 bytes |
| コンパイル時間 | 7,235 ms |
| コンパイル使用メモリ | 333,512 KB |
| 実行使用メモリ | 34,648 KB |
| 最終ジャッジ日時 | 2025-02-16 22:55:17 |
| 合計ジャッジ時間 | 22,176 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 38 |
ソースコード
#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
namespace my{
using ml=atcoder::modint1000000007;
auto&operator>>(istream&i,ml&x){int t;i>>t;x=t;return i;}
auto&operator<<(ostream&o,const ml&x){return o<<(int)x.val();}
#define eb emplace_back
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define VV(n,...) vec<ll>__VA_ARGS__;setsize({n},__VA_ARGS__);vin(__VA_ARGS__)
#define FO(n) for(ll ij=n;ij-->0;)
#define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define fo(i,...) FO##__VA_OPT__(R)(i __VA_OPT__(,__VA_ARGS__))
#define fe(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):a)
#define single_testcase void solve();}int main(){my::io();my::solve();}namespace my{
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ll=long long;
constexpr auto range(bool s,auto...a){array<ll,3>r{0,0,1};ll I=0;((r[I++]=a),...);if(!s&&I==1)swap(r[0],r[1]);r[0]-=s;if(s)r[2]*=-1;return r;}
constexpr char newline=10;
constexpr char space=32;
constexpr ll width2(auto x){x|=1;ll r=0;while(x>0)x>>=1,++r;return r;}
bool amax(auto&a,const auto&b){return a<b?a=b,1:0;}
template<class F=less<>>auto&sort(auto&a,F f={}){ranges::sort(a,f);return a;}
auto&unique(auto&a){sort(a).erase(ranges::unique(a).begin(),a.end());return a;}
template<class T>struct queue:std::queue<T>{
queue(const initializer_list<T>&a={}){fe(a,e)this->emplace(e);}
queue(const vector<T>&a){fe(a,e)this->emplace(e);}
ll size()const{return std::queue<T>::size();}
T pop(){T r=this->front();std::queue<T>::pop();return r;}
friend ostream&operator<<(ostream&o,queue q){while(q.size())o<<q.pop()<<string(q.size()>0,space);return o;}
};
template<ll k>auto pack_kth(const auto&...a){return get<k>(make_tuple(a...));}
template<class T,ll n>auto pack_slice(const auto&...a){return[&]<size_t...I>(index_sequence<I...>){return array<T,n>{get<I>(forward_as_tuple(a...))...};}(make_index_sequence<n>{});}
template<class V>concept vectorial=is_base_of_v<vector<typename V::value_type>,V>;
template<class T>struct vec_attr{using core_type=T;static constexpr int rank=0;};
template<vectorial V>struct vec_attr<V>{using core_type=typename vec_attr<typename V::value_type>::core_type;static constexpr int rank=vec_attr<typename V::value_type>::rank+1;};
template<class T>using core_t=vec_attr<T>::core_type;
template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;}
template<class V>ostream&operator<<(ostream&o,const vector<V>&v){fe(v,e)o<<e<<string(&e!=&v.back(),vectorial<V>?newline:space);return o;}
template<class V>struct vec;
template<ll rank,class T>struct tensor_helper{using type=vec<typename tensor_helper<rank-1,T>::type>;};
template<class T>struct tensor_helper<0,T>{using type=T;};
template<ll rank,class T>using tensor=typename tensor_helper<rank,T>::type;
template<class V>struct vec:vector<V>{
using vector<V>::vector;
vec(const vector<V>&v){vector<V>::operator=(v);}
template<class...A>requires(sizeof...(A)>=3)vec(A...a){const ll n=sizeof...(a)-1;auto t=pack_slice<ll,n>(a...);ll s[n];fo(i,n)s[i]=t[i];*this=make_vec(s,pack_kth<n>(a...));}
template<class T,ll n,ll i=0>static auto make_vec(const ll(&s)[n],T x){if constexpr(i==n-1)return vec<T>(s[i],x);else{auto X=make_vec<T,n,i+1>(s,x);return vec<decltype(X)>(s[i],X);}}
vec&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;}
vec operator^(const vec&u)const{return vec{*this}^=u;}
vec&operator+=(const vec&u){vec&v=*this;assert(v.size()==u.size());fo(i,v.size())v[i]+=u[i];return v;}
vec&operator-=(const vec&u){vec&v=*this;assert(v.size()==u.size());fo(i,v.size())v[i]-=u[i];return v;}
vec operator+(const vec&u)const{return vec{*this}+=u;}
vec operator-(const vec&u)const{return vec{*this}-=u;}
vec&operator++(){fe(*this,e)++e;return*this;}
vec&operator--(){fe(*this,e)--e;return*this;}
vec operator-()const{vec v=*this;fe(v,e)e=-e;return v;}
vec&operator%=(auto M){vec&v=*this;fe(v,e)e%=M;return v;}
vec operator%(auto M)const{return vec{*this}%=M;}
ll size()const{return vector<V>::size();}
auto pop_front(){auto r=*this->begin();this->erase(this->begin());return r;}
template<class F=less<>>auto sort(F f={})const{vec v=*this;ranges::sort(v,f);return v;}
auto unique()const{auto r=sort();r.erase(ranges::unique(r).begin(),r.end());return r;}
};
template<class...A>requires(sizeof...(A)>=2)vec(A...a)->vec<tensor<sizeof...(a)-2,remove_reference_t<decltype(get<sizeof...(a)-1>(declval<tuple<A...>>()))>>>;
vec(ll)->vec<ll>;
template<ll n,class...A>void setsize(const ll(&l)[n],A&...a){((a=vec<void*>::make_vec(l,core_t<A>{})),...);}
void lin(auto&...a){(cin>>...>>a);}
void vin(auto&...a){fo(i,(a.size()&...))(cin>>...>>a[i]);}
void dec(auto&...a){((--a),...);}
template<class T>struct pow_linear:vec<T>{pow_linear(ll a,ll n):vec<T>(n+1,1){fo(i,n)(*this)[i+1]=(*this)[i]*a;}};
ll pow2ll(ll n){static const auto v=pow_linear<ll>(2,(ll)sizeof(ll)*CHAR_BIT-1);return v[n];}
constexpr ll at2ll(auto x,ll i){return x>>i&1;}
void sort(auto&...a){vec<common_type_t<decltype(a)...>>v;(v.eb(a),...);sort(v);ll I=0;((a=v[I++]),...);}
struct dsu{
ll n;
vec<ll>par_or_size;
dsu(ll n):n(n),par_or_size(n,-1){}
ll merge(ll a,ll b){
ll x=leader(a),y=leader(b);
if(x==y)return x;
if(-par_or_size[x]<-par_or_size[y])swap(x,y);
par_or_size[x]+=par_or_size[y];
par_or_size[y]=x;
return x;
}
ll leader(ll a){
assert(0<=a&&a<n);
if(par_or_size[a]<0)return a;
return par_or_size[a]=leader(par_or_size[a]);
}
ll size(ll a){return-par_or_size[leader(a)];}
auto groups(){
vec<vec<ll>>res(n);
fo(u,n)res[leader(u)].eb(u);
fe(res,e)ranges::sort(e);
unique(res);
if(res[0].empty())res.pop_front();
return res;
}
};
template<class WT>struct edge{
ll from,to;
WT wt;
ll id;
edge()=default;
edge(ll from,ll to,WT wt=1,ll id=-1):from(from),to(to),wt(wt),id(id){}
auto operator<=>(const edge&e)const{return wt<=>e.wt;}
friend ostream&operator<<(ostream&o,const edge&e){return o<<"(to "<<e.to<<", wt "<<e.wt<<",id "<<e.id<<")";}
};
template<class WT>struct graph{
vec<vec<edge<WT>>>edges;
graph()=default;
graph(ll n):edges(n){}
decltype(auto)operator[](ll i){return edges[i];}
decltype(auto)operator[](ll i)const{return edges[i];}
ll size()const{return edges.size();}
friend ostream&operator<<(ostream&o,const graph&g){
fo(u,g.size()){
o<<"from "<<u<<": ";
fo(j,g[u].size())o<<g[u][j]<<string(j!=g[u].size()-1,space);
o<<newline;
}
return o;
}
void add_edges(const vec<ll>&p){fo(i,p.size())if(p[i]!=-1)edges[p[i]].eb(p[i],i,1,i);}
void add_edges(const vec<ll>&a,const vec<ll>&b){fo(i,a.size())edges[a[i]].eb(a[i],b[i],1,i);}
void add_edges(const vec<ll>&a,const vec<ll>&b,const vec<WT>&w){fo(i,a.size())edges[a[i]].eb(a[i],b[i],w[i],i);}
void add_edges(const vec<edge<WT>>&es){fe(es,e)edges[e.from].eb(e);}
};
template<class WT>struct rooted_tree:graph<WT>{
vec<edge<WT>>rev_edge;
ll r;
rooted_tree(ll n,ll r):graph<WT>(n),rev_edge(n,{-1,-1,WT(),-1}),r(r){}
ll root()const{return r;}
void add_edge_and_set_rev_edge(const edge<WT>&e){
this->edges[e.from].eb(e);
this->rev_edge[e.to]=e;
}
};
template<class T,class WT>auto sum_pair_and_tree_dist(const rooted_tree<WT>&g){
ll n=g.size();
WT wmax=0;
fo(u,n)fe(g[u],e)amax(wmax,e.wt);
T res=0;
fo(b,width2(wmax)){
dsu uf(n);
fo(u,n)fe(g[u],e)if(at2ll(e.wt,b))uf.merge(u,e.to);
T t=0;
fe(uf.groups(),v)t+=v.size()*(v.size()-1)/2;
res+=t*pow2ll(b);
}
return res;
}
template<class WT>auto bfs_tree(const graph<WT>&g,ll s){
rooted_tree<WT>tree(g.size(),s);
vec<bool>used(g.size());
queue<edge<WT>>q{{-1,s,WT{},-1}};
while(q.size()){
auto pu=q.pop();
ll u=pu.to;
if(used[u])continue;
used[u]=1;
if(pu.from!=-1)tree.add_edge_and_set_rev_edge(pu);
fe(g[u],e)if(!used[e.to])q.emplace(e);
}
return tree;
}
template<class WT>auto bfs_tree(const rooted_tree<WT>&g){return bfs_tree(g,g.root());}
void pp(auto&&...a){ll n=sizeof...(a);((cout<<a<<string(--n>0,space)),...);cout<<newline;}
single_testcase
void solve(){
LL(N);
VV(N-1,a,b,c);dec(a,b);
rooted_tree<ll>g(N,0);
g.add_edges(a,b,c);
g.add_edges(b,a,c);
g=bfs_tree(g);
pp(sum_pair_and_tree_dist<ml>(g));
}}