Submission #636009

# Submission time Handle Problem Language Result Execution time Memory
636009 2022-08-27T14:50:06 Z Do_you_copy Transport (COCI19_transport) C++17
0 / 130
1000 ms 25752 KB
#include <bits/stdc++.h>
//#define int long long
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update>

ll min(const ll &a, const ll &b){
    return (a < b) ? a : b;
}

ll max(const ll &a, const ll &b){
    return (a > b) ? a : b;
}

//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 1e5 + 1;
int n;
int a[maxN];

vector <pii> adj[maxN];
int par[maxN];
bool visited[maxN];
int subsz[maxN];

void cent_dfs(int u, int p){
    subsz[u] = 1;
    for (auto &i: adj[u]){
        if (i.fi == p || visited[i.fi]) continue;
        cent_dfs(i.fi, u);
        subsz[u] += subsz[i.fi];
    }
}

int find_cent(int u, int p, int sz){
    for (auto &i: adj[u]){
        if (i.fi == p || visited[i.fi]) continue;
        if (subsz[i.fi] > sz / 2) return find_cent(i.fi, u, sz);
    }
    return u;
}

ll ans = 0;
vector <int> save1;
vector <pii> save2;
ordered_set S;
int cost[maxN];
void find_cost(int u, int p, int minn, int dis){
    cost[u] = min(minn, dis);
    S.insert({-cost[u], u});
    for (auto &i: adj[u]){
        if (i.fi == p || visited[i.fi]) continue;
        find_cost(i.fi, u, cost[u], dis + a[u] - i.se);
    }
}

void dfs(int u, int p, int minn, int dis){
    //dis: fuel left if we run from u to root(centroid)
    //minn: minimum fuel on the way to root
    if (minn >= 0) save1.pb(dis);
    if (u != p) save2.pb({cost[u], u});
    for (auto &i: adj[u]){
        if (i.fi == p || visited[i.fi]) continue;
        dfs(i.fi, u, min(minn - i.se + a[i.fi], 0), dis + a[i.fi] - i.se);
        if (u == p){
            cerr << S.size();
            for (auto j: save2) S.erase(j);
            for (int j: save1) ans += S.order_of_key({j + 1, -10000000});
            for (auto j: save2) S.insert(j);
            save1.clear();
        }
    }
}

void build_cent(int u){
    S.clear();
    cent_dfs(u, u);
    int cent = find_cent(u, u, subsz[u]);
    visited[cent] = 1;
    find_cost(cent, cent, 0, 0);
    dfs(cent, cent, 0, 0);
    for (const auto &i: adj[cent]){
        if (visited[i.fi]) continue;
        build_cent(i.fi);
    }
}

void Init(){
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[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});
    }
    build_cent(1);
    cout << ans;
}

#define debu
#define taskname "test"
signed main(){
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        #ifdef debug
        freopen(taskname".out", "w", stdout);
        #endif
    }
    faster;
    ll tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
    if (fopen("timeout.txt", "r")){
        ofstream timeout("timeout.txt");
        timeout << 1000 * double(clock()) / CLOCKS_PER_SEC;
        #ifndef debug
        cerr << "\nTime elapsed: " << 1000 * double(clock()) / CLOCKS_PER_SEC << "ms\n";
        #endif
    }
}

Compilation message

transport.cpp: In function 'int main()':
transport.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 1081 ms 3436 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1064 ms 3896 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1092 ms 14772 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1042 ms 19120 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1072 ms 25752 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1087 ms 7064 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1080 ms 10744 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1070 ms 13992 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1068 ms 16296 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1078 ms 23940 KB Time limit exceeded
2 Halted 0 ms 0 KB -