QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#1573#251272#61. Cut Cut Cut!HJY2022hos.lyric🐇Failed.2025-02-24 09:25:032025-02-24 09:25:03

详细

Extra Test:

Invalid Input

input:

7 11
5 7
4 6
6 7
1 7
2 7
6 7
1 5
1 6
1 3
1 4
2 5

output:


result:

FAIL duplicated edges

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#251272#61. Cut Cut Cut!hos.lyric🐇AC ✓241ms90272kbC++146.1kb2023-11-14 14:58:492023-11-14 14:58:50

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 <random>
#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")

////////////////////////////////////////////////////////////////////////////////
// 2^61 - 1 = 2'305'843'009'213'693'951
struct ModLong61 {
  static constexpr unsigned long long M = (1ULL << 61) - 1;
  unsigned long long x;
  constexpr ModLong61() : x(0ULL) {}
  constexpr ModLong61(unsigned x_) : x(x_) {}
  constexpr ModLong61(unsigned long long x_) : x(x_ % M) {}
  constexpr ModLong61(int x_) : x((x_ < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  constexpr ModLong61(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModLong61 &operator+=(const ModLong61 &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModLong61 &operator-=(const ModLong61 &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModLong61 &operator*=(const ModLong61 &a) {
    const unsigned __int128 y = static_cast<unsigned __int128>(x) * a.x;
    x = (y >> 61) + (y & M);
    x = (x >= M) ? (x - M) : x;
    return *this;
  }
  ModLong61 &operator/=(const ModLong61 &a) { return (*this *= a.inv()); }
  ModLong61 pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModLong61 a = *this, b = 1ULL; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModLong61 inv() const {
    unsigned long long a = M, b = x; long long y = 0, z = 1;
    for (; b; ) { const unsigned long long q = a / b; const unsigned long long c = a - q * b; a = b; b = c; const long long w = y - static_cast<long long>(q) * z; y = z; z = w; }
    assert(a == 1ULL); return ModLong61(y);
  }
  ModLong61 operator+() const { return *this; }
  ModLong61 operator-() const { ModLong61 a; a.x = x ? (M - x) : 0ULL; return a; }
  ModLong61 operator+(const ModLong61 &a) const { return (ModLong61(*this) += a); }
  ModLong61 operator-(const ModLong61 &a) const { return (ModLong61(*this) -= a); }
  ModLong61 operator*(const ModLong61 &a) const { return (ModLong61(*this) *= a); }
  ModLong61 operator/(const ModLong61 &a) const { return (ModLong61(*this) /= a); }
  template <class T> friend ModLong61 operator+(T a, const ModLong61 &b) { return (ModLong61(a) += b); }
  template <class T> friend ModLong61 operator-(T a, const ModLong61 &b) { return (ModLong61(a) -= b); }
  template <class T> friend ModLong61 operator*(T a, const ModLong61 &b) { return (ModLong61(a) *= b); }
  template <class T> friend ModLong61 operator/(T a, const ModLong61 &b) { return (ModLong61(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModLong61 &a) const { return (x == a.x); }
  bool operator!=(const ModLong61 &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModLong61 &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

#include <chrono>

#ifdef LOCAL
mt19937_64 rng(58);
#else
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#endif

////////////////////////////////////////////////////////////////////////////////


using Mint = ModLong61;

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

/*
  edge -> vector
  edge -> edge: random coefficient
  dims, f: span of incoming edges
*/
vector<int> dims;
vector<Mint> f[100'010][20];

vector<Mint> g[100'010];

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    A.resize(M);
    B.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
    }
    
    vector<vector<int>> graph(N), hparg(N);
    for (int i = 0; i < M; ++i) {
      graph[A[i]].push_back(B[i]);
      hparg[B[i]].push_back(A[i]);
    }
    
    const int K = graph[0].size();
    vector<int> ks(N, -1);
    for (int k = 0; k < K; ++k) {
      ks[graph[0][k]] = k;
    }
    
    dims.assign(N, 0);
    for (int v = 1; v < N; ++v) {
      int m = 0;
      if (~ks[v]) {
        g[m].assign(K, 0);
        g[m][ks[v]] = 1;
        ++m;
      }
      for (const int u : hparg[v]) if (u) {
        g[m].assign(K, 0);
        for (int i = 0; i < dims[u]; ++i) {
          const Mint coef = (unsigned long long)rng();
          for (int k = 0; k < K; ++k) {
            g[m][k] += coef * f[u][i][k];
          }
        }
        ++m;
      }
      int r = 0;
      for (int h = 0; h < K; ++h) {
        for (int i = r; i < m; ++i) if (g[i][h]) {
          swap(g[r], g[i]);
          break;
        }
        if (r < m && g[r][h]) {
          const Mint s = g[r][h].inv();
          for (int j = h; j < K; ++j) g[r][j] *= s;
          for (int i = r + 1; i < m; ++i) {
            const Mint t = g[i][h];
            if (t) for (int j = h; j < K; ++j) g[i][j] -= t * g[r][j];
          }
          ++r;
        }
      }
      dims[v] = r;
      for (int i = 0; i < r; ++i) {
        f[v][i].swap(g[i]);
      }
    }
    
    for (int v = 1; v < N; ++v) {
      if (v > 1) printf(" ");
      printf("%d", dims[v]);
    }
    puts("");
  }
  return 0;
}