Submission #299862

#TimeUsernameProblemLanguageResultExecution timeMemory
299862caoashTransport (COCI19_transport)C++14
13 / 130
1097 ms38000 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vl = vector<ll>; #define pb push_back #define rsz resize #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() using pi = pair<int,int>; #define f first #define s second #define mp make_pair const int MX = 200005; const int MOD = (int) (1e9 + 7); const ll INF = (ll) 1e18; namespace output { void pr(int x) { cout << x; } void pr(long x) { cout << x; } void pr(ll x) { cout << x; } void pr(unsigned x) { cout << x; } void pr(unsigned long x) { cout << x; } void pr(unsigned long long x) { cout << x; } void pr(float x) { cout << x; } void pr(double x) { cout << x; } void pr(long double x) { cout << x; } void pr(char x) { cout << x; } void pr(const char* x) { cout << x; } void pr(const string& x) { cout << x; } void pr(bool x) { pr(x ? "true" : "false"); } template<class T1, class T2> void pr(const pair<T1,T2>& x); template<class T> void pr(const T& x); template<class T, class... Ts> void pr(const T& t, const Ts&... ts) { pr(t); pr(ts...); } template<class T1, class T2> void pr(const pair<T1,T2>& x) { pr("{",x.f,", ",x.s,"}"); } template<class T> void pr(const T& x) { pr("{"); // const iterator needed for vector<bool> bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; pr("}"); } void ps() { pr("\n"); } // print w/ spaces template<class T, class... Ts> void ps(const T& t, const Ts&... ts) { pr(t); if (sizeof...(ts)) pr(" "); ps(ts...); } void pc() { cout << "]" << endl; } // debug w/ commas template<class T, class... Ts> void pc(const T& t, const Ts&... ts) { pr(t); if (sizeof...(ts)) pr(", "); pc(ts...); } #define dbg(x...) pr("[",#x,"] = ["), pc(x); } #ifdef LOCAL using namespace output; #endif #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int n; ll a[MX]; bool blocked[MX]; vector<pair<ll, int>> adj[MX]; ll ans = 0; Tree<pair<ll, int>> vals[MX]; ll dist[MX]; ll mdist[MX]; int sz[MX]; void dfs(int v, int p) { if (p != -1 && mdist[v] >= 0) ++ans; dist[v] += a[v]; for (pi to : adj[v]) { if (blocked[to.f]) continue; if (to.f != p) { dist[to.f] = dist[v] - to.s; mdist[to.f] = min(mdist[v], dist[to.f]); dfs(to.f, v); } } } ll need = 0; void comb(Tree<pair<ll, int>> &a, Tree<pair<ll, int>> &b) { if (sz(a) < sz(b)) swap(a, b); for (auto x : b) { a.insert(x); ++need; } b.clear(); } void dfs2(int v, int p) { if (p != -1) vals[v].insert(mp(dist[v], v)); for (pi to : adj[v]) { if (blocked[to.f]) continue; if (to.f != p) { dfs2(to.f, v); comb(vals[v], vals[to.f]); } } // dbg(v, vals[v]); // dbg(sz(vals[v])); int rem = vals[v].order_of_key(mp(dist[v], INT_MIN)); for (int i = 0; i < rem; i++) { ++need; vals[v].erase(vals[v].begin()); } // dbg(v, vals[v]); } vi order; void dfs3(int v, int p) { for (pi to : adj[v]) { if (blocked[to.f]) continue; if (to.f != p) { dfs3(to.f, v); } } order.pb(v); } void solve(int root) { dfs3(root, -1); for (int x : order) mdist[x] = INF; mdist[root] = 0; dist[root] = 0; dfs(root, -1); dfs2(root, -1); Tree<pair<ll, int>> &fin = vals[root]; vector<bool> ok(n); for (auto &x : fin) { ok[x.s] = true; } // dbg(fin); ans += sz(fin); order.clear(); for (pi to : adj[root]) { if (blocked[to.f]) continue; dfs3(to.f, root); for (int x : order) { if (ok[x]) { fin.erase(mp(dist[x], x)); } } // dbg(to.f, fin); for (int x : order) { ll cdist = mdist[x]; // dbg(x, cdist); // dbg(x, -cdist + a[root]); // dbg(ans); ans += sz(fin) - fin.order_of_key(mp(-cdist + a[root], INT_MIN)); // dbg(ans); } for (int x : order) { if(ok[x]) fin.insert(mp(dist[x], x)); } order.clear(); } fin.clear(); // ps(ans); } void dfs4(int v, int p) { sz[v] = 1; for (pi to : adj[v]) { if (blocked[to.f]) continue; if (to.f != p) { dfs4(to.f, v); sz[v] += sz[to.f]; } } } int find(int v, int p) { bool found = false; for (pi to : adj[v]) { if (blocked[to.f]) continue; if (to.f != p) { if (sz[to.f] > n / 2) { found = true; return find(to.f, v); } } } if (!found) { return v; } return -1; } void go(int root) { dfs4(root, -1); int cc = find(root, -1); // dbg(root, cc); solve(cc); // dbg(cc, need); // dbg(cc, ans); blocked[cc] = true; for (pi to : adj[cc]) { if (!blocked[to.f]) { go(to.f); } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; u--, v--; adj[u].pb(mp(v, w)), adj[v].pb(mp(u, w)); } // solve(1); // dbg(need); go(0); cout << ans << '\n'; }
#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...