Submission #937634

# Submission time Handle Problem Language Result Execution time Memory
937634 2024-03-04T09:54:51 Z IBory Swapping Cities (APIO20_swap) C++17
0 / 100
1 ms 344 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

const int MAX = 200007;
int N, M, R[MAX], D[MAX];
bool T[MAX], C[MAX], isLine;
vector<tuple<int, int, int>> E;

void init(int _N, int _M, vector<int> U, vector<int> V, vector<int> W) {
	N = _N, M = _M;
	for (int i = 0; i < M; ++i) {
        E.emplace_back(W[i], U[i], V[i]);
		D[U[i]]++;
		D[V[i]]++;
	}
	if (N == M + 1 && *max_element(D, D + MAX) == 2) isLine = 1;
	sort(E.begin(), E.end());
}

void Clear() {
	iota(R, R + MAX, 0);
	memset(T, 0, sizeof(T));
	memset(D, 0, sizeof(D));
    memset(C, 0, sizeof(C));
}

int Find(int n) {
    T[R[n]] |= T[n];
    C[R[n]] |= C[n];
	if (n == R[n]) return n;
	int r = Find(R[n]);
	T[r] |= T[n];
    C[r] |= C[n];
	return R[n] = r;
}

void Union(int a, int b) {
	if (++D[a] == 3) T[a] = 1;
	if (++D[b] == 3) T[b] = 1;
	a = Find(a), b = Find(b);
	if (a == b) {
        C[a] = 1;
        return;
    }
	R[a] = b;
	T[b] |= T[a];
    C[b] |= C[a];
}

int getMinimumFuelCapacity(int x, int y) {
	if (isLine) return -1;

	int l = -1, r = M - 1;
	while (l + 1 < r) {
		int mid = (l + r) >> 1;

		Clear();
		for (int i = 0; i <= mid; ++i) {
			auto [_, a, b] = E[i];
			Union(a, b);
		}

		bool ok = (Find(x) == Find(y)) && (T[Find(x)] || C[Find(x)]);
		(ok ? r : l) = mid;
	}

	auto [ans, a, b] = E[r];
	return ans;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -