QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#283008#6330. XOR ReachableMisuki#WA 242ms132904kbC++204.9kb2023-12-13 17:45:402023-12-13 17:45:40

Judging History

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

  • [2023-12-13 17:45:40]
  • 评测
  • 测评结果:WA
  • 用时:242ms
  • 内存:132904kb
  • [2023-12-13 17:45:40]
  • 提交

answer

#pragma GCC optimize("O2")
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <compare>
#include <complex>
#include <concepts>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numbers>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>

//#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

namespace R = std::ranges;
namespace V = std::views;

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = tuple<int, int, int>;
using ldb = long double;
//#define double ldb

template<class T>
ostream& operator<<(ostream& os, const pair<T, T> pr) {
  return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(const T &X : arr)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(const T &X : vec)
    os << X << ' ';
  return os;
}

/**
 * template name: DSU
 * author: Misuki
 * last update: 2023/01/07
 * verify: Library Checker - Unionfind
 */

struct DSU {
  vector<int> dep;
  vector<int> bos;
  vector<int> sz;
  vector<array<int, 2>> dh, bh, sh;
  vector<ll> ch;
  int size;
  ll cost;

  DSU(int _size) : size(_size), dep(_size, 0), bos(_size), sz(_size, 1), cost(0ll) {
    iota(bos.begin(), bos.end(), 0);
  }

  int query(int V) {
    if (bos[V] == V)
      return V;
    else
      return bos[V];
  }

  bool merge(int V1, int V2) {
    int B1 = query(V1);
    int B2 = query(V2);

    if (B1 == B2) {
      return false;
    }

    dh.push_back({B1, dep[B1]});
    bh.push_back({B1, bos[B1]});
    sh.push_back({B1, sz[B1]});
    dh.push_back({B2, dep[B2]});
    bh.push_back({B2, bos[B2]});
    sh.push_back({B2, sz[B2]});
    ch.push_back(cost);

    cost -= (ll)sz[B1] * (sz[B1] - 1) / 2 + (ll)sz[B2] * (sz[B2] - 1) / 2;
    cost += (ll)(sz[B1] + sz[B2]) * (sz[B1] + sz[B2] - 1) / 2;

    if (dep[B1] < dep[B2]) {
      bos[B1] = B2, sz[B2] += sz[B1];
    } else if (dep[B1] > dep[B2]) {
      bos[B2] = B1, sz[B1] += sz[B2];
    } else {
      bos[B1] = B2, sz[B2] += sz[B1];
      dep[B2] += 1;
    }

    return true;
  }

  void rollback() {
    for(int i : {0, 1}) {
      dep[dh.back()[0]] = dh.back()[1]; dh.pop_back();
      bos[bh.back()[0]] = bh.back()[1]; bh.pop_back();
      sz[sh.back()[0]] = sh.back()[1]; sh.pop_back();
    }
    cost = ch.back(); ch.pop_back();
  }
};

vector<array<int, 2>> nxt(1, {-1, -1});
vector<vector<int>> ope(1), qry(1);
int go(int v, int bit) {
  if (nxt[v][bit] == -1) {
    nxt[v][bit] = ssize(nxt);
    nxt.push_back({-1, -1});
    ope.emplace_back();
    qry.emplace_back();
  }
  return nxt[v][bit];
}
void insert(int c, int eid, int k) {
  for(int v = 0, bit = 29; bit >= 0; bit--) {
    if (k >> bit & 1) {
      int tmp = go(v, c >> bit & 1);
      ope[tmp].emplace_back(eid);
      v = go(v, ~c >> bit & 1);
    } else {
      v = go(v, c >> bit & 1);
    }
  }
}
void insert2(int d, int qid) {
  int v = 0;
  for(int bit = 29; bit >= 0; bit--)
    v = go(v, d >> bit & 1);
  qry[v].emplace_back(qid);
}

signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  int n, m, k; cin >> n >> m >> k;
  vector<array<int, 3>> e(m);
  for(auto &[u, v, w] : e) {
    cin >> u >> v >> w;
    u--, v--;
  }
  int q; cin >> q;
  vector<int> d(q);
  for(int &x : d)
    cin >> x;

  for(int i = 0; i < m; i++)
    insert(e[i][2], i, k);
  for(int i = 0; i < q; i++)
    insert2(d[i], i);

  DSU dsu(n);
  vector<ll> ans(q);
  auto dfs = [&](int v, auto self) -> void {
    if (v == -1) return;
    int cnt = 0;
    for(int eid : ope[v])
      cnt += dsu.merge(e[eid][0], e[eid][1]);
    for(int qid : qry[v])
      ans[qid] = dsu.cost;
    self(nxt[v][0], self);
    self(nxt[v][1], self);
    for(int i = 0; i < cnt; i++)
      dsu.rollback();
  };

  dfs(0, dfs);

  for(ll x : ans)
    cout << x << '\n';

  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3588kb

input:

4 5 5
1 2 17
1 3 4
2 3 20
2 4 3
3 4 5
4
0
7
16
167

output:

2
6
3
0

result:

ok 4 number(s): "2 6 3 0"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3708kb

input:

9 13 488888932
2 7 771479959
3 8 783850182
5 7 430673756
6 8 350738034
4 9 400768807
2 3 83653266
1 2 829786563
5 8 357613791
7 9 579696618
3 7 423191200
3 5 867380255
1 9 907715012
6 9 1033650694
8
498260055
144262908
117665696
848664012
983408133
32610599
478007408
134182829

output:

16
7
5
13
13
16
16
5

result:

ok 8 numbers

Test #3:

score: -100
Wrong Answer
time: 242ms
memory: 132904kb

input:

446 99235 764320136
1 2 467639025
1 39 240791819
1 197 320023434
1 391 968343602
1 116 841220144
1 345 697806443
1 409 489643820
1 339 733516272
1 89 560099922
1 431 722346703
1 433 246809211
1 344 769002876
1 234 597076758
1 178 505730391
1 75 826728597
1 74 14756981
1 63 280238017
1 288 638627144
...

output:

3206785848745
3191047971721
3203945009821
3157893732805
99235
3155330867545
3175348807981
99235
99235
99235
99235
99235
99235
99235
3159688357525
99235
99235
99235
3202395991921
3156868461853
3204203215885
99235
99235
99235
99235
99235
99235
99235
3209886381505
3158662795261
3199557098545
99235
3198...

result:

wrong answer 1st numbers differ - expected: '99235', found: '3206785848745'