답안 #319769

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
319769 2020-11-06T11:54:10 Z arujbansal 자매 도시 (APIO20_swap) C++17
7 / 100
254 ms 14332 KB
#include "swap.h"
#include <bits/stdc++.h>

#define FAST_IO ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define setIO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int) (x).size()
#define lc(i) 2*i
#define rc(i) 2*i+1
//#define int long long
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vii = vector<ii>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;

const int MAXN = 1e5 + 5, MOD = 1e9 + 7, INF = 1e9 + 5;

struct Edge {
    int u, v, w;

//    Edge(int u, int v, int w) : u(u), v(v), w(w) {}

    bool operator<(const Edge &other) {
        return w < other.w;
    }
};

struct DSU {
    int n;
    vi par, s, deg, mxDeg, timeValid;
    vii root[MAXN];

    void init(int n) {
        this->n = n;
        par.resize(n);
        iota(all(par), 0);
        s.assign(n, 1);
        deg.assign(n, 0);
        mxDeg.assign(n, 0);
        timeValid.assign(n, INF);

        for (int i = 0; i < n; i++)
            root[i].eb(0, i);
    }

    int get(int x) {
        if (par[x] == x) return x;
        return get(par[x]);
    }

    void unite(int u, int v, int time) {
        deg[u]++;
        deg[v]++;

        int x = get(u);
        int y = get(v);

        if (x == y) {
            timeValid[x] = min(timeValid[x], time);
            return;
        }

        if (deg[u] >= 3) timeValid[x] = min(timeValid[x], time);
        if (deg[v] >= 3) timeValid[y] = min(timeValid[y], time);

        if (s[x] > s[y]) {
            swap(x, y);
            swap(u, v);
        }

        par[x] = y;
        s[y] += s[x];
        timeValid[y] = min(timeValid[y], timeValid[x]);

        root[u].eb(time, y);
    }

    bool query(int u, int v, int time) {
        time++;

        auto x = upper_bound(all(root[u]), ii(time, n + 1));
        auto y = upper_bound(all(root[v]), ii(time, n + 1));
        x--, y--;

        ii rootU = *x;
        ii rootV = *y;

        if (rootU.second == rootV.second)
            return timeValid[rootU.second] <= time;

        return false;
    }
};

DSU graph;
vector<Edge> edges;
int n, m;

void init(int N, int M, vi U, vi V, vi W) {
    n = N;
    m = M;

    edges.resize(M);

    for (int i = 0; i < M; i++) {
        edges[i].u = U[i];
        edges[i].v = V[i];
        edges[i].w = W[i];
    }

    sort(all(edges));

    graph.init(N);

    for (int i = 0; i < M; i++)
        graph.unite(edges[i].u, edges[i].v, i + 1);
}

int getMinimumFuelCapacity(int X, int Y) {
    int lo = 0, hi = m - 1;
    int ans = INF;

    while (lo <= hi) {
        int mid = (lo + hi) >> 1;
        if (graph.query(X, Y, mid)) {
            ans = min(ans, edges[mid].w);
            hi = mid - 1;
        } else lo = mid + 1;
    }

    return ans < INF ? ans : -1;
}

//signed main() {
//    FAST_IO;
//#ifdef arujbansal_local
//    setIO("input.txt", "output.txt");
//#endif
//
//    int a, b;
//    cin >> a >> b;
//    int u[b], v[b], wt[b];
//
//    for (int i = 0; i < b; i++)
//        cin >> u[i] >> v[i] >> wt[i];
//
//    init(a, b, u, v, wt);
//
//    int q;
//    cin >> q;
//    while (q--) {
//        int x, y;
//        cin >> x >> y;
//        cout << getMinimumFuelCapacity(x, y) << "\n";
//    }
//
////    for (const auto &[time, root] : graph.root[4])
////        cout << time << " " << root << "\n";
//}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 4 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 2 ms 2924 KB Output is correct
8 Correct 3 ms 2796 KB Output is correct
9 Correct 73 ms 10104 KB Output is correct
10 Correct 77 ms 11744 KB Output is correct
11 Correct 82 ms 11880 KB Output is correct
12 Correct 84 ms 12132 KB Output is correct
13 Correct 73 ms 11240 KB Output is correct
14 Correct 72 ms 10212 KB Output is correct
15 Correct 234 ms 13820 KB Output is correct
16 Correct 215 ms 13460 KB Output is correct
17 Correct 219 ms 13904 KB Output is correct
18 Correct 205 ms 13132 KB Output is correct
19 Incorrect 115 ms 6628 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 254 ms 13840 KB Output is correct
4 Correct 247 ms 14156 KB Output is correct
5 Correct 243 ms 14332 KB Output is correct
6 Correct 234 ms 14028 KB Output is correct
7 Correct 250 ms 14180 KB Output is correct
8 Correct 231 ms 13844 KB Output is correct
9 Correct 248 ms 14052 KB Output is correct
10 Correct 234 ms 13856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 4 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 2 ms 2924 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Incorrect 3 ms 2796 KB Output isn't correct
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 4 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 2 ms 2924 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 73 ms 10104 KB Output is correct
11 Correct 77 ms 11744 KB Output is correct
12 Correct 82 ms 11880 KB Output is correct
13 Correct 84 ms 12132 KB Output is correct
14 Correct 73 ms 11240 KB Output is correct
15 Incorrect 3 ms 2796 KB Output isn't correct
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 4 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 2 ms 2924 KB Output is correct
8 Correct 3 ms 2796 KB Output is correct
9 Correct 73 ms 10104 KB Output is correct
10 Correct 77 ms 11744 KB Output is correct
11 Correct 82 ms 11880 KB Output is correct
12 Correct 84 ms 12132 KB Output is correct
13 Correct 73 ms 11240 KB Output is correct
14 Correct 72 ms 10212 KB Output is correct
15 Correct 234 ms 13820 KB Output is correct
16 Correct 215 ms 13460 KB Output is correct
17 Correct 219 ms 13904 KB Output is correct
18 Correct 205 ms 13132 KB Output is correct
19 Correct 254 ms 13840 KB Output is correct
20 Correct 247 ms 14156 KB Output is correct
21 Correct 243 ms 14332 KB Output is correct
22 Correct 234 ms 14028 KB Output is correct
23 Correct 250 ms 14180 KB Output is correct
24 Correct 231 ms 13844 KB Output is correct
25 Correct 248 ms 14052 KB Output is correct
26 Correct 234 ms 13856 KB Output is correct
27 Incorrect 3 ms 2796 KB Output isn't correct
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 4 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 2 ms 2924 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 73 ms 10104 KB Output is correct
11 Correct 77 ms 11744 KB Output is correct
12 Correct 82 ms 11880 KB Output is correct
13 Correct 84 ms 12132 KB Output is correct
14 Correct 73 ms 11240 KB Output is correct
15 Correct 72 ms 10212 KB Output is correct
16 Correct 234 ms 13820 KB Output is correct
17 Correct 215 ms 13460 KB Output is correct
18 Correct 219 ms 13904 KB Output is correct
19 Correct 205 ms 13132 KB Output is correct
20 Correct 254 ms 13840 KB Output is correct
21 Correct 247 ms 14156 KB Output is correct
22 Correct 243 ms 14332 KB Output is correct
23 Correct 234 ms 14028 KB Output is correct
24 Correct 250 ms 14180 KB Output is correct
25 Correct 231 ms 13844 KB Output is correct
26 Correct 248 ms 14052 KB Output is correct
27 Correct 234 ms 13856 KB Output is correct
28 Incorrect 3 ms 2796 KB Output isn't correct
29 Halted 0 ms 0 KB -