Submission #406991

# Submission time Handle Problem Language Result Execution time Memory
406991 2021-05-18T09:41:16 Z Collypso Swapping Cities (APIO20_swap) C++17
30 / 100
2000 ms 524292 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define F first
#define S second
#pragma GCC optimize ("O3")
#pragma GCC optimize ("O2")
//#define endl '\n'
//#define int ll

using namespace std;

vt<vt<pair<int, int>>> g;
vt<vt<int>> parent, parentMax;
vt<int> timeEnter, timeExit;
int timer, logTmp;

void dfsLCA(int v, int p, int w = 0)
{
    timeEnter[v] = ++timer;
    parent[v][0] = p, parentMax[v][0] = w;
    for(int i = 1; i <= logTmp; i++)
    {
        parent[v][i] = parent[parent[v][i - 1]][i - 1];
        parentMax[v][i] = max(parentMax[v][i - 1], parentMax[parent[v][i - 1]][i - 1]);
    }
    for(auto to : g[v])
        if (to.F != p) dfsLCA(to.F, v, to.S);
    timeExit[v] = ++timer;
}

bool isAncestor(int v1, int v2)
{
    return timeEnter[v1] <= timeEnter[v2] && timeExit[v1] >= timeExit[v2];
}

int maxLCA(int v1, int v2)
{
    if (isAncestor(v1, v2)) swap(v1, v2);
    int val = 0;
    for(int i = logTmp; i >= 0; i--)
        if (!isAncestor(parent[v1][i], v2))
            val = max(val, parentMax[v1][i]), v1 = parent[v1][i];
    if (!isAncestor(v1, v2)) val = max(val, parentMax[v1][0]), v1 = parent[v1][0];
    for(int i = logTmp; i >= 0; i--)
        if (!isAncestor(parent[v2][i], v1))
            val = max(val, parentMax[v2][i]), v2 = parent[v2][i];
    if (!isAncestor(v2, v1)) val = max(val, parentMax[v2][0]), v2 = parent[v2][0];
    return val;
}

vt<int> dp;

void dfsDP1(int v, int p = -1)
{
    dp[v] = INT_MAX;
    if (sz(g[v]) >= 3) dp[v] = g[v][2].S;
    for(auto to : g[v])
    {
        if (to.F != p) dfsDP1(to.F, v);
        dp[v] = min(dp[v], max(to.S, dp[to.F]));
    }
}

void dfsDP2(int v, int p = -1)
{
    for(auto to : g[v])
    {
        if (to.F == p) continue;
        dp[to.F] = min(dp[to.F], max(to.S, dp[v]));
        dfsDP2(to.F, v);
    }
}

void init(int N, int M, vt<int> U, vt<int> V, vt<int> W)
{
    g.resize(N);
    for(int i = 0; i < M; i++) g[U[i]].pb({V[i], W[i]}), g[V[i]].pb({U[i], W[i]});
    logTmp = ceil(log2(N)) + 1;
    dp.resize(N), timeEnter.resize(N), timeExit.resize(N);
    parent.resize(N, vt<int>(logTmp + 1)), parentMax.resize(N, vt<int>(logTmp + 1));
    for(int i = 0; i < N; i++) sort(all(g[i]), [](auto& a, auto& b){return a.S < b.S;});
    dfsLCA(0, 0), dfsDP1(0), dfsDP2(0);
}

int getMinimumFuelCapacity(int X, int Y)
{
    if (dp[X] == INT_MAX && dp[Y] == INT_MAX) return -1;
    return max(maxLCA(X, Y), min(dp[X], dp[Y]));
}

/**
main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    freopen("input.txt", "r", stdin);
    int N, M, Q;
    cin >> N >> M;
    vt<int> U(M), V(M), W(M);
    for(int i = 0; i < M; i++) cin >> U[i] >> V[i] >> W[i];
    init(N, M, U, V, W);
    cin >> Q;
    while(Q--)
    {
        int X, Y;
        cin >> X >> Y;
        cout << getMinimumFuelCapacity(X, Y) << endl;
    }
}
/**/

