QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#375122#7415. Fast Spanning TreeXiaohubaTL 4ms36636kbC++234.1kb2024-04-02 21:50:112024-04-02 21:50:12

Judging History

你现在查看的是最新测评结果

  • [2024-04-02 21:50:12]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:36636kb
  • [2024-04-02 21:50:11]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

// #define LOCK_GETCHAR
// #define USE_INT_128

#if __cplusplus < 201400
#warning "Please use c++14 or higher."
#define CONSTEXPR_FUNC
#define ENABLE_IF_INT
#else
#define CONSTEXPR_FUNC constexpr
#define ENABLE_IF_INT , enable_if_t<_is_integer<T>, int> = 0
template <class T> constexpr bool _is_integer = numeric_limits<T>::is_integer;
template <> constexpr bool _is_integer<bool> = false;
template <> constexpr bool _is_integer<char> = false;
#ifdef USE_INT_128
template <> constexpr bool _is_integer<__int128> = true;
template <> constexpr bool _is_integer<__uint128_t> = true;
#endif
template <class T ENABLE_IF_INT>
constexpr T INF = numeric_limits<T>::max() >> 1;
#endif

#if !defined(_WIN32) && !defined(LOCK_GETCHAR)
#define getchar getchar_unlocked
#endif

#define il inline
#define mkp make_pair
#define fi first
#define se second
#define For(i, j, k) for (decltype(j - k) i = (j); i <= (k); ++i)     // NOLINT
#define ForDown(i, j, k) for (decltype(j - k) i = (j); i >= (k); --i) // NOLINT
#define pb push_back
#define eb emplace_back
#ifndef ONLINE_JUDGE
#define FileIO(filename)                                                       \
  freopen(filename ".in", "r", stdin);                                         \
  freopen(filename ".out", "w", stdout)
#else
#define FileIO(filename) void(0)
#endif

using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef USE_INT_128
using lll = __int128_t;
using ulll = __uint128_t;
#endif

// clang-format off
template<typename T> constexpr il T sq(const T & x){ return x * x; }
template<typename T> CONSTEXPR_FUNC il void cmin(T & x, const T &y){ x = min(x, y); }
template<typename T> CONSTEXPR_FUNC il void cmax(T & x, const T &y){ x = max(x, y);}
template<typename T> CONSTEXPR_FUNC il T qpow(T x, ull y, T mod){T ans = 1; x %= mod; while (y) { if(y & 1)(ans *= x) %= mod;(x *= x) %= mod; y >>= 1;} return ans;}
template<typename T> CONSTEXPR_FUNC il T qpow(T x, ull y){T ans = 1; while (y) {if(y & 1) ans *= x;x *= x;y >>= 1;} return ans;}
template<typename T ENABLE_IF_INT> il void read(T &x){ x = 0; int f = 1; int c = getchar(); while(!isdigit(c)) {if (c == '-') f = -1;c = getchar();} while(isdigit(c)) {x = x * 10 + c - '0';c = getchar();} x *= f;}
template<typename T, typename ... Args> il void read(T &x, Args &... y){ read(x); read(y...); }
// clang-format on

// File head end

namespace {
constexpr ll MAXN = 6e5 + 5;
int n, m, f[MAXN];
ll w[MAXN];
set<pair<ll, int>> g[MAXN];
set<pair<ll, int>>::iterator iter[MAXN];
tuple<int, int, ll> A[MAXN];
priority_queue<int, vector<int>, greater<>> pq;
int find(int x) { return f[x] < 0 ? x : f[x] = find(f[x]); }
il void upd(int x, bool flg) {
  int u = find(get<0>(A[x])), v = find(get<1>(A[x]));
  if (u == v)
    return;
  ll val = get<2>(A[x]) - w[u] - w[v];
  if (flg)
    g[u].erase(iter[x << 1]), g[v].erase(iter[x << 1 | 1]);
  if (val <= 0)
    return pq.emplace(x), void();
  bool _ok;
  tie(iter[x << 1], _ok) = g[u].emplace(w[u] + (val >> 1), x << 1);
  tie(iter[x << 1 | 1], _ok) = g[v].emplace(w[v] + (val >> 1), x << 1 | 1);
  return;
}

il void Main() {
  read(n, m);
  For(i, 1, n) read(w[i]), f[i] = -1;
  For(i, 1, m) {
    int x, y, z;
    read(x, y, z), A[i] = {x, y, z};
    upd(i, 0);
  }
  vector<int> Ans;
  while (!pq.empty()) {
    int u = pq.top();
    pq.pop();
    int p = find(get<0>(A[u])), q = find(get<1>(A[u]));
    if (p == q)
      continue;
    Ans.eb(u);
    if (-f[p] < -f[q])
      swap(p, q);
    f[p] += f[q], w[p] += w[q], f[q] = p;
    for (auto it = g[q].begin(); it != g[q].end();) {
      bool _ok;
      tie(iter[it->se], _ok) = g[p].emplace(*it), assert(_ok);
      it = g[q].erase(it);
    }
    while (!g[p].empty() && g[p].begin()->fi <= w[p])
      upd(g[p].begin()->se >> 1, 1);
  }
  cout << Ans.size() << '\n';
  for (int x : Ans)
    cout << x << ' ';
}
} // namespace

signed main() { return Main(), 0; }

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 36636kb

input:

5 5
1 4 3 4 0
4 5 5
3 1 1
2 5 2
4 3 1
4 1 4

output:

4
2 3 1 4 

result:

ok 5 number(s): "4 2 3 1 4"

Test #2:

score: -100
Time Limit Exceeded

input:

3 5
3 2 2
1 2 6
1 2 6
1 2 3
1 2 6
2 3 6

output:


result: