結果

問題 No.2160 みたりのDominator
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-12-11 03:36:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 5,587 bytes
コンパイル時間 1,297 ms
コンパイル使用メモリ 111,720 KB
実行使用メモリ 31,484 KB
最終ジャッジ日時 2024-10-15 03:05:20
合計ジャッジ時間 7,734 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 32 ms
23,056 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 2 ms
5,248 KB
testcase_17 AC 2 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 2 ms
5,248 KB
testcase_21 AC 2 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 2 ms
5,248 KB
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 2 ms
5,248 KB
testcase_28 AC 2 ms
5,248 KB
testcase_29 AC 2 ms
5,248 KB
testcase_30 AC 2 ms
5,248 KB
testcase_31 AC 2 ms
5,248 KB
testcase_32 AC 2 ms
5,248 KB
testcase_33 AC 2 ms
5,248 KB
testcase_34 AC 38 ms
29,780 KB
testcase_35 AC 2 ms
5,248 KB
testcase_36 AC 2 ms
5,248 KB
testcase_37 AC 2 ms
5,248 KB
testcase_38 AC 2 ms
5,248 KB
testcase_39 AC 2 ms
5,248 KB
testcase_40 AC 48 ms
23,400 KB
testcase_41 AC 43 ms
22,624 KB
testcase_42 AC 26 ms
15,004 KB
testcase_43 AC 37 ms
18,552 KB
testcase_44 AC 47 ms
22,728 KB
testcase_45 AC 46 ms
21,104 KB
testcase_46 AC 40 ms
19,428 KB
testcase_47 AC 24 ms
12,316 KB
testcase_48 AC 31 ms
14,440 KB
testcase_49 AC 39 ms
16,584 KB
testcase_50 AC 38 ms
16,484 KB
testcase_51 AC 30 ms
16,592 KB
testcase_52 AC 40 ms
21,408 KB
testcase_53 AC 47 ms
22,380 KB
testcase_54 AC 27 ms
14,436 KB
testcase_55 AC 115 ms
25,724 KB
testcase_56 AC 104 ms
23,288 KB
testcase_57 AC 109 ms
23,804 KB
testcase_58 AC 123 ms
30,200 KB
testcase_59 AC 110 ms
25,488 KB
testcase_60 AC 110 ms
31,100 KB
testcase_61 AC 112 ms
24,956 KB
testcase_62 AC 125 ms
31,484 KB
testcase_63 AC 115 ms
26,492 KB
testcase_64 AC 104 ms
25,212 KB
testcase_65 AC 54 ms
19,572 KB
testcase_66 AC 60 ms
18,196 KB
testcase_67 AC 62 ms
18,856 KB
testcase_68 AC 117 ms
29,212 KB
testcase_69 AC 86 ms
22,372 KB
testcase_70 AC 118 ms
29,412 KB
testcase_71 AC 44 ms
17,676 KB
testcase_72 AC 84 ms
27,012 KB
testcase_73 AC 116 ms
26,456 KB
testcase_74 AC 115 ms
25,116 KB
testcase_75 AC 20 ms
15,752 KB
testcase_76 AC 17 ms
13,264 KB
testcase_77 AC 66 ms
25,760 KB
testcase_78 AC 79 ms
27,152 KB
testcase_79 AC 13 ms
10,004 KB
testcase_80 AC 15 ms
10,968 KB
testcase_81 AC 25 ms
17,776 KB
testcase_82 AC 29 ms
19,220 KB
testcase_83 AC 33 ms
16,788 KB
61_evil_bias_nocross_01.txt AC 48 ms
23,400 KB
61_evil_bias_nocross_02.txt AC 43 ms
22,756 KB
61_evil_bias_nocross_03.txt AC 26 ms
15,004 KB
61_evil_bias_nocross_04.txt AC 35 ms
18,536 KB
61_evil_bias_nocross_05.txt AC 45 ms
22,728 KB
61_evil_bias_nocross_06.txt AC 46 ms
23,264 KB
61_evil_bias_nocross_07.txt AC 30 ms
16,468 KB
61_evil_bias_nocross_08.txt AC 40 ms
21,276 KB
61_evil_bias_nocross_09.txt AC 45 ms
22,504 KB
61_evil_bias_nocross_10.txt AC 26 ms
14,436 KB
61_evil_bias_nocross_11.txt AC 33 ms
18,100 KB
61_evil_bias_nocross_12.txt AC 41 ms
20,740 KB
権限があれば一括ダウンロードができます

ソースコード

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


struct Scc {
  int n;
  vector<int> as, bs;
  vector<int> ptr, zu, us;

  int l;
  vector<int> ids;
  int operator[](int u) const { return ids[u]; }

  explicit Scc(int n_) : n(n_), as(), bs(), l(-1) {}
  void ae(int u, int v) {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    as.push_back(u);
    bs.push_back(v);
  }

