QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#289081 | #7860. Graph of Maximum Degree 3 | ucup-team1469# | RE | 1ms | 3528kb | C++17 | 8.0kb | 2023-12-23 15:13:53 | 2023-12-23 15:13:54 |
Judging History
answer
#ifndef LOCAL
#define FAST_IO
#endif
// ============
#include <bits/stdc++.h>
#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, m, n) for (i32 i = (i32)(n)-1; i >= (i32)(m); --i)
#define PER(...) OVERRIDE(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define LEN(x) (i32)(x.size())
using namespace std;
using u32 = unsigned int;
using u64 = unsigned long long;
using i32 = signed int;
using i64 = signed long long;
using f64 = double;
using f80 = long double;
using pi = pair<i32, i32>;
using pl = pair<i64, i64>;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = V<V<T>>;
template <typename T>
using VVV = V<V<V<T>>>;
template <typename T>
using VVVV = V<V<V<V<T>>>>;
template <typename T>
using PQR = priority_queue<T, V<T>, greater<T>>;
template <typename T>
bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <typename T>
i32 lob(const V<T> &arr, const T &v) {
return (i32)(lower_bound(ALL(arr), v) - arr.begin());
}
template <typename T>
i32 upb(const V<T> &arr, const T &v) {
return (i32)(upper_bound(ALL(arr), v) - arr.begin());
}
template <typename T>
V<i32> argsort(const V<T> &arr) {
V<i32> ret(arr.size());
iota(ALL(ret), 0);
sort(ALL(ret), [&](i32 i, i32 j) -> bool {
if (arr[i] == arr[j]) {
return i < j;
} else {
return arr[i] < arr[j];
}
});
return ret;
}
#ifdef INT128
using u128 = __uint128_t;
using i128 = __int128_t;
istream &operator>>(istream &is, i128 &x) {
i64 v;
is >> v;
x = v;
return is;
}
ostream &operator<<(ostream &os, i128 x) {
os << (i64)x;
return os;
}
istream &operator>>(istream &is, u128 &x) {
u64 v;
is >> v;
x = v;
return is;
}
ostream &operator<<(ostream &os, u128 x) {
os << (u64)x;
return os;
}
#endif
[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
SetUpIO() {
#ifdef FAST_IO
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cout << fixed << setprecision(15);
}
} set_up_io;
void scan(char &x) { cin >> x; }
void scan(u32 &x) { cin >> x; }
void scan(u64 &x) { cin >> x; }
void scan(i32 &x) { cin >> x; }
void scan(i64 &x) { cin >> x; }
void scan(string &x) { cin >> x; }
template <typename T>
void scan(V<T> &x) {
for (T &ele : x) {
scan(ele);
}
}
void read() {}
template <typename Head, typename... Tail>
void read(Head &head, Tail &...tail) {
scan(head);
read(tail...);
}
#define CHAR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define U32(...) u32 __VA_ARGS__; read(__VA_ARGS__);
#define U64(...) u64 __VA_ARGS__; read(__VA_ARGS__);
#define I32(...) i32 __VA_ARGS__; read(__VA_ARGS__);
#define I64(...) i64 __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define VEC(type, name, size) V<type> name(size); read(name);
#define VVEC(type, name, size1, size2) VV<type> name(size1, V<type>(size2)); read(name);
// ============
#ifdef DEBUGF
#else
#define DBG(x) (void) 0
#endif
// ============
#include <cassert>
#include <utility>
#include <vector>
class UnionFind {
std::vector<int> par;
int _root(int v) {
if (par[v] < 0) {
return v;
} else {
return par[v] = _root(par[v]);
}
}
public:
UnionFind(int n) : par(n, -1) {}
int root(int v) {
assert(v >= 0 && v < (int) par.size());
return _root(v);
}
int size(int v) {
assert(v >= 0 && v < (int) par.size());
return -par[_root(v)];
}
bool unite(int u, int v) {
assert(u >= 0 && u < (int) par.size() && v >= 0 && v < (int) par.size());
u = _root(u);
v = _root(v);
if (u == v) {
return false;
}
if (par[u] < par[v]) {
std::swap(u, v);
}
par[v] += par[u];
par[u] = v;
return true;
}
bool same(int u, int v) {
assert(u >= 0 && u < (int) par.size() && v >= 0 && v < (int) par.size());
return _root(u) == _root(v);
}
};
// ============
constexpr u32 MOD = 998244353;
void solve() {
I32(n, m);
VV<pi> g(n);
REP(i, m) {
I32(u, v, c);
--u;
--v;
g[u].emplace_back(v, c);
g[v].emplace_back(u, c);
}
u32 ans = 0;
{ // |S| = 1
ans += n;
}
{ // |S| = 2
REP(u, n) {
for (auto [v, c] : g[u]) {
if (c == 0 && u < v && find(ALL(g[u]), pi(v, c ^ 1)) != g[u].end()) {
++ans;
}
}
}
}
{ // |S| = 3
auto check = [&](i32 u, i32 v, i32 w) -> bool {
REP(col, 2) {
i32 cnt = 0;
if (find(ALL(g[u]), pi(v, col)) != g[u].end()) {
++cnt;
}
if (find(ALL(g[v]), pi(w, col)) != g[v].end()) {
++cnt;
}
if (find(ALL(g[w]), pi(u, col)) != g[w].end()) {
++cnt;
}
if (cnt <= 1) {
return false;
}
}
return true;
};
REP(v, n) {
V<i32> nei;
for (auto [u, c] : g[v]) {
if (c == 1 && find(ALL(g[v]), pi(u, 0)) != g[v].end()) {
continue;
}
if (u > v) nei.push_back(u);
}
REP(i, nei.size()) REP(j, i + 1, nei.size()) {
if (check(v, nei[i], nei[j])) ++ans;
}
}
}
{ // |S| = 4
auto check = [&](i32 u, i32 v, i32 w, i32 x) -> bool {
i32 vs[4] = {u, v, w, x};
REP(col, 2) {
UnionFind uf(4);
REP(i, 4) REP(j, i) {
if (find(ALL(g[vs[i]]), pi(vs[j], col)) != g[vs[i]].end()) {
uf.unite(i, j);
}
}
if (uf.size(0) != 4) {
return false;
}
}
return true;
};
REP(v, n) {
V<i32> nei;
for (auto [u, c] : g[v]) {
if (c == 1 && find(ALL(g[v]), pi(u, 0)) != g[v].end()) {
continue;
}
if (u > v) nei.push_back(u);
}
if (LEN(g[v]) == 3 && LEN(nei) == 2) {
i32 a = nei[0], b = nei[1];
if (find(ALL(g[v]), pi(a, 0)) != g[v].end() && find(ALL(g[v]), pi(a, 1)) != g[v].end()) {
swap(a, b);
}
if (find(ALL(g[v]), pi(b, 0)) != g[v].end() && find(ALL(g[v]), pi(b, 1)) != g[v].end()) {
// a-v=b
if (LEN(g[b]) == 3) {
i32 c = -1;
for (auto [azxfnvlmbax, axkvxvs] : g[c]) {
if (azxfnvlmbax != v) {
c = azxfnvlmbax;
}
}
assert(c != -1);
check(v, a, b, c);
}
}
}
if (LEN(nei) == 3) {
if (check(v, nei[0], nei[1], nei[2])) ++ans;
}
}
}
cout << ans << '\n';
}
int main() {
i32 t = 1;
//cin >> t;
while (t--) {
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3500kb
input:
3 4 1 2 0 1 3 1 2 3 0 2 3 1
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
4 6 1 2 0 2 3 0 3 4 0 1 4 1 2 4 1 1 3 1
output:
5
result:
ok 1 number(s): "5"
Test #3:
score: -100
Runtime Error
input:
20 28 9 6 1 9 6 0 3 8 0 8 4 0 3 8 1 3 4 1 2 13 0 13 1 0 19 1 0 2 1 1 2 19 1 13 19 1 14 15 1 14 15 0 7 12 0 12 17 0 20 17 0 7 17 1 7 20 1 12 20 1 16 18 0 18 10 0 5 10 0 16 10 1 16 5 1 18 5 1 4 6 0 9 11 0