Compilation message

swap.cpp:114:1: warning: "/*" within comment [-Wcomment]
  114 | /**/
      |
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 560 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 164 ms 30384 KB Output is correct
10 Correct 260 ms 38228 KB Output is correct
11 Correct 226 ms 37188 KB Output is correct
12 Correct 225 ms 39580 KB Output is correct
13 Correct 233 ms 41004 KB Output is correct
14 Correct 165 ms 30216 KB Output is correct
15 Correct 312 ms 41136 KB Output is correct
16 Correct 280 ms 38972 KB Output is correct
17 Correct 333 ms 43768 KB Output is correct
18 Correct 298 ms 42716 KB Output is correct
19 Execution timed out 2103 ms 248360 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 275 ms 37892 KB Output is correct
4 Correct 283 ms 39568 KB Output is correct
5 Correct 305 ms 38568 KB Output is correct
6 Correct 294 ms 39320 KB Output is correct
7 Correct 280 ms 39044 KB Output is correct
8 Correct 278 ms 38324 KB Output is correct
9 Correct 289 ms 39344 KB Output is correct
10 Correct 279 ms 37964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 560 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Runtime error 534 ms 524292 KB Execution killed with signal 9
# Verdict Execution time Memory Grader output
1 Runtime error 534 ms 524292 KB Execution killed with signal 9
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 560 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 164 ms 30384 KB Output is correct
10 Correct 260 ms 38228 KB Output is correct
11 Correct 226 ms 37188 KB Output is correct
12 Correct 225 ms 39580 KB Output is correct
13 Correct 233 ms 41004 KB Output is correct
14 Correct 165 ms 30216 KB Output is correct
15 Correct 312 ms 41136 KB Output is correct
16 Correct 280 ms 38972 KB Output is correct
17 Correct 333 ms 43768 KB Output is correct
18 Correct 298 ms 42716 KB Output is correct
19 Correct 275 ms 37892 KB Output is correct
20 Correct 283 ms 39568 KB Output is correct
21 Correct 305 ms 38568 KB Output is correct
22 Correct 294 ms 39320 KB Output is correct
23 Correct 280 ms 39044 KB Output is correct
24 Correct 278 ms 38324 KB Output is correct
25 Correct 289 ms 39344 KB Output is correct
26 Correct 279 ms 37964 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
28 Correct 2 ms 588 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 432 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 2 ms 460 KB Output is correct
33 Correct 2 ms 588 KB Output is correct
34 Correct 2 ms 588 KB Output is correct
35 Correct 2 ms 556 KB Output is correct
36 Correct 16 ms 4620 KB Output is correct
37 Correct 250 ms 38960 KB Output is correct
38 Correct 263 ms 36804 KB Output is correct
39 Correct 206 ms 35244 KB Output is correct
40 Correct 223 ms 34428 KB Output is correct
41 Correct 192 ms 34092 KB Output is correct
42 Correct 148 ms 31388 KB Output is correct
43 Correct 241 ms 37804 KB Output is correct
44 Correct 231 ms 38276 KB Output is correct
45 Correct 236 ms 39336 KB Output is correct
46 Correct 170 ms 35372 KB Output is correct
47 Correct 26 ms 4668 KB Output is correct
48 Correct 672 ms 42228 KB Output is correct
49 Correct 672 ms 41132 KB Output is correct
50 Correct 637 ms 40236 KB Output is correct
51 Correct 584 ms 39696 KB Output is correct
52 Correct 473 ms 37400 KB Output is correct
53 Correct 342 ms 29304 KB Output is correct
54 Correct 666 ms 42544 KB Output is correct
55 Correct 682 ms 42672 KB Output is correct
56 Correct 733 ms 44812 KB Output is correct
57 Correct 444 ms 40856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 534 ms 524292 KB Execution killed with signal 9