Submission #625637

#TimeUsernameProblemLanguageResultExecution timeMemory
625637Soul234Race (IOI11_race)C++14
100 / 100
495 ms72100 KiB
#include<bits/stdc++.h>
using namespace std;

void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << h; if(sizeof...(t)) cerr << ", ";
    DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL

#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
#define fi first
#define se second

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;

void setIO(string NAME = "") {
    cin.tie(0)->sync_with_stdio(0);
    if(sz(NAME)) {
        freopen((NAME + ".inp").c_str(),"r",stdin);
        freopen((NAME + ".out").c_str(),"w",stdout);
    }
}

tcT> bool ckmin(T&a, const T&b) {
    return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
    return b > a ? a=b,1 : 0; }

const int MOD = 1e9 + 7;
const int MX = 2e5+5;
const ll INF = 1e18;

int N, dep[MX];
V<pi> adj[MX];
map<ll, int> mn_dep[MX];
ll dist[MX], K;
int ans = MOD;

void dfs(int u, int p) {
    mn_dep[u][dist[u]] = dep[u];
    each(ed, adj[u]) if(ed.fi != p) {
        dep[ed.fi] = dep[u] + 1;
        dist[ed.fi] = dist[u] + ed.se;
        dfs(ed.fi, u);
    }
}

void dfsCalc(int u, int p) {
    each(ed, adj[u]) if(ed.fi != p) {
        dfsCalc(ed.fi, u);
        int v = ed.fi;
        if(sz(mn_dep[u]) < sz(mn_dep[v])) swap(mn_dep[u], mn_dep[v]);
        each(val, mn_dep[v]) {
            ll du = K + dist[u]*2 - val.fi;
            if(mn_dep[u].find(du) != mn_dep[u].end()) ckmin(ans, val.se + mn_dep[u][du] - 2*dep[u]);
        }
        each(val, mn_dep[v]) {
            if(mn_dep[u].find(val.fi) != mn_dep[u].end()) ckmin(mn_dep[u][val.fi], val.se);
            else mn_dep[u][val.fi] = val.se;
        }
        mn_dep[v].clear();
    }
}

int best_path(int n, int k, int eds[][2], int cst[]) {
    N = n;
    K = k;
    F0R(i, N-1) {
        int u = eds[i][0], v = eds[i][1], w = cst[i];
        adj[u].pb({v, w});
        adj[v].pb({u, w});
    }
    dfs(0, -1);
    dfsCalc(0, -1);
    if(ans >= MOD) ans = -1;
    return ans;
}

Compilation message (stderr)

race.cpp: In function 'void setIO(std::string)':
race.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen((NAME + ".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen((NAME + ".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...