QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#114917#5048. All Pair Maximum Flowhos_lyricWA 1ms3748kbC++146.5kb2023-06-24 01:36:042023-06-24 01:36:08

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-24 01:36:08]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3748kb
  • [2023-06-24 01:36:04]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;


vector<int> uf;
int root(int u) {
  return (uf[u] < 0) ? u : (uf[u] = root(uf[u]));
}
bool connect(int u, int v) {
  u = root(u);
  v = root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}


int N, M;
vector<int> A, B;
vector<Int> C;

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    A.resize(M);
    B.resize(M);
    C.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d%lld", &A[i], &B[i], &C[i]);
      --A[i];
      --B[i];
    }
    
    vector<vector<pair<int, int>>> graph(N);
    for (int i = 0; i < M; ++i) {
      graph[A[i]].emplace_back(B[i], i);
      graph[B[i]].emplace_back(A[i], i);
    }
    for (int u = 0; u < N; ++u) {
      for (auto &edge : graph[u]) if (edge.first < u) {
        edge.first += N;
      }
      sort(graph[u].begin(), graph[u].end());
    }
    
    using Entry = pair<Int, pair<int, pair<int, int>>>;
    priority_queue<Entry, vector<Entry>, greater<Entry>> que;
    vector<int> outer(M, 0);
    auto cs = C;
    for (int i = 0; i < M; ++i) {
      int u = A[i], v = B[i];
      if ((v + 1) % N == u) {
        swap(u, v);
      }
      if ((u + 1) % N == v) {
        outer[i] = 1;
        que.emplace(cs[i], make_pair(i, make_pair(u, v)));
      }
    }
    for (; !que.empty(); ) {
      const Int c = que.top().first;
      const int i = que.top().second.first;
      const int u = que.top().second.second.first;
      const int v = que.top().second.second.second;
      que.pop();
      if (outer[i] == 1 && cs[i] == c) {
// cerr<<c<<" "<<i<<" "<<u<<" "<<v<<endl;
        // face from u to v in negative direction
        for (int w = v, x = u; ; ) {
          if (w < x) {
            w += N;
          }
          const int pos = lower_bound(graph[x].begin(), graph[x].end(), make_pair(w, -1)) - graph[x].begin();
          assert(pos + 1 < (int)graph[x].size());
          const int y = graph[x][pos + 1].first % N;
          const int j = graph[x][pos + 1].second;
          if (j == i) {
            break;
          }
// cerr<<"  "<<x<<" "<<y<<endl;
          ++outer[j];
          que.emplace(cs[j] += c, make_pair(j, make_pair(x, y)));
          w = x; x = y;
        }
        outer[i] = -1;
      }
    }
// cerr<<"outer = "<<outer<<endl;
    
    // now Gomory-Hu tree
    vector<pair<Int, int>> es;
    for (int i = 0; i < M; ++i) if (~outer[i]) {
      es.emplace_back(cs[i], i);
    }
    sort(es.begin(), es.end(), greater<pair<Int, int>>{});
    uf.assign(N, -1);
    Int ans = 0;
    for (const auto &e : es) {
      const int i = e.second;
      const int ru = root(A[i]);
      const int rv = root(B[i]);
      assert(ru != rv);
      ans += cs[i] * (Int)(-uf[ru]) * (-uf[rv]);
      connect(ru, rv);
    }
    printf("%lld\n", ans);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6 8
1 2 1
2 3 10
3 4 100
4 5 1000
5 6 10000
6 1 100000
1 4 1000000
1 5 10000000

output:

12343461

result:

ok 1 number(s): "12343461"

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3748kb

input:

20 30
5 7 9066926
13 15 636587393
1 12 234024833
7 11 863853881
7 9 961926707
12 20 525748907
7 10 217196987
15 20 715248370
17 19 577652480
16 19 78750484
1 2 216519168
2 3 26083428
3 4 381598120
4 5 78513523
5 6 106643887
6 7 20069507
7 8 467260856
8 9 383736316
9 10 400897545
10 11 404258163
11 1...

output:

101681004988

result:

wrong answer 1st numbers differ - expected: '858325335', found: '101681004988'