답안 #673922

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
673922 2022-12-22T12:22:27 Z c2zi6 자매 도시 (APIO20_swap) C++17
컴파일 오류
0 ms 0 KB
#include "swap.h"

#include <bits/stdc++.h>

using namespace std;

struct EDGE {
    int u, v, w;
};

struct DSUNODE;
vector<DSUNODE> nodes;
struct DSUNODE {
    int vert;
    int parent;
    int distToParent;
    int size;
    int left;
    int right;
    int when;
    int leader(int mxpath = 2e9) {
        if (parent == -1 || distToParent > mxpath) return vert;
        return nodes[nodes[parent].leader(mxpath)].vert;
    }
};

vector<EDGE> edges;

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    edges = vector<EDGE>(M);
    nodes = vector<DSUNODE>(N);
    for (int i = 0; i < N; i++) {
        DSUNODE& a = nodes[i];
        a.parent = -1;
        a.distToParent = 0;
        a.size = 1;
        a.vert = a.left = a.right = i;
        a.when = 2e9;
    }
    for (int i = 0; i < M; i++) {
        edges[i] = {U[i], V[i], W[i]};
    }
    sort(edges.begin(), edges.end(), [](const EDGE& a, const EDGE& b){return a.w < b.w;});
    //cout << "WORKS" << endl;
    for (int i = 0; i < M; i++) {
        //cout << nodes[edges[i].u].leader() << " " << nodes[edges[i].v].leader() << endl;
        DSUNODE& u = nodes[nodes[edges[i].u].leader()];
        DSUNODE& v = nodes[nodes[edges[i].v].leader()];
        if (u.vert == v.vert) {
            if ((edges[i].u == u.left && edges[i].v == u.right) || (edges[i].u == u.right && edges[i].v == u.left)) {
                u.left = -1;
                u.right = -1;
                u.when = min(u.when, edges[i].w);
            }
            continue;
        }

        int newleft = -1, newright = -1;
        if (edges[i].u == u.left && edges[i].v == v.left) {
            newleft = u.right;
            newright = v.right;
        } else if (edges[i].u == u.left && edges[i].v == v.right) {
            newleft = u.right;
            newright = v.left;
        } else if (edges[i].u == u.right && edges[i].v == v.left) {
            newleft = u.left;
            newright = v.right;
        } else if (edges[i].u == u.right && edges[i].v == v.right) {
            newleft = u.left;
            newright = v.left;
        }

        if (u.size > v.size) {
            v.parent = u.vert;
            v.distToParent = edges[i].w;
            u.size += v.size;
            u.left = newleft;
            u.right = newright;
            if (newleft == -1) {
                u.when = min(u.when, edges[i].w);
            }
        } else {
            u.parent = v.vert;
            u.distToParent = edges[i].w;
            v.size += u.size;
            v.left = newleft;
            v.right = newright;
            if (newleft == -1) {
                v.when = min(v.when, edges[i].w);
            }
        }
    }
}

bool can(int X, int Y, int M) {
    int u = nodes[X].leader(M), v = nodes[Y].leader(M);
    return u == v && M >= nodes[u].when;
}

int getMinimumFuelCapacity(int X, int Y) {
    int l = 0, r = 1e9;
    while (l + 1 < r) {
        int m = (l + r) / 2;
        if (can(X, Y, m)) r = m;
        else l = m;
    }
    return r;
}

int main() {
  //  int N, M;
  //assert(2 == scanf("%d %d", &N, &M));

  //std::vector<int> U(M), V(M), W(M);
  //for (int i = 0; i < M; ++i) {
  //  assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
  //}

  int Q;
  assert(1 == scanf("%d", &Q));

  std::vector<int> X(Q), Y(Q);
  for (int i = 0; i < Q; ++i) {
    assert(2 == scanf("%d %d", &X[i], &Y[i]));
  }

  init(5, 6, {0, 0, 1, 1, 1, 2}, {1, 2, 2, 3, 4, 3}, {4, 4, 1, 2, 10, 3});

  std::vector<int> minimum_fuel_capacities(Q);
  for (int i = 0; i < Q; ++i) {
    minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
  }

  for (int i = 0; i < Q; ++i) {
    printf("%d\n", minimum_fuel_capacities[i]);
  }

  return 0;
}

Compilation message

/usr/bin/ld: /tmp/cc6LZsPF.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccD5Cq4D.o:swap.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status