제출 #1368337

#제출 시각아이디문제언어결과실행 시간메모리
1368337yonatanlSwapping Cities (APIO20_swap)C++20
0 / 100
2096 ms15480 KiB
#include "swap.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <math.h>
#include <iomanip>

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define upmax(a, b) a = max(a, b)
#define upmin(a, b) a = min(a, b)

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

const ll INF = 2e18;
const ll MOD = 1e9 + 7;

ll n, m;
vvpll g;
vvll dp;
vll deg;
vector<pair<ll, pll>> edges;

struct DSU {
    vll papa, sz, swappable;
    DSU(ll n) {
        papa.resize(n), sz.resize(n, 1);
        swappable.resize(n, 0);
        for (ll i = 0; i < n; i++) papa[i] = i;
    }
    ll find(ll i) {
        if (papa[i] == i) return i;
        papa[i] = find(papa[i]);
        return papa[i];
    }
    bool unite(ll a, ll b) {
        bool is_swappable = false;
        a = find(a);
        b = find(b);
        is_swappable = (swappable[a] || swappable[b]);
        if (a == b) {
            swappable[a] = true;
            return false;
        }
        if (sz[a] < sz[b]) swap(a, b);
        sz[a] += sz[b];
        papa[b] = a;
        if (deg[a] == 3 || deg[b] == 3) {
            is_swappable = true;
        }
        swappable[a] = is_swappable;
        return true;
    }
};

void dijkstra(ll x, ll y) {
    dp.clear();
    dp.resize(n, vll(n, INF));
    dp[x][y] = 0;
    priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>> pq;
    pq.push({ 0, {x, y} });
    while (!pq.empty()) {
        ll cur_x = pq.top().second.first;
        ll cur_y = pq.top().second.second;
        ll max_edge = pq.top().first;
        pq.pop();
        for (auto& it : g[cur_x]) {
            ll next_x = it.first;
            ll w = it.second;
            if (next_x == cur_y) continue;
            if (max(w, max_edge) < dp[next_x][cur_y]) {
                dp[next_x][cur_y] = max(w, max_edge);
                pq.push({ dp[next_x][cur_y], {next_x, cur_y} });
            }
        }
        for (auto& it : g[cur_y]) {
            ll next_y = it.first;
            ll w = it.second;
            if (next_y == cur_x) continue;
            if (max(w, max_edge) < dp[cur_x][next_y]) {
                dp[cur_x][next_y] = max(w, max_edge);
                pq.push({ dp[cur_x][next_y], {cur_x, next_y} });
            }
        }
    }
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    n = N, m = M;
    g.clear(), g.resize(n);
    deg.clear(), deg.resize(n);
    edges.clear();
    rep(i, 0, m) {
        g[U[i]].push_back({ V[i], W[i] });
        g[V[i]].push_back({ U[i], W[i] });
        edges.push_back({ W[i], {U[i], V[i]} });
    }
    sort(edges.begin(), edges.end());
}

int getMinimumFuelCapacity(int X, int Y) {
    ll x = X, y = Y;
    DSU dsu(n);
    rep(i, 0, m) {
        ll w = edges[i].first;
        ll a = edges[i].second.first;
        ll b = edges[i].second.second;
        dsu.unite(a, b);
        ll root_x = dsu.find(x);
        ll root_y = dsu.find(y);
        if (root_x == root_y && dsu.swappable[root_x]) {
            return w;
        }
    }
    return -1;
    dijkstra(x, y);
    if (dp[y][x] == INF) return -1;
    return dp[y][x];
}

/*
5 4
0 1 3
0 2 10
0 3 5
0 4 4
8
0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3

3 3 
0 1 1 
1 2 2 
2 0 3 
3
0 1 
0 2 
1 2

4 3
0 1 1
1 2 2
2 3 1 
3
0 1 
0 2 
1 2 

5 6 
0 1 4 
0 2 4 
1 2 1 
1 3 2
1 4 10 
2 3 3 
3
1 2 
2 4 
0 1 
*/
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…