結果

問題 No.2985 May Count Induced C4 Subgraphs
ユーザー 👑 hos.lyric
提出日時 2024-12-10 00:46:24
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,388 ms / 5,000 ms
コード長 7,297 bytes
コンパイル時間 1,800 ms
コンパイル使用メモリ 133,704 KB
実行使用メモリ 30,592 KB
最終ジャッジ日時 2024-12-10 00:46:52
合計ジャッジ時間 24,510 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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


// https://x.com/NachiaVivias/status/1863300035263553791/photo/1

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

vector<vector<int>> graph;
vector<vector<pair<int, int>>> graphI;

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];
    }
    
    graph.assign(N, {});
    graphI.assign(N, {});
    for (int i = 0; i < M; ++i) {
      graph[A[i]].push_back(B[i]);
      graph[B[i]].push_back(A[i]);
      graphI[A[i]].emplace_back(B[i], i);
      graphI[B[i]].emplace_back(A[i], i);
    }
    
    vector<int> deg(N);
    for (int u = 0; u < N; ++u) deg[u] = graph[u].size();
    vector<int> us(N);
    for (int u = 0; u < N; ++u) us[u] = u;
    sort(us.begin(), us.end(), [&](int u, int v) -> bool {
      return (deg[u] > deg[v]);
    });
    vector<int> ord(N, -1);
    for (int j = 0; j < N; ++j) ord[us[j]] = j;
// cerr<<"us = "<<us<<endl;
    
    Mint Y[10] = {};
    vector<int> tri(M, 0);
    
    // \sum[u] min{deg(u)^2, M} <= \sum[u] deg[u] sqrt(M) = O(M sqrt(M))
    {
      vector<int> fs(N, 0);
      for (int j = 0; j < N; ++j) {
        const int u = us[j];
        for (const auto &vi : graphI[u]) if (ord[u] < ord[vi.first]) {
          fs[vi.first] = ~vi.second;
        }
        for (const auto &vi : graphI[u]) if (ord[u] < ord[vi.first]) {
          const int v = vi.first;
          for (const auto &wi : graphI[v]) if (ord[v] < ord[wi.first]) {
            const int w = wi.first;
            if (fs[w]) {
              // C[3]
// cerr<<"C3 "<<u<<" "<<v<<" "<<w<<endl;
              Y[5] -= 3;
              Y[6] += 1;
              Y[8] += (deg[u] + deg[v] + deg[w] - 6);
              ++tri[vi.second];
              ++tri[wi.second];
              ++tri[~fs[w]];
            }
          }
        }
        for (const int v : graph[u]) if (ord[u] < ord[v]) {
          fs[v] = 0;
        }
        for (const int v : graph[u]) if (ord[u] < ord[v]) {
          for (const int w : graph[v]) if (ord[u] < ord[w]) {
            // C[4]
            Y[7] += fs[w];
            ++fs[w];
          }
        }
        for (const int v : graph[u]) if (ord[u] < ord[v]) {
          for (const int w : graph[v]) if (ord[u] < ord[w]) {
            --fs[w];
          }
        }
      }
    }
// cerr<<"tri = "<<tri<<endl;
    
    for (int i = 0; i < M; ++i) {
      const Int t = tri[i];
      Y[9] += t*(t-1)/2;
    }
    for (int u = 0; u < N; ++u) {
      const Int d = deg[u];
      const Int d2 = d*(d-1)/2;
      const Int d3 = d*(d-1)*(d-2)/6;
      Y[2] += d2;
      Y[3] -= d2;
      Y[4] += d3;
    }
    Y[0] += (unsigned)((__int128)N*(N-1)*(N-2)*(N-3)/24 % MO);
    Y[1] += Mint(M) * Mint((Int)(N-2)*(N-3)/2);
    Y[2] *= (N-3);
    Y[3] += (Int)M*(M-1)/2;
    for (int i = 0; i < M; ++i) Y[5] += (Int)(deg[A[i]] - 1) * (deg[B[i]] - 1);
    Y[6] *= (N-3);
    
cerr<<"Y = ";pv(Y,Y+10);
    const Mint T = Mint(-1)/3;
    const Mint COEF[10] = {1, -1, 1, 1, -1, -1, -1, Mint(2)/3, 1, Mint(-2)/3};
cerr<<"sample: "<<(2+T*1)<<" "<<(13+T*2)<<endl;
    Mint ans = 0;
    for (int i = 0; i < 10; ++i) ans += COEF[i] * Y[i];
    printf("%u %u\n", T.x, ans.x);
    
#ifdef LOCAL
map<vector<int>,int>brt;
for(int p=0;p<1<<N;++p)if(__builtin_popcount(p)==4){
 vector<int>ds;
 for(int u=0;u<N;++u)if(p>>u&1){
  int d=0;
  for(const int v:graph[u])if(p>>v&1)++d;
  ds.push_back(d);
 }
 sort(ds.begin(),ds.end(),greater<int>{});
 ++brt[ds];
}
for(const auto&kv:brt)cerr<<kv<<endl;
#endif
  }
  return 0;
}
0