이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=a; i<b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"Pedro Is The Master "<<i<<endl
#define INF 5000000000000000000LL
template<class A=ll>
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}
template<class A=ll>
void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}}
struct hash_pair
{
template <class T1, class T2>
size_t operator() (pair<T1, T2> p) const
{
size_t hash1 = hash<T1>()(p.first);
size_t hash2 = hash<T2>()(p.second);
return (hash1 ^ hash2);
}
};
template<class T=ll>
class SparseTable //Range Minimum Queries
{
public:
ll N;
vector<T> a;
vector<vector<T> > v;
SparseTable() {N=0LL;}
SparseTable(vector<T> b)
{
a=b; N=a.size();
ll lo=(ll) floor((double) log2(N)) +1LL;
vector<T> xx;
REP(i,0,lo) {xx.pb(mp(INF,INF));} REP(i,0,N) {v.pb(xx);}
REP(step,0LL,lo)
{
REP(i,0,N-(1LL<<step)+1LL)
{
if(step==0) {v[i][0]=a[i];}
else {v[i][step]=min(v[i][step-1],v[i+(1LL<<(step-1))][step-1]);}
}
}
}
T query(ll l, ll r)
{
ll step=(ll) floor((double) log2(r-l+1LL));
return min(v[l][step],v[r-(1LL<<step)+1LL][step]);
}
};
class SucPath
{
public:
ll N;
vector<ll> fo;
vector<vector<ll> > f2; //sparse table of steps powers of 2
ll ms; //max_steps
SucPath() {N=0LL;}
SucPath(vector<ll> x, ll max_steps)
{
N=x.size(); fo=x; ms=max_steps;
vector<ll> xx;
REP(i,0,(ll) (floor(log2(ms)))+1LL) {xx.pb(0LL);}
REP(i,0,N) {f2.pb(xx);}
Conf2(0);
}
void Conf2(ll e) //O(NlogN)
{
if((1LL<<e)>ms) {return;}
if(e==0) {REP(i,0,N) {f2[i][e]=fo[i];} Conf2(e+1);}
else
{
REP(i,0,N)
{
f2[i][e]=f2[f2[i][e-1]][e-1];
}
}
Conf2(e+1);
}
ll f(ll x,ll s) //O(logN)
{
ll ind=0;
while(s>0)
{
if(s%2!=0) {x=f2[x][ind];}
s/=2; ind++;
}
return x;
}
pl cycle() //Floyd's Algorithm, O(N) time, O(1) memory, return <element of cycle,length od cycle>
{
ll a=fo[0]; ll b=fo[fo[0]];
while(a!=b) {a=fo[a]; b=fo[fo[b]];}
ll l=1; b=fo[a];
while(b!=a) {b=fo[b]; l++;}
return mp(a,l);
}
};
class ST
{
public:
ll N;
class SV //seg value
{
public:
ll a;
SV() {a=0LL;}
SV(ll x) {a=x;}
SV operator & (SV X) {SV ANS(a+X.a); return ANS;}
};
class LV //lazy value
{
public:
ll a;
LV() {a=0LL;}
LV(ll x) {a=x;}
LV operator & (LV X) {LV ANS(a+X.a); return ANS;}
};
SV upval(ll c) //how lazy values modify a seg value inside a node, c=current node
{
SV X(p[c].a+(range[c].ss-range[c].ff+1)*lazy[c].a);
return X;
}
SV neuts; LV neutl;
vector<SV> ar;
vector<SV> p;
vector<LV> lazy;
vector<pl> range;
ST() {N=0LL;}
ST(vector<ll> arr)
{
N = (ll) 1<<(ll) ceil(log2(arr.size()));
REP(i,0,2*N) {range.pb(mp(0LL,0LL));}
REP(i,0,arr.size()) {SV X(arr[i]); ar.pb(X);}
REP(i,arr.size(),N) {ar.pb(neuts);}
REP(i,0,N) {p.pb(neuts);}
REP(i,0,N) {p.pb(ar[i]); range[i+N]=mp(i,i);}
ll cur = N-1;
while(cur>0)
{
p[cur]=p[2*cur]&p[2*cur+1];
range[cur]=mp(range[2*cur].ff,range[2*cur+1].ss);
cur--;
}
REP(i,0,2*N) {lazy.pb(neutl);}
}
void prop(ll c) //how lazy values propagate
{
p[c]=upval(c);
lazy[2*c]=lazy[c]&lazy[2*c]; lazy[2*c+1]=lazy[c]&lazy[2*c+1];
lazy[c]=neutl;
if(2*c>=N)
{
p[2*c]=upval(2*c); p[2*c+1]=upval(2*c+1);
ar[2*c-N]=p[2*c];
ar[2*c+1-N]=p[2*c+1];
lazy[2*c]=neutl; lazy[2*c+1]=neutl;
}
}
SV query(ll a,ll b, ll c=1LL) //range [a,b], current node. initially: query(a,b,1)
{
ll x=range[c].ff; ll y=range[c].ss;
if(y<a || x>b) {return neuts;}
if(x>=a && y<=b) {return upval(c);}
prop(c);
SV ans = query(a,b,2*c)&query(a,b,2*c+1);
return ans;
}
void update(LV s, ll a, ll b, ll c=1LL) //update LV, range [a,b], current node, current range. initially: update(s,a,b,1,0,S.N-1)
{
ll x=range[c].ff; ll y=range[c].ss;
if(y<a || x>b) {return ;}
if(x>=a && y<=b)
{
lazy[c]=s&lazy[c];
if(c>=N) {p[c]=upval(c); lazy[c]=neutl; ar[c-N]=p[c];}
return;
}
update(s,a,b,2*c); update(s,a,b,2*c+1);
p[c]=upval(2*c)&upval(2*c+1);
}
};
class Tree
{
public:
ll N;
vector<ll> p;
vector<vector<ll> > sons;
vector<vector<ll> > adj;
ll root;
vector<bool> visited;
vector<ll> level; //starting in 0
vector<ll> sub; //number of nodes in subtree
vector<ll> val; //node values
vector<ll> DFSarr1; //DFS Array
vector<ll> DFSarr2; //DFS Array for LCA with whole path
vector<ll> pos; //inverted DFSArr, only for LCA
vector<pl> levDFSarr; //array of levels on DFSarr, only needed for LCA
vector<ll> sumto; //weighted graph, length of path root-->i
SparseTable<pl> S; //for LCA
SucPath P; //for function f
ll max_steps; //for function f
Tree(vector<vector<ll> > ad, ll r=0LL)
{
N=ad.size(); root=r; adj=ad;
REP(i,0,N) {visited.pb(false);}
vector<ll> xx; REP(i,0,N) {sons.pb(xx); p.pb(-1); level.pb(0); sub.pb(1LL); pos.pb(0LL); sumto.pb(0LL);}
DFS_Build(r,r);
REP(i,0,DFSarr2.size()) {pos[DFSarr2[i]]=i;}
REP(i,0,DFSarr2.size()) {levDFSarr.pb(mp(level[DFSarr2[i]],DFSarr2[i]));}
SparseTable<pl> X(levDFSarr); S=X;
max_steps=N;
SucPath Y(p,N); P=Y;
REP(i,0,N) {val.pb(0LL);}
}
void Reset()
{
REP(i,0,N) {visited[i]=false;}
}
void DFS_Build(ll s, ll par)
{
DFSarr1.pb(s);
DFSarr2.pb(s);
if(s!=root) {level[s]=level[par]+1LL;}
p[s]=par;
visited[s]=true;
REP(i,0,adj[s].size())
{
if(adj[s][i]==par) {continue;}
sons[s].pb(adj[s][i]);
DFS_Build(adj[s][i],s);
sub[s]+=sub[adj[s][i]];
DFSarr2.pb(s);
}
return;
}
void DFS(ll s, ll las=-1LL)
{
REP(i,0,adj[s].size())
{
if(adj[s][i]==las) {continue;}
DFS(adj[s][i],s);
}
}
ll LCA(ll a, ll b)
{
a=pos[a]; b=pos[b];
ll l=min(a,b); ll r=max(a,b);
pl ans=S.query(l,r);
return ans.ss;
}
ll f(ll x, ll k)
{
return P.f(x,k);
}
class HeavyPath
{
public:
ll N;
ll low, high;
Tree *T;
ST S;
HeavyPath() {N=0LL;}
HeavyPath(ll x, ll y, Tree *K)
{
T=K;
low=x; high=y;
if(T->level[x]<T->level[y]) {swap(high,low);}
N = T->level[x]-T->level[y]+1LL;
vector<ll> st_val; ll c = low;
while(1>0) {st_val.pb(T->val[c]); if(c==high) {break;} c=T->p[c];}
ST R(st_val); S=R;
}
ll pos(ll x)
{
return (T->level[low]-T->level[x]);
}
ST::SV query(ll ind1, ll ind2)
{
return S.query(ind1,ind2);
}
void update(ST::LV X, ll ind1, ll ind2)
{
S.update(X,ind1,ind2);
}
};
vector<HeavyPath *> h_path; //heavy paths
unordered_map<ll,HeavyPath *> HP; //m[s] = heavy path including s
void HLD()
{
vector<bool> lowest; ll c;
REP(i,0,N)
{
bool x = true;
REP(j,0,sons[i].size())
{
c=sons[i][j];
if(2*sub[c]>=sub[i]) {x=false;}
}
lowest.pb(x);
}
REP(i,0,N)
{
if(!lowest[i]) {continue;}
c=i;
while(c!=root && 2*sub[c]>=sub[p[c]]) {c=p[c];}
HeavyPath *P = new HeavyPath(i,c,this);
c=i; while(1>0) {HP[c]=P; if(c==P->high) {break;} c=p[c];}
h_path.pb(P);
}
}
ST::SV query_ancestor(ll s, ll anc)
{
ST::SV ans;
if(level[s]<level[anc]) {return ans;}
ll c = s;
while(1>0)
{
ST::SV thispath;
if(level[HP[c]->high]>=level[anc])
{
thispath = HP[c]->query(HP[c]->pos(c),HP[c]->N-1);
}
else
{
thispath = HP[c]->query(HP[c]->pos(c),HP[c]->pos(anc));
}
ans=ans&thispath;
c=HP[c]->high; c=p[c];
if(c==root || level[c]<level[anc]) {break;}
}
return ans;
}
ST::SV query(ll a, ll b) //query along path a->b
{
ll lca = LCA(a,b);
ST::SV V1 = query_ancestor(a,lca);
ST::SV V2; if(b!=lca) {V2= query_ancestor(b,f(b,level[b]-level[lca]-1LL));}
return V1&V2;
}
void update_ancestor(ST::LV X, ll s, ll anc)
{
if(level[s]<level[anc]) {return;}
ll c = s;
while(1>0)
{
if(level[HP[c]->high]>=level[anc])
{
HP[c]->update(X,HP[c]->pos(c),HP[c]->N-1);
}
else
{
HP[c]->update(X,HP[c]->pos(c),HP[c]->pos(anc));
}
c=HP[c]->high; if(c==root) {break;} c=p[c];
if(level[c]<level[anc]) {break;}
}
}
void update(ST::LV X, ll a, ll b)
{
ll lca = LCA(a,b);
if(a!=lca) {update_ancestor(X,a,f(a,level[a]-level[lca]-1LL));}
if(b!=lca) {update_ancestor(X,b,f(b,level[b]-level[lca]-1LL));}
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll N; cin>>N; vector<ll> xx; vector<vector<ll> > adj; REP(i,0,N) {adj.pb(xx);}
pl cur; unordered_map<pl,pl,hash_pair> m;
REP(i,0,N-1)
{
cin>>cur.ff>>cur.ss; cur.ff--; cur.ss--;
adj[cur.ff].pb(cur.ss); adj[cur.ss].pb(cur.ff);
ll S,M; cin>>S>>M;
m[mp(cur.ff,cur.ss)]=mp(S,M);
m[mp(cur.ss,cur.ff)]=mp(S,M);
}
Tree T(adj,0);
T.HLD();
REP(i,0,N-1)
{
ST::LV X(1LL);
T.update(X,i,i+1);
}
vector<ll> use;
REP(i,0,N) {if(i==0) {use.pb(0);} else{use.pb(T.query(i,i).a);}}
ll ans=0LL;
REP(i,1,N)
{
pl P = m[mp(i,T.p[i])];
ans+=min(use[i]*P.ff,P.ss);
}
cout<<ans<<endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
putovanje.cpp: In constructor 'ST::ST(std::vector<int>)':
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
175 | REP(i,0,arr.size()) {SV X(arr[i]); ar.pb(X);}
| ~~~~~~~~~~~~~~
putovanje.cpp:175:9: note: in expansion of macro 'REP'
175 | REP(i,0,arr.size()) {SV X(arr[i]); ar.pb(X);}
| ^~~
putovanje.cpp: In constructor 'Tree::Tree(std::vector<std::vector<int> >, ll)':
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
255 | REP(i,0,DFSarr2.size()) {pos[DFSarr2[i]]=i;}
| ~~~~~~~~~~~~~~~~~~
putovanje.cpp:255:9: note: in expansion of macro 'REP'
255 | REP(i,0,DFSarr2.size()) {pos[DFSarr2[i]]=i;}
| ^~~
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
256 | REP(i,0,DFSarr2.size()) {levDFSarr.pb(mp(level[DFSarr2[i]],DFSarr2[i]));}
| ~~~~~~~~~~~~~~~~~~
putovanje.cpp:256:9: note: in expansion of macro 'REP'
256 | REP(i,0,DFSarr2.size()) {levDFSarr.pb(mp(level[DFSarr2[i]],DFSarr2[i]));}
| ^~~
putovanje.cpp: In member function 'void Tree::DFS_Build(ll, ll)':
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
275 | REP(i,0,adj[s].size())
| ~~~~~~~~~~~~~~~~~
putovanje.cpp:275:9: note: in expansion of macro 'REP'
275 | REP(i,0,adj[s].size())
| ^~~
putovanje.cpp: In member function 'void Tree::DFS(ll, ll)':
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
288 | REP(i,0,adj[s].size())
| ~~~~~~~~~~~~~~~~~
putovanje.cpp:288:9: note: in expansion of macro 'REP'
288 | REP(i,0,adj[s].size())
| ^~~
putovanje.cpp: In member function 'void Tree::HLD()':
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
353 | REP(j,0,sons[i].size())
| ~~~~~~~~~~~~~~~~~~
putovanje.cpp:353:13: note: in expansion of macro 'REP'
353 | REP(j,0,sons[i].size())
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |