QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#542540 | #7860. Graph of Maximum Degree 3 | Repeater | RE | 0ms | 3568kb | C++20 | 2.5kb | 2024-09-01 02:24:12 | 2024-09-01 02:24:12 |
Judging History
answer
#include <bits/stdc++.h>
struct DSU
{
std::vector<int> f, siz;
DSU() {}
DSU(int n)
{
init(n);
}
void init(int n)
{
f.resize(n);
std::iota(f.begin(), f.end(), 0);
siz.assign(n, 1);
}
int find(int x)
{
while (x != f[x])
{
x = f[x] = f[f[x]];
}
return x;
}
bool same(int x, int y)
{
return find(x) == find(y);
}
bool merge(int x, int y)
{
x = find(x);
y = find(y);
if (x == y) {
return false;
}
siz[x] += siz[y];
f[y] = x;
return true;
}
int size(int x)
{
return siz[find(x)];
}
};
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m; std::cin >> n >> m;
DSU dsu(n);
std::vector<std::vector<int>> adj(n);
std::vector<std::set<int>> vis(n);
for(int i = 0; i < m; i++)
{
int u, v, w; std::cin >> u >> v >> w;
u--, v--;
if(w == 0)
{
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
else
{
if(u > v) std::swap(u, v);
vis[u].emplace(v);
}
}
std::vector<int> fa(n, -1);
auto init = [&](auto init, int x, int lst) -> void
{
for(auto y : adj[x])
{
if(y == lst) continue;
fa[y] = x;
init(init, y, x);
}
};
for(int i = 0; i < n; i++)
{
if(fa[i] != -1) continue;
init(init, i, -1);
}
std::vector<int> tmp;
auto check = [&]() -> int
{
int ok = 1;
assert(tmp.size());
std::ranges::sort(tmp);
for(int i = 0; i < tmp.size(); i++)
for(int j = i + 1; j < tmp.size(); j++)
if(vis[tmp[i]].contains(tmp[j]))
dsu.merge(tmp[i], tmp[j]);
for(int i = 1; i < tmp.size(); i++)
if(!dsu.same(tmp[0], tmp[i])) ok = 0;
for(auto i : tmp) dsu.f[i] = i;
// std::cerr << ok << " : ";
// for(auto i : tmp) std::cerr << i + 1 << " ";
// std::cerr << "\n";
return ok;
};
int ans = 0;
auto cacl = [&](auto cacl, int x, int cnt = 0) -> void
{
assert(tmp.size() <= 4);
if(cnt >= 4) return;
ans += check();
for(auto y : adj[x])
{
if(y == fa[x]) continue;
tmp.emplace_back(y);
cacl(cacl, y, cnt + 1);
if(tmp.size() == 4) tmp.pop_back();
}
};
for(int i = 0; i < n; i++)
{
tmp.clear();
tmp.emplace_back(i);
cacl(cacl, i);
}
std::cout << ans << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3516kb
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: 3568kb
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