Submission #480397

#TimeUsernameProblemLanguageResultExecution timeMemory
480397bonopoTransport (COCI19_transport)C++17
0 / 130
328 ms16880 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define pb push_back #define rc (i<<1)|1 #define lc (i<<1) #define s second #define f first #define el "\n" #define int long long typedef long long ll; const int MM=1e5+5, MOD=1e9+7, LOG=19; int N, sz[MM], bk[MM], g[MM]; ll ans; vector<pair<int,int>> adj[MM]; vector<ll> vdw, vup; void dfs_up(int u, int pa, ll gc, ll lo) { if(lo>=0) ++ans, vup.pb(lo); for(auto &e:adj[u]) if(e.f!=pa&&!bk[e.f]) { dfs_up(e.f, u, gc+g[e.f]-e.s, min(lo+g[e.f]-e.s, g[e.f]-e.s)); } } void dfs_dw(int u, int pa, ll gc, ll lo) { if(lo>=0) ++ans; vdw.pb(-lo); for(auto &e:adj[u]) if(e.f!=pa&&!bk[e.f]) { dfs_dw(e.f, u, gc+g[u]-e.s, min(lo, gc+g[e.f]-e.s)); } } inline void solve(int rt) { vector<ll> gdw, gup; for(auto &e:adj[rt]) if(!bk[e.f]) { vdw.clear(); vup.clear(); dfs_dw(e.f, rt, g[rt]-e.s, g[rt]-e.s); dfs_up(e.f, rt, g[e.f]-e.s, g[e.f]-e.s); sort(begin(vup), end(vup)); sort(begin(vdw), end(vdw)); for(ll &x:vdw) gdw.pb(x); for(ll &x:vup) gup.pb(x); for(int i=0, j=0, sz=vup.size(); i<vdw.size(); i++) { while(j<sz&&vup[j]<vdw[i]) ++j; ans-=sz-j; } } sort(begin(gup), end(gup)); sort(begin(gdw), end(gdw)); for(int i=0, j=0, sz=gup.size(); i<gdw.size(); i++) { while(j<sz&&gup[j]<gdw[i]) ++j; ans+=sz-j; } // rt to rt } void gsz(int u, int pa) { sz[u]=1; for(auto &e:adj[u]) if(e.f!=pa&&!bk[e.f]) { gsz(e.f, u); sz[u]+=sz[e.f]; } } int crt(int u, int th, int pa) { for(auto &e:adj[u]) if(e.f!=pa&&!bk[e.f]) { if(sz[e.f]>th) return crt(e.f, th, u); } return u; } void rec(int u, int nd) { if(nd==1) return; gsz(u, 0); int ct=crt(u, nd/2, 0); bk[ct]=1; solve(ct); gsz(ct, 0); for(auto &e:adj[ct]) if(!bk[e.f]) rec(e.f, sz[e.f]); } int32_t main() { cin.tie(nullptr)->sync_with_stdio(false); cin>>N; for(int i=1; i<=N; i++) cin>>g[i]; for(int i=1; i<N; i++) { int u, v, w; cin>>u>>v>>w; adj[u].pb({v,w}); adj[v].pb({u,w}); } rec(1, N); cout<<ans<<el; } // MM

Compilation message (stderr)

transport.cpp: In function 'void solve(long long int)':
transport.cpp:44:43: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |         for(int i=0, j=0, sz=vup.size(); i<vdw.size(); i++) {
      |                                          ~^~~~~~~~~~~
transport.cpp:51:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(int i=0, j=0, sz=gup.size(); i<gdw.size(); i++) {
      |                                      ~^~~~~~~~~~~
transport.cpp: In function 'void rec(long long int, long long int)':
transport.cpp:72:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   72 |     if(nd==1) return; gsz(u, 0);
      |     ^~
transport.cpp:72:23: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   72 |     if(nd==1) return; gsz(u, 0);
      |                       ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...