QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#275326#6520. Classic Problemucup-team1321#WA 439ms13492kbC++233.6kb2023-12-04 16:44:532023-12-04 16:44:54

Judging History

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

  • [2023-12-04 16:44:54]
  • 评测
  • 测评结果:WA
  • 用时:439ms
  • 内存:13492kb
  • [2023-12-04 16:44:53]
  • 提交

answer

#include <bits/stdc++.h>
#include <unordered_map>
#ifndef LOCAL
#define debug(...) 42
#else
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#endif
#define rep1(a) for (auto i = 0; i < a; i++)
#define rep2(i, a) for (auto i = 0; i < a; i++)
#define rep3(i, a, b) for (auto i = a; i < b; i++)
#define rep4(i, a, b, c) for (auto i = a; i < b; i += c)
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)

#define pb emplace_back
using namespace std;
template <typename T, typename T2> void cmin(T &x, const T2 &y) {
  x = x < y ? x : y;
}
template <typename T, typename T2> void cmax(T &x, const T2 &y) {
  x = x > y ? x : y;
}
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
template <class T> using vc = vector<T>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
mt19937 rng(time(NULL));
const int inf = 1000000000;
const ll lnf = 1000000000000000000;
#define sz(x) int((x).size())
#define all(x) begin(x), end(x)
#define fi first
#define se second
struct unionfind {
  vector<int> p;
  unionfind(int N) { p = vector<int>(N, -1); }
  int root(int x) { return p[x] < 0 ? x : p[x] = root(p[x]); }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (p[x] < p[y]) {
        swap(x, y);
      }
      p[y] += p[x];
      p[x] = y;
    }
  }
  int size(int x) { return -p[root(x)]; }
};
void solve() {
  int n, m;
  cin >> n >> m;
  if (m == 0) {
    cout << n - 1 << "\n";
    return;
  }
  map<pii, int> mp;
  ll ans = 0;
  vi cut{0};
  vc<array<int, 3>> e;
  rep(i, m) {
    int x, y, z;
    cin >> x >> y >> z;
    --x, --y;
    if (x + 1 == y) {
      if (z > 1) {
        cut.push_back(y);
        mp[pii(x, y)] = z;
        e.pb(array<int, 3>{x, y, z});
      }
      if (z == 1)
        continue;
      if (z == 0) {
        ans--;
      }
    } else {
      mp[pii(x, y)] = z;
      e.pb(array<int, 3>{x, y, z});
    }
  }
  cut.pb(n);
  sort(all(cut));
  cut.resize(unique(all(cut)) - cut.begin());
  vc<pii> seg;
  rep(i, cut.size() - 1) {
    int l = cut[i];
    int r = cut[i + 1] - 1;
    seg.pb(l, r);
    ans += r - l;
  }
  // debug(seg);
  // debug(ans);
  auto belong = [&](int x) { return --upper_bound(all(cut), x) - cut.begin(); };
  // debug(belong(0), belong(2));
  vc<array<int, 3>> E;
  for (auto [x, y, z] : e) {
    E.pb(array<int, 3>{belong(x), belong(y), z});
  }
  auto try_w = [&](int u, int v, int w) {
    int l = max(seg[u].first, seg[v].first - w);
    int r = min(seg[u].second, seg[v].second - w);
    for (int p = l; p <= r; p++) {
      if (!mp.count(pii(p, p + w))) {
        return true;
      }
    }
    return false;
  };
  int N = seg.size();
  rep(w, 1, 700) {
    rep(i, N) {
      int j = i + 1;
      while (j < N && seg[j].first - seg[i].second <= w) {
        j += 1;
      }
      // [i, j)
      for (int k = i + 1; k < j; k++) {
        if (try_w(i, k, w)) {
          E.pb(array<int, 3>{i, k, w});
        }
      }
    }
  }
  sort(all(E), [&](auto x, auto y) { return x[2] < y[2]; });
  unionfind F(N);
  for (auto [u, v, w] : E) {
    if (F.same(u, v))
      continue;
    F.unite(u, v);
    ans += w;
  }
  cout << ans << "\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3572kb

input:

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

output:

4
4
1000000003

result:

ok 3 number(s): "4 4 1000000003"

Test #2:

score: -100
Wrong Answer
time: 439ms
memory: 13492kb

input:

16
1000000000 0
447 99681
1 2 1000000000
1 3 1000000000
2 3 1000000000
1 4 1000000000
2 4 1000000000
3 4 1000000000
1 5 1000000000
2 5 1000000000
3 5 1000000000
4 5 1000000000
1 6 1000000000
2 6 1000000000
3 6 1000000000
4 6 1000000000
5 6 1000000000
1 7 1000000000
2 7 1000000000
3 7 1000000000
4 7 ...

output:

999999999
446000000000
732256441
999999999
999999999
999999999
2295
1112
5457
4854
4290
8392
1251
3252
3644
1121

result:

wrong answer 4th numbers differ - expected: '999999680', found: '999999999'