QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#542542 | #7860. Graph of Maximum Degree 3 | Repeater | RE | 0ms | 3624kb | C++20 | 2.4kb | 2024-09-01 02:27:16 | 2024-09-01 02:27:17 |
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;
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
{
if(cnt >= 4) return;
ans += check();
assert(tmp.size() <= 4);
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3552kb
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: 3624kb
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: 3536kb
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: -100
Runtime Error
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 ...