結果

問題 No.2160 みたりのDominator
ユーザー 👑 hos.lyric
提出日時 2022-12-11 03:36:04
言語 C++14
(gcc 13.3.0 + boost 1.87.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
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 93
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0