Submission #299852

# Submission time Handle Problem Language Result Execution time Memory
299852 2020-09-15T20:52:36 Z caoash Transport (COCI19_transport) C++14
Compilation error
0 ms 0 KB
#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);
        }
    }
}

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);
    }
}

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]);
    int rem = vals[v].order_of_key(mp(dist[v], INT_MIN));
    for (int i = 0; i < rem; i++) {
        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) {
    for (int i = 0; i < n; i++) dist[i] = 0;
    for (int i = 0; i < n; i++) mdist[i] = INF;
    mdist[0] = 0;
    for (int i = 0; i < n; i++) vals[i].clear();
    dfs(root, -1);
    dfs2(root, -1);
    Tree<pair<ll, int>> &fin = vals[root];
    // dbg(fin);
    ans += sz(fin);
    for (pi to : adj[root]) {
        if (blocked[to.f]) continue;
        dfs3(to.f, root);
        vector<pi> todo;
        for (int x : order) {
            int ind = fin.order_of_key(mp(dist[x], x));
            pair<ll, int> cval = *fin.find_by_order(ind);
            if (cval.f == dist[x] && cval.s == x) {
                fin.erase(mp(dist[x], x));     
                // dbg("REMOVING", dist[x], x);
                todo.pb(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 (pair<ll, int> x : todo) {
            // dbg("ADDING BACK: ", x);
            fin.insert(x); 
        }
        order.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;
    }
}

void go(int root) {
    dfs4(root, -1);
    int cc = find(root, -1);
    solve(cc);
    // 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);
    go(0);
    ps(ans);
} 

Compilation message

transport.cpp: In function 'int main()':
transport.cpp:227:5: error: 'ps' was not declared in this scope; did you mean 'output::ps'?
  227 |     ps(ans);
      |     ^~
      |     output::ps
transport.cpp:53:41: note: 'output::ps' declared here
   53 |     template<class T, class... Ts> void ps(const T& t, const Ts&... ts) {
      |                                         ^~
transport.cpp: In function 'int find(int, int)':
transport.cpp:200:1: warning: control reaches end of non-void function [-Wreturn-type]
  200 | }
      | ^