답안 #333432

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
333432 2020-12-05T23:27:14 Z 534351 자매 도시 (APIO20_swap) C++17
7 / 100
656 ms 524292 KB
#include "swap.h"
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>

using namespace std;
// using namespace __gnu_pbds;

// template<class T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

const int MAXN = 100013;
const int INF = 1e9 + 7;

int N, T;
int dsu[MAXN];
vpi edge[MAXN];
int t[MAXN];
pii corners[MAXN];
vi nodes[MAXN];
bitset<MAXN> ok;
int st[MAXN], ft[MAXN];
pii jump[20][MAXN];

bool insubt(int u, int v) //is v in the subtree of u?
{
    return (u == N || (st[u] <= st[v] && st[v] <= ft[u]));
}
void dfs(int u, int p)
{
    st[u] = T;
    ft[u] = T;
    T++;
    for (auto e : edge[u])
    {
        int d = e.fi, v = e.se;
        if (v == p) continue;
        jump[0][v] = {u, d};
        dfs(v, u);
        ft[u] = ft[v];
    }
}
int get(int u)
{
    return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
bool merge(int u, int v)
{
    u = get(u);
    v = get(v);
    if (u == v) return false;
    dsu[u] = v;
    return true;
}
void init(int n, int m, vi U, vi V, vi W)
{
    N = n;
    fill(t, t + N, INF);
    vector<pair<int, pii> > edges;
    FOR(i, 0, m)
    {
        edges.PB({W[i], {U[i], V[i]}});
    }
    sort(ALL(edges));
    FOR(i, 0, N)
    {
        dsu[i] = i;
        nodes[i].PB(i);
        corners[i] = {i, i};
    }
    for (auto e : edges)
    {
        int d = e.fi, u = e.se.fi, v = e.se.se;
        int cu = get(u), cv = get(v);
        if (cu == cv)
        {
            if (!ok[cu])
            {
                for (int w : nodes[cu]) t[w] = d;
                ok[cu] = true;
            }
            //they're in the same component.
        }
        else
        {
            edge[u].PB({d, v});
            edge[v].PB({d, u});
            // cerr << "EDGE " << u << " <-> " << v << ' ' << d << endl;
            if (SZ(nodes[cu]) < SZ(nodes[cv]))
            {
                swap(u, v);
                swap(cu, cv);
            }
            //they're in different components.
            bool bad = (!ok[cu] && !ok[cv]
            && (u == corners[cu].fi || u == corners[cu].se) && (v == corners[cv].fi || v == corners[cv].se));
            if (bad) //update corners
            {
                pii corn = {corners[cu].fi ^ corners[cu].se ^ u, corners[cv].fi ^ corners[cv].se ^ v};
                corners[cu] = corn;
            }
            else //mark anybody that needs to be marked.
            {
                if (!ok[cu])
                {
                    for (int w : nodes[cu]) t[w] = d;
                }
                if (!ok[cv])
                {
                    for (int w : nodes[cv]) t[w] = d;
                }
                ok[cu] = true;
            }
            nodes[cu].insert(nodes[cu].end(), ALL(nodes[cv]));
            nodes[cv].clear();
            dsu[v] = u;
        }
    }
    jump[0][N] = {N, 0};
    jump[0][0] = {N, 0};
    dfs(0, N);
    FOR(i, 1, 20)
    {
        FOR(j, 0, N + 1)
        {
            jump[i][j] = {jump[i - 1][jump[i - 1][j].fi].fi, max(jump[i - 1][j].se, jump[i - 1][jump[i - 1][j].fi].se)};
        }
    }
    //now you want to be able to find longest edge on path u to v
}
int ask(int u, int v)
{
    // cerr << "ASK " << u << ' ' << v << endl;
    int res = 0;
    FORD(i, 20, 0)
    {
        if (insubt(jump[i][u].fi, v)) continue;
        ckmax(res, jump[i][u].se);
        u = jump[i][u].fi;
    }
    if (!insubt(u, v))
    {
        ckmax(res, jump[0][u].se);
    }
    FORD(i, 20, 0)
    {
        if (insubt(jump[i][v].fi, u)) continue;
        ckmax(res, jump[i][v].se);
        v = jump[i][v].fi;
    }
    if (!insubt(v, u))
    {
        ckmax(res, jump[0][v].se);
    }
    return res;
}

int getMinimumFuelCapacity(int u, int v)
{
    // cerr << "ASK " << ask(u, v) << endl;
    int res = max(ask(u, v), max(t[u], t[v]));
    if (res == INF) res = -1;
    return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 4 ms 5248 KB Output is correct
5 Correct 4 ms 5484 KB Output is correct
6 Correct 4 ms 5484 KB Output is correct
7 Correct 4 ms 5612 KB Output is correct
8 Correct 4 ms 5484 KB Output is correct
9 Correct 118 ms 32608 KB Output is correct
10 Correct 150 ms 40032 KB Output is correct
11 Correct 151 ms 39136 KB Output is correct
12 Correct 159 ms 41184 KB Output is correct
13 Correct 121 ms 43104 KB Output is correct
14 Correct 156 ms 32224 KB Output is correct
15 Correct 614 ms 43872 KB Output is correct
16 Correct 593 ms 41440 KB Output is correct
17 Correct 656 ms 46816 KB Output is correct
18 Correct 604 ms 45920 KB Output is correct
19 Runtime error 608 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 324 ms 38348 KB Output is correct
4 Correct 321 ms 39600 KB Output is correct
5 Correct 331 ms 39052 KB Output is correct
6 Correct 314 ms 39476 KB Output is correct
7 Correct 339 ms 39392 KB Output is correct
8 Correct 332 ms 38336 KB Output is correct
9 Correct 319 ms 39140 KB Output is correct
10 Correct 321 ms 37976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 3 ms 5100 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5484 KB Output is correct
7 Correct 4 ms 5484 KB Output is correct
8 Correct 4 ms 5612 KB Output is correct
9 Correct 4 ms 5484 KB Output is correct
10 Correct 4 ms 5484 KB Output is correct
11 Incorrect 5 ms 5484 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 3 ms 5100 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5484 KB Output is correct
7 Correct 4 ms 5484 KB Output is correct
8 Correct 4 ms 5612 KB Output is correct
9 Correct 4 ms 5484 KB Output is correct
10 Correct 118 ms 32608 KB Output is correct
11 Correct 150 ms 40032 KB Output is correct
12 Correct 151 ms 39136 KB Output is correct
13 Correct 159 ms 41184 KB Output is correct
14 Correct 121 ms 43104 KB Output is correct
15 Correct 4 ms 5484 KB Output is correct
16 Incorrect 5 ms 5484 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 4 ms 5248 KB Output is correct
5 Correct 4 ms 5484 KB Output is correct
6 Correct 4 ms 5484 KB Output is correct
7 Correct 4 ms 5612 KB Output is correct
8 Correct 4 ms 5484 KB Output is correct
9 Correct 118 ms 32608 KB Output is correct
10 Correct 150 ms 40032 KB Output is correct
11 Correct 151 ms 39136 KB Output is correct
12 Correct 159 ms 41184 KB Output is correct
13 Correct 121 ms 43104 KB Output is correct
14 Correct 156 ms 32224 KB Output is correct
15 Correct 614 ms 43872 KB Output is correct
16 Correct 593 ms 41440 KB Output is correct
17 Correct 656 ms 46816 KB Output is correct
18 Correct 604 ms 45920 KB Output is correct
19 Correct 324 ms 38348 KB Output is correct
20 Correct 321 ms 39600 KB Output is correct
21 Correct 331 ms 39052 KB Output is correct
22 Correct 314 ms 39476 KB Output is correct
23 Correct 339 ms 39392 KB Output is correct
24 Correct 332 ms 38336 KB Output is correct
25 Correct 319 ms 39140 KB Output is correct
26 Correct 321 ms 37976 KB Output is correct
27 Correct 4 ms 5484 KB Output is correct
28 Incorrect 5 ms 5484 KB Output isn't correct
29 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 3 ms 5100 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5484 KB Output is correct
7 Correct 4 ms 5484 KB Output is correct
8 Correct 4 ms 5612 KB Output is correct
9 Correct 4 ms 5484 KB Output is correct
10 Correct 118 ms 32608 KB Output is correct
11 Correct 150 ms 40032 KB Output is correct
12 Correct 151 ms 39136 KB Output is correct
13 Correct 159 ms 41184 KB Output is correct
14 Correct 121 ms 43104 KB Output is correct
15 Correct 156 ms 32224 KB Output is correct
16 Correct 614 ms 43872 KB Output is correct
17 Correct 593 ms 41440 KB Output is correct
18 Correct 656 ms 46816 KB Output is correct
19 Correct 604 ms 45920 KB Output is correct
20 Correct 324 ms 38348 KB Output is correct
21 Correct 321 ms 39600 KB Output is correct
22 Correct 331 ms 39052 KB Output is correct
23 Correct 314 ms 39476 KB Output is correct
24 Correct 339 ms 39392 KB Output is correct
25 Correct 332 ms 38336 KB Output is correct
26 Correct 319 ms 39140 KB Output is correct
27 Correct 321 ms 37976 KB Output is correct
28 Correct 4 ms 5484 KB Output is correct
29 Incorrect 5 ms 5484 KB Output isn't correct
30 Halted 0 ms 0 KB -