Submission #299889

# Submission time Handle Problem Language Result Execution time Memory
299889 2020-09-15T23:30:14 Z caoash Transport (COCI19_transport) C++14
130 / 130
833 ms 21620 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 = 100005;
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;
#else
#define dbg(x...) 4
#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>> fin;
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);
        }
    }
}
 
multiset<ll> curr;

void dfs2(int v, int p) {
    curr.insert(dist[v]);
    for (pi to : adj[v]) {
        if (blocked[to.f]) continue;
        if (to.f != p) {
            dfs2(to.f, v);
        }
    }
    // dbg(v, curr, dist[v]);
    if (dist[v] == (*--curr.end())) {
        if (p != -1) fin.insert(mp(dist[v], v));        
    }
    curr.erase(curr.find(dist[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;
    // dbg(root, sz(order));
    mdist[root] = 0;
    dist[root] = 0;
    dfs(root, -1);
    dfs2(root, -1);
    dbg(root, fin);
    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, int root) {
    // dbg("FINDING", v);
    for (pi to : adj[v]) {
        if (blocked[to.f]) continue;
        if (to.f != p) {
            if (sz[to.f] > sz[root] / 2) {
                return find(to.f, v, root);
            }
        }
    }
    return v;
}
 
void go(int root) {
    dfs4(root, -1);
    int cc = find(root, -1, root);
    // 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(2);
    // dbg(need);
    //dbg("KEKW");
    go(0);
    cout << ans << '\n';
} 

Compilation message

transport.cpp:67: warning: "dbg" redefined
   67 | #define dbg(x...) 4
      | 
transport.cpp:61: note: this is the location of the previous definition
   61 |     #define dbg(x...) pr("[",#x,"] = ["), pc(x);
      | 
transport.cpp: In function 'void solve(int)':
transport.cpp:67:19: warning: statement has no effect [-Wunused-value]
   67 | #define dbg(x...) 4
      |                   ^
transport.cpp:137:5: note: in expansion of macro 'dbg'
  137 |     dbg(root, fin);
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2944 KB Output is correct
2 Correct 10 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 3456 KB Output is correct
2 Correct 21 ms 3584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 315 ms 10356 KB Output is correct
2 Correct 201 ms 10112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 380 ms 13016 KB Output is correct
2 Correct 367 ms 15600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 575 ms 16804 KB Output is correct
2 Correct 561 ms 21620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 220 ms 5880 KB Output is correct
2 Correct 76 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 169 ms 7668 KB Output is correct
2 Correct 234 ms 6536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 399 ms 8824 KB Output is correct
2 Correct 298 ms 8312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 554 ms 10360 KB Output is correct
2 Correct 487 ms 10096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 833 ms 12656 KB Output is correct
2 Correct 620 ms 11252 KB Output is correct