답안 #404115

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404115 2021-05-13T19:35:49 Z Collypso 자매 도시 (APIO20_swap) C++17
37 / 100
2000 ms 28012 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<pair<int, int>> dsu;

int dsuValue(int x)
{
    if (dsu[x].F == x) return x;
    return dsu[x].F = dsuValue(dsu[x].F);
}

void dsuMerge(int x, int y)
{
    x = dsuValue(x), y = dsuValue(y);
    if (dsu[y].S < dsu[x].S) swap(x, y);
    dsu[x].F = y;
    dsu[y].S += dsu[x].S;
}

vt<vt<pair<int, int>>> g, mst;
vt<bool> marked, used;

void init(int N, int M, vt<int> U, vt<int> V, vt<int> W)
{
    g.resize(N), mst.resize(N), dsu.resize(N), marked.resize(N), used.resize(N);
    vt<pair<int, pair<int, int>>> edges;
    for(int i = 0; i < M; i++)
    {
        g[U[i]].pb({W[i], V[i]});
        g[V[i]].pb({W[i], U[i]});
        edges.pb({W[i], {U[i], V[i]}});
    }
    sort(all(edges));
    for(int i = 0; i < N; i++) sort(all(g[i])), dsu[i] = {i, 1};
    for(int i = 0; i < M; i++)
    {
        int u, v, w;
        u = edges[i].S.F, v = edges[i].S.S, w = edges[i].F;
        if (dsuValue(u) == dsuValue(v)) continue;
        mst[u].pb({v, w}), mst[v].pb({u, w});
        dsuMerge(u, v);
    }
}

bool completed;
vt<int> path, pathW;

void dfs1(int v, int to, int p = -1)
{
    if (v == to) completed = 1;
    if (completed) return;
    for(auto u : mst[v])
    {
        if (u.F == p) continue;
        path.pb(u.F), pathW.pb(u.S);
        dfs1(u.F, to, v);
        if (completed) return;
        path.pop_back(), pathW.pop_back();
    }
}

int maxFuel;

void dfs2(int v, int firstV, int firstU, int p = -1, int mx = 0)
{
    used[v] = 1;
    for(int i = 0; i < sz(g[v]); i++)
    {
        int u = g[v][i].S, w = g[v][i].F;
        if ((u == p) || (v == firstV && u == firstU)) continue;
        if (used[u])
        {
            maxFuel = min(maxFuel, max(mx, w));
            break;
        }
        mx = max(mx, w);
        if (sz(g[v]) - i >= 2)
        {
            if (g[v][i + 1].S == p || marked[g[v][i + 1].S])
            {
                if (sz(g[v]) - i >= 3) maxFuel = min(maxFuel, max(mx, g[v][i + 2].F));
            }
            else maxFuel = min(maxFuel, max(mx, g[v][i + 1].F));
        }
        dfs2(u, firstV, firstU, v, mx);
        break;
    }
}

int getMinimumFuelCapacity(int X, int Y)
{
    completed = 0;
    path.clear(), pathW.clear(), fill(all(marked), 0);
    // CLEAR (FILL MARKED WITH 0) EVERYTHING

    path.pb(X);
    dfs1(X, Y);
    for(int x : path) marked[x] = 1;
    maxFuel = INT_MAX;
    for(int i = 1; i < sz(path) - 1; i++)
    {
        for(auto v : g[path[i]])
        {
            if (marked[v.S]) continue;
            maxFuel = min(maxFuel, v.F);
        }
    }

    fill(all(used), 0);
    used[Y] = 1;
    dfs2(X, X, path[1]);

    fill(all(used), 0);
    used[X] = 1;
    dfs2(Y, Y, path[sz(path) - 2]);

    for(int x : pathW) maxFuel = max(maxFuel, x);

    if (maxFuel == INT_MAX) return -1;
    return maxFuel;
}

/**
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:154:1: warning: "/*" within comment [-Wcomment]
  154 | /**/
      |
# 결과 실행 시간 메모리 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 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 97 ms 16864 KB Output is correct
10 Correct 170 ms 23532 KB Output is correct
11 Correct 186 ms 22308 KB Output is correct
12 Correct 169 ms 22964 KB Output is correct
13 Correct 153 ms 23724 KB Output is correct
14 Execution timed out 2052 ms 20732 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Execution timed out 2087 ms 17632 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 428 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 2 ms 460 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 428 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 432 KB Output is correct
25 Correct 2 ms 432 KB Output is correct
26 Correct 2 ms 588 KB Output is correct
27 Correct 2 ms 428 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
# 결과 실행 시간 메모리 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 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 97 ms 16864 KB Output is correct
11 Correct 170 ms 23532 KB Output is correct
12 Correct 186 ms 22308 KB Output is correct
13 Correct 169 ms 22964 KB Output is correct
14 Correct 153 ms 23724 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 2 ms 428 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 428 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
29 Correct 2 ms 432 KB Output is correct
30 Correct 2 ms 432 KB Output is correct
31 Correct 2 ms 588 KB Output is correct
32 Correct 2 ms 428 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 17 ms 2764 KB Output is correct
35 Correct 178 ms 21440 KB Output is correct
36 Correct 140 ms 19076 KB Output is correct
37 Correct 136 ms 17376 KB Output is correct
38 Correct 122 ms 17108 KB Output is correct
39 Correct 141 ms 17136 KB Output is correct
40 Correct 99 ms 16040 KB Output is correct
41 Correct 159 ms 20000 KB Output is correct
42 Correct 170 ms 19896 KB Output is correct
43 Correct 145 ms 19872 KB Output is correct
44 Correct 127 ms 18644 KB Output is correct
45 Correct 137 ms 19924 KB Output is correct
46 Correct 197 ms 20604 KB Output is correct
47 Correct 160 ms 17364 KB Output is correct
48 Correct 114 ms 18296 KB Output is correct
49 Correct 92 ms 13572 KB Output is correct
50 Correct 74 ms 10576 KB Output is correct
51 Correct 137 ms 16992 KB Output is correct
52 Correct 192 ms 22816 KB Output is correct
53 Correct 197 ms 25148 KB Output is correct
54 Correct 326 ms 28012 KB Output is correct
55 Correct 149 ms 22320 KB Output is correct
56 Correct 174 ms 24104 KB Output is correct
# 결과 실행 시간 메모리 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 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 97 ms 16864 KB Output is correct
10 Correct 170 ms 23532 KB Output is correct
11 Correct 186 ms 22308 KB Output is correct
12 Correct 169 ms 22964 KB Output is correct
13 Correct 153 ms 23724 KB Output is correct
14 Execution timed out 2052 ms 20732 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 97 ms 16864 KB Output is correct
11 Correct 170 ms 23532 KB Output is correct
12 Correct 186 ms 22308 KB Output is correct
13 Correct 169 ms 22964 KB Output is correct
14 Correct 153 ms 23724 KB Output is correct
15 Execution timed out 2052 ms 20732 KB Time limit exceeded
16 Halted 0 ms 0 KB -