  void dfs0(int u) {
    if (!ids[u]) {
      ids[u] = -1;
      for (int i = ptr[u]; i < ptr[u + 1]; ++i) dfs0(zu[i]);
      us.push_back(u);
    }
  }
  void dfs1(int u) {
    if (!~ids[u]) {
      ids[u] = l;
      for (int i = ptr[u]; i < ptr[u + 1]; ++i) dfs1(zu[i]);
    }
  }
  void run() {
    const int m = as.size();
    ptr.resize(n + 2);
    zu.resize(m);
    for (int u = 0; u < n + 2; ++u) ptr[u] = 0;
    for (int i = 0; i < m; ++i) ++ptr[as[i] + 2];
    for (int u = 2; u < n + 1; ++u) ptr[u + 1] += ptr[u];
    for (int i = 0; i < m; ++i) zu[ptr[as[i] + 1]++] = bs[i];
    ids.assign(n, 0);
    us.clear();
    for (int u = 0; u < n; ++u) dfs0(u);
    for (int u = 0; u < n + 2; ++u) ptr[u] = 0;
    for (int i = 0; i < m; ++i) ++ptr[bs[i] + 2];
    for (int u = 2; u < n + 1; ++u) ptr[u + 1] += ptr[u];
    for (int i = 0; i < m; ++i) zu[ptr[bs[i] + 1]++] = as[i];
    l = 0;
    for (int j = n; --j >= 0; ) if (!~ids[us[j]]) { dfs1(us[j]); ++l; }
  }

  vector<vector<int>> group() const {
    assert(~l);
    vector<vector<int>> uss(l);
    for (int u = 0; u < n; ++u) uss[ids[u]].push_back(u);
    return uss;
  }
};

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


int N[3], M;
vector<int> U, V;

int main() {
  for (; ~scanf("%d%d%d%d", &N[0], &N[1], &N[2], &M); ) {
    U.resize(M);
    V.resize(M);
    for (int m = 0; m < M; ++m) {
      scanf("%d%d", &U[m], &V[m]);
    }
    
    auto id = [&](int h, int x) -> int {
      assert(0 <= x); assert(x <= N[h] + 1);
      if (x == 0) {
        return N[0] + N[1] + N[2] + 1;
      }
      if (x == N[h] + 1) {
        return N[0] + N[1] + N[2] + 2;
      }
      switch (h) {
        case 0: return x;
        case 1: return N[0] + x;
        case 2: return N[0] + N[1] + x;
        default: assert(false);
      }
    };
    // edge (u, v) means  u: t-side ==> v: t-side
    Scc scc(N[0] + N[1] + N[2] + 3);
    for (int h = 0; h < 3; ++h) {
      for (int x = 0; x <= N[h]; ++x) {
        scc.ae(id(h, x), id(h, x + 1));
      }
    }
    for (int m = 0; m < M; ++m) {
      scc.ae(U[m], V[m]);
      scc.ae(V[m], U[m]);
    }
    scc.run();
    vector<int> lss[3], rss[3];
    for (int h = 0; h < 3; ++h) {
      lss[h].assign(scc.l, N[h] + 2);
      rss[h].assign(scc.l, -1);
      for (int x = 0; x <= N[h] + 1; ++x) {
        const int a = scc[id(h, x)];
        chmin(lss[h][a], x);
        chmax(rss[h][a], x);
      }
    }
    vector<int> good[3];
    for (int h = 0; h < 3; ++h) {
      good[h].assign(N[h] + 1, 1);
    }
    vector<int> as;
    for (int a = 0; a < scc.l; ++a) {
      int hsLen = 0;
      int hs[3];
      for (int h = 0; h < 3; ++h) {
        if (lss[h][a] <= rss[h][a]) {
          hs[hsLen++] = h;
        }
      }
      if (hsLen == 1) {
        const int h = hs[0];
        for (int x = lss[h][a]; x < rss[h][a]; ++x) {
          good[h][x] = 0;
        }
      } else {
        as.push_back(a);
      }
    }
    vector<int> sumss[3];
    for (int h = 0; h < 3; ++h) {
      sumss[h].assign(N[h] + 2, 0);
      for (int x = 0; x < N[h] + 1; ++x) {
        sumss[h][x + 1] = sumss[h][x] + good[h][x];
      }
    }
    
    const int asLen = as.size();
    vector<int> head[3], tail[3];
    for (int h = 0; h < 3; ++h) {
      head[h].resize(asLen + 1);
      head[h][0] = 0;
      for (int i = 0; i < asLen; ++i) {
        const int a = as[i];
        head[h][i + 1] = (lss[h][a] <= rss[h][a]) ? rss[h][a] : head[h][i];
      }
      tail[h].resize(asLen + 1);
      tail[h][asLen] = N[h] + 1;
      for (int i = asLen; --i >= 0; ) {
        const int a = as[i];
        tail[h][i] = (lss[h][a] <= rss[h][a]) ? lss[h][a] : tail[h][i + 1];
      }
// cerr<<"head["<<h<<"] = "<<head[h]<<endl;
// cerr<<"tail["<<h<<"] = "<<tail[h]<<endl;
    }
    Int ans = 0;
    for (int i = 1; i < asLen; ++i) {
      // cut between as[i - 1] and as[i]
      Int prod = 1;
      for (int h = 0; h < 3; ++h) {
        prod *= (sumss[h][tail[h][i]] - sumss[h][head[h][i]]);
      }
      ans += prod;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
0