QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#663862 | #7860. Graph of Maximum Degree 3 | NCl3 | WA | 38ms | 17928kb | C++20 | 3.8kb | 2024-10-21 17:58:18 | 2024-10-21 17:58:18 |
Judging History
answer
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using ull = unsigned long long;
constexpr int N = 1e5 + 10;
constexpr int inf = 1e9 + 10;
constexpr ll llinf = 1e18 + 10;
constexpr int mod = 998244353;
void debug() {
std::cerr << "\n";
}
template<typename T, typename... Args>
void debug(T val, const Args&... args) {
std::cerr << val << " ";
debug(args...);
}
template<typename T>
void debug(const std::vector<T>& vec) {
for (const T& x : vec) std::cerr << x << ' ';std::cerr << "\n";
}
struct DSU
{
std::vector<int> f, siz, d;
DSU() {}
DSU(int n)
{
build(n);
}
void build(int n)
{
f.resize(n + 1); d.resize(n + 1);
std::iota(f.begin(), f.end(), 0);
siz.assign(n + 1, 1);
}
int find(int x)
{
if (x != f[x]) {
int px = find(f[x]);
d[x] += d[f[x]];
f[x] = px;
}
return f[x];
}
bool same(int x, int y)
{
return find(x) == find(y);
}
bool merge(int x, int y, int op = 1)
{
int px = find(x), py = find(y);
if (px == py)
{
return false;
}
siz[py] += siz[px];
f[px] = py;
d[px] = d[y] - d[x] + op;//要确保d[px]>0
return true;
}
int size(int x)
{
return siz[find(x)];
}
int dist(int x)
{
return d[x];
}
};
void ncl3() {
int n, m;
std::cin >> n >> m;
std::vector<std::vector<int>> er(n), eb(n);
std::vector<std::array<int, 3>> edges(m);
std::vector<int> dr(n), db(n);
for (auto& [u, v, c] : edges) {
std::cin >> u >> v >> c;
u--, v--;
if (c == 0) {
er[u].push_back(v);
er[v].push_back(u);
dr[u]++, dr[v]++;
}
if (c == 1) {
eb[u].push_back(v);
eb[v].push_back(u);
db[u]++, db[v]++;
}
}
ll ans = n;
for (int u = 0; u < n; u++) {
int v = -1;
for (auto v1 : er[u]) {
for (auto v2 : eb[u]) {
if (v1 == v2) {
v = v1;
}
}
}
if (v != -1 && u < v) {
ans++;
for (auto v1 : er[u]) {
if (v1 == v) continue;
for (auto v2 : eb[v]) {
if (v2 == u) continue;
if (v1 == v2) {
ans++;
}
}
}
for (auto v1 : eb[u]) {
if (v1 == v) continue;
for (auto v2 : er[v]) {
if (v2 == u) continue;
if (v1 == v2) {
ans++;
}
}
}
}
}
DSU dsu(n);
for (auto [u, v, c] : edges) {
if (dr[u] + db[u] == 3 && dr[v] + db[v] == 3)
dsu.merge(u, v);
}
std::vector<int> st(n);
for (int i = 0; i < n; i++) {
if (dsu.size(i) == 4 && dr[i] && db[i]) {
st[dsu.find(i)]++;
}
}
for (int i = 0; i < n; i++) {
ans += (st[i] == 4);
}
std::cout << ans % mod << "\n";
}
/*
预处理、找性质缩小范围、集合思想、正难则反、穷举
*/
int main() {
#ifdef LOCAL
clock_t time = clock();
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t = 1;
// std::cin >> t;
while (t--) {
ncl3();
}
#ifdef LOCAL
std::cout << "Time Used:" << clock() - time << "ms" << "\n";
#endif
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
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: 3876kb
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: 0ms
memory: 3616kb
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: 0ms
memory: 3592kb
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: -100
Wrong Answer
time: 38ms
memory: 17928kb
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:
144846
result:
wrong answer 1st numbers differ - expected: '144604', found: '144846'