Submission #365005

#TimeUsernameProblemLanguageResultExecution timeMemory
365005tatyamSwapping Cities (APIO20_swap)C++17
100 / 100
456 ms23660 KiB
#include <bits/stdc++.h> using namespace std; struct UnionFind{ vector<vector<pair<int, int>>> data; vector<int> flag_time, degree; UnionFind(int n = 0): data(n, {{-1, -1}}), flag_time(n, INT_MAX), degree(n){} int merged_time(int x) const { const auto [time, root] = data[x].back(); if(root < 0) return INT_MAX; return time; } int root(int t, int x) const { // after t if(t < merged_time(x)) return x; return root(t, data[x].back().second); } bool unite(int t, int x, int y){ bool flag = ++degree[x] >= 3 || ++degree[y] >= 3; x = root(t, x); y = root(t, y); if(x == y){ if(flag_time[x] == INT_MAX) flag_time[x] = t; return 0; } const int size_x = data[x].back().second, size_y = data[y].back().second; if(size_x > size_y) swap(x, y); if(flag_time[x] == INT_MAX && (flag || flag_time[y] != INT_MAX)) flag_time[x] = t; data[x].emplace_back(t, size_x + size_y); data[y].emplace_back(t, x); return 1; } } uf; vector<array<int, 3>> edge; void init(int N, int M, vector<int> U, vector<int> V, vector<int> W){ uf = UnionFind(N); edge.resize(M); for(int i = 0; i < M; i++) edge[i] = {U[i], V[i], W[i]}; sort(edge.begin(), edge.end(), [&](const auto& a, const auto& b){ return a[2] < b[2]; }); for(int i = 0; i < M; i++){ const auto& [U, V, W] = edge[i]; uf.unite(i + 1, U, V); } } int getMinimumFuelCapacity(int X, int Y){ auto check = [&](int t){ const int x = uf.root(t, X), y = uf.root(t, Y); return x == y && uf.flag_time[x] <= t; }; int ng = 0, ok = edge.size(); if(!check(ok)) return -1; while(ok - ng > 1){ const int cen = (ok + ng) / 2; (check(cen) ? ok : ng) = cen; } return edge[ok - 1][2]; }
#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...