Submission #874606

#TimeUsernameProblemLanguageResultExecution timeMemory
874606SUNWOOOOOOOO자매 도시 (APIO20_swap)C++17
6 / 100
329 ms47280 KiB
// #include "swap.h"
#include <bits/stdc++.h>
using namespace std;
using tint = array <int, 3>;
const int mxN = 200005;
vector <int> info1[mxN], info2[mxN]; // time, group
int n, m, deg[mxN], ftime[mxN]; // time when deg3 or cycle
tint edge[mxN];

struct dsu {
    int G[mxN];
    vector <int> Gelm[mxN];
    void init(){
        for (int i = 0; i < n; i++){
            G[i] = i;
            Gelm[i].push_back(i);
            info1[i].push_back(-1);
            info2[i].push_back(i);
        }
    }
    void uni(int time, int x, int y){
        x = G[x], y = G[y];
        if (x != y){
            if (Gelm[x].size() < Gelm[y].size()) swap(x, y);
            for (int elm : Gelm[y]) {
                G[elm] = x;
                Gelm[x].push_back(elm);
                info1[elm].push_back(time);
                info2[elm].push_back(x);
            }
            if (ftime[x] == -1 && ftime[y] != -1) ftime[x] = time;
            Gelm[y].clear();
        }
        if (x == y && ftime[x] == -1) ftime[x] = time;
    }
} dsu;

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    n = N, m = M;
    for (int i = 0; i < n; i++) ftime[i] = -1;
    for (int i = 0; i < m; i++) edge[i] = {W[i], U[i], V[i]};
    sort(edge, edge + m);

    dsu.init();
    for (int i = 0; i < m; i++){
        for (int j : {1, 2}){
            int x = edge[i][j];
            if (++deg[x] == 3){
                x = dsu.G[x];
                if (ftime[x] == -1) ftime[x] = i;
            }
        }
        dsu.uni(i, edge[i][1], edge[i][2]);
    }
}

int getMinimumFuelCapacity(int x, int y) {
    int low = 0, high = m - 1, ans = -1;
    while (low <= high){
        int mid = (low + high) / 2;
        int xi = upper_bound(info1[x].begin(), info1[x].end(), mid) - info1[x].begin() - 1;
        int yi = upper_bound(info1[y].begin(), info1[y].end(), mid) - info1[y].begin() - 1;
        x = info2[x][xi], y = info2[y][yi];
        if (x == y && ftime[x] != -1 && ftime[x] <= mid){
            ans = mid;
            high = mid - 1;
        }
        else low = mid + 1;
    }
    return (ans != -1 ? edge[ans][0] : -1);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...