QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#183149#4898. 基础图论练习题hos_lyric#0 21ms16728kbC++145.8kb2023-09-19 06:20:142024-07-04 02:03:25

Judging History

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

  • [2024-07-04 02:03:25]
  • 评测
  • 测评结果:0
  • 用时:21ms
  • 内存:16728kb
  • [2023-09-19 06:20:14]
  • 提交

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; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
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>;

int root(vector<int> &uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = root(uf, uf[u]));
}
bool connect(vector<int> &uf, int u, int v) {
  u = root(uf, u);
  v = root(uf, 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;
int A, B;
vector<Int> D;
vector<int> X;
vector<Int> U, V;
vector<int> W;

vector<pair<int, int>> es;


namespace brute {
constexpr int F = 19;
vector<int> ufs[F];

// (u, v), (u + 1, v + 1), ..., (u + 2^f-1, v + 2^f-1)
int rec(int f, int u, int v) {
  int ret = 0;
  if (connect(ufs[f], u, v)) {
    if (f) {
      ret += rec(f - 1, u, v);
      ret += rec(f - 1, u + (1 << (f-1)), v + (1 << (f-1)));
    } else {
      ret += 1;
    }
  }
  return ret;
}

Mint run() {
  for (int f = 0; f < F; ++f) {
    ufs[f].assign(N, -1);
  }
  Mint ans = 0;
  for (const auto &e : es) {
    int num = 0;
    if (e.second < A) {
      const int a = e.second;
      // const int f = 31 - __builtin_clz(N - D[a]);
      // num += rec(f, 0, D[a]);
      // num += rec(f, N - D[a] - (1 << f), N - (1 << f));
for(int u=0;u<N-D[a];++u)num+=rec(0,u,u+D[a]);
    } else {
      const int b = e.second - A;
      num += rec(0, U[b], V[b]);
    }
// cerr<<e<<": "<<num<<endl;
    ans += e.first * num;
  }
  return ans;
}
}  // brute


int main() {
  for (; ~scanf("%lld%d%d", &N, &A, &B); ) {
    D.resize(A);
    X.resize(A);
    for (int a = 0; a < A; ++a) {
      scanf("%lld%d", &D[a], &X[a]);
    }
    U.resize(B);
    V.resize(B);
    W.resize(B);
    for (int b = 0; b < B; ++b) {
      scanf("%lld%lld%d", &U[b], &V[b], &W[b]);
    }
cerr<<"N = "<<N<<", A = "<<A<<", B = "<<B<<endl;
    
    es.resize(A + B);
    for (int a = 0; a < A; ++a) {
      es[a] = make_pair(X[a], a);
    }
    for (int b = 0; b < B; ++b) {
      es[A + b] = make_pair(W[b], A + b);
    }
    sort(es.begin(), es.end());
    
    Mint ans = 0;
    {
      ans = brute::run();
    }
    printf("%u\n", ans.x);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 21ms
memory: 16728kb

input:

161199 9 46510
147335 540442844
159493 801351455
149342 821625305
128476 843250712
95524 275754315
139315 106523502
93575 680460786
155498 328812257
146020 410466645
79992 141967 50596784
152210 68644 268349216
72549 96959 42994091
93869 27394 945120577
2909 81886 270684270
12735 35026 871917997
974...

output:

32595942

result:

wrong answer 1st numbers differ - expected: '359714743', found: '32595942'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Runtime Error

Test #11:

score: 0
Runtime Error

input:

569435269457904707 2 0
490445920091092693 772271583
144842828305643603 609043885

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Runtime Error

Test #31:

score: 0
Runtime Error

input:

755526150476311190 942 0
492334667739348527 1
755523898623296976 1
532486636690994793 1
755526150476030559 1
755526150476249097 1
502164090270592200 1
657422656495814703 1
487200614853438190 1
311037325561173142 1
755526150475651155 1
125287404340238660 1
755524914808674090 1
755526150476177007 1
75...

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #3:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #5:

0%

Subtask #9:

score: 0
Skipped

Dependency #1:

0%