QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#289090 | #7860. Graph of Maximum Degree 3 | ucup-team1469# | WA | 50ms | 10624kb | C++17 | 8.0kb | 2023-12-23 15:17:52 | 2023-12-23 15:17:52 |
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[b]) {
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();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3560kb
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: 1ms
memory: 3448kb
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: 0
Accepted
time: 1ms
memory: 3504kb
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
output:
27
result:
ok 1 number(s): "27"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3464kb
input:
100 150 93 23 0 23 81 0 76 81 0 93 81 1 93 76 1 23 76 1 100 65 0 65 56 0 19 56 0 100 56 1 100 19 1 65 19 1 2 98 0 2 98 1 26 63 0 63 90 0 26 63 1 26 90 1 6 11 0 11 67 0 6 11 1 6 67 1 37 89 0 89 64 0 25 64 0 37 64 1 37 25 1 89 25 1 84 10 0 10 29 0 75 29 0 84 29 1 84 75 1 10 75 1 7 70 1 7 70 0 28 92 0 ...
output:
141
result:
ok 1 number(s): "141"
Test #5:
score: 0
Accepted
time: 44ms
memory: 9628kb
input:
100000 133680 36843 86625 0 86625 63051 0 35524 63051 0 36843 63051 1 36843 35524 1 86625 35524 1 55797 82715 0 55797 82715 1 70147 35104 0 35104 91732 0 70147 35104 1 70147 91732 1 94917 70395 0 70395 68250 0 24100 68250 0 94917 68250 1 94917 24100 1 70395 24100 1 83033 18450 1 83033 18450 0 34462 ...
output:
144604
result:
ok 1 number(s): "144604"
Test #6:
score: 0
Accepted
time: 45ms
memory: 9676kb
input:
100000 133388 86620 74346 0 74346 19047 0 54911 19047 0 86620 19047 1 86620 54911 1 74346 54911 1 23715 93094 0 93094 91208 0 63189 91208 0 23715 91208 1 23715 63189 1 93094 63189 1 99337 41426 1 99337 41426 0 83742 45546 0 45546 73862 0 83742 45546 1 83742 73862 1 85256 2812 0 2812 59368 0 85918 59...
output:
144348
result:
ok 1 number(s): "144348"
Test #7:
score: 0
Accepted
time: 48ms
memory: 10424kb
input:
100000 150000 86541 24385 0 24385 75745 0 52353 75745 0 86541 75745 1 86541 52353 1 24385 52353 1 89075 78015 0 89075 78015 1 52519 74846 0 74846 12045 0 73265 12045 0 52519 12045 1 52519 73265 1 74846 73265 1 17884 63159 0 63159 47308 0 56073 47308 0 17884 47308 1 17884 56073 1 63159 56073 1 72134 ...
output:
144639
result:
ok 1 number(s): "144639"
Test #8:
score: 0
Accepted
time: 50ms
memory: 10468kb
input:
100000 150000 91951 68612 1 91951 68612 0 18361 92673 0 92673 52678 0 86520 52678 0 18361 52678 1 18361 86520 1 92673 86520 1 58779 2421 0 58779 2421 1 66622 6461 0 6461 96943 0 66622 6461 1 66622 96943 1 27201 480 1 27201 480 0 19082 3895 0 3895 17796 0 3117 17796 0 19082 17796 1 19082 3117 1 3895 ...
output:
144471
result:
ok 1 number(s): "144471"
Test #9:
score: 0
Accepted
time: 42ms
memory: 10624kb
input:
100000 150000 43756 3552 0 3552 90269 0 43756 3552 1 43756 90269 1 11104 36935 1 11104 36935 0 11648 5480 0 5480 45320 0 11648 5480 1 11648 45320 1 19216 85746 0 19216 85746 1 68825 11173 0 11173 43155 0 68825 11173 1 68825 43155 1 27349 75259 0 27349 75259 1 1704 24478 0 24478 5980 0 1704 24478 1 1...
output:
144217
result:
ok 1 number(s): "144217"
Test #10:
score: 0
Accepted
time: 46ms
memory: 10500kb
input:
99999 149998 51151 43399 0 51151 43399 1 45978 28343 0 28343 9008 0 85724 9008 0 45978 9008 1 45978 85724 1 28343 85724 1 79446 12915 0 12915 65925 0 28869 65925 0 79446 65925 1 79446 28869 1 12915 28869 1 82642 95556 0 95556 68817 0 68334 68817 0 82642 68817 1 82642 68334 1 95556 68334 1 61212 7638...
output:
144219
result:
ok 1 number(s): "144219"
Test #11:
score: 0
Accepted
time: 49ms
memory: 10448kb
input:
100000 149999 26736 28785 0 28785 37945 0 26736 28785 1 26736 37945 1 1240 74368 0 74368 45022 0 1240 74368 1 1240 45022 1 40673 1276 0 1276 56395 0 40673 1276 1 40673 56395 1 35181 63341 0 63341 35131 0 60120 35131 0 35181 35131 1 35181 60120 1 63341 60120 1 99363 36973 0 99363 36973 1 85717 77683 ...
output:
144380
result:
ok 1 number(s): "144380"
Test #12:
score: 0
Accepted
time: 50ms
memory: 10432kb
input:
100000 150000 63695 11044 0 11044 34978 0 56531 34978 0 63695 34978 1 63695 56531 1 11044 56531 1 72139 3715 0 3715 21024 0 96696 21024 0 72139 21024 1 72139 96696 1 3715 96696 1 54670 49014 0 54670 49014 1 7670 61055 0 61055 38409 0 7670 61055 1 7670 38409 1 83399 50676 0 50676 98893 0 60069 98893 ...
output:
144559
result:
ok 1 number(s): "144559"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3452kb
input:
1 0
output:
1
result:
ok 1 number(s): "1"
Test #14:
score: 0
Accepted
time: 2ms
memory: 5480kb
input:
100000 0
output:
100000
result:
ok 1 number(s): "100000"
Test #15:
score: 0
Accepted
time: 49ms
memory: 10100kb
input:
100000 150000 95066 31960 0 31960 89758 0 10935 89758 0 95066 89758 1 95066 10935 1 31960 10935 1 48016 97823 0 97823 10871 0 23454 10871 0 48016 10871 1 48016 23454 1 97823 23454 1 73749 35525 0 35525 54232 0 42182 54232 0 73749 54232 1 73749 42182 1 35525 42182 1 75405 71341 0 71341 70032 0 3284 7...
output:
125000
result:
ok 1 number(s): "125000"
Test #16:
score: -100
Wrong Answer
time: 0ms
memory: 3448kb
input:
4 6 1 2 0 1 2 1 1 3 0 2 4 1 3 4 0 3 4 1
output:
6
result:
wrong answer 1st numbers differ - expected: '7', found: '6'