# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
393564 |
2021-04-24T03:18:39 Z |
KoD |
Duathlon (APIO18_duathlon) |
C++17 |
|
177 ms |
37964 KB |
#include <bits/stdc++.h>
template <class T>
using Vec = std::vector<T>;
using ll = long long;
template <class F>
struct RecLambda: private F {
explicit RecLambda(F&& f): F(std::forward<F>(f)) { }
template <class... Args>
decltype(auto) operator () (Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
struct DSU {
Vec<int> par;
DSU(const int n): par(n, -1) { }
int find(const int u) {
return par[u] < 0 ? u : par[u] = find(par[u]);
}
void merge(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
par[x] += par[y];
par[y] = x;
}
};
int main() {
int N, M;
std::cin >> N >> M;
Vec<Vec<std::pair<int, int>>> graph(N);
Vec<int> A(M), B(M);
for (int i = 0; i < M; ++i) {
std::cin >> A[i] >> B[i];
A[i] -= 1;
B[i] -= 1;
graph[A[i]].emplace_back(B[i], i);
graph[B[i]].emplace_back(A[i], i);
}
Vec<bool> bridge(M);
Vec<int> ord(N), low(N);
{
Vec<int> depth(N, -1);
int time = 0;
const auto dfs = RecLambda([&](auto&& dfs, const int u, const int p, const int d) -> void {
depth[u] = d;
ord[u] = time++;
low[u] = ord[u];
for (const auto [v, e]: graph[u]) {
if (depth[v] == -1) {
dfs(v, u, d + 1);
if (low[v] > ord[u]) {
bridge[e] = true;
}
low[u] = std::min(low[u], low[v]);
}
else if (v != p and depth[v] < depth[u]) {
low[u] = std::min(low[u], ord[v]);
}
}
});
for (int i = 0; i < N; ++i) {
if (depth[i] == -1) {
dfs(i, i, 0);
}
}
}
DSU dsu(N);
for (int i = 0; i < M; ++i) {
if (!bridge[i]) {
dsu.merge(A[i], B[i]);
}
}
Vec<Vec<int>> group(N);
for (int i = 0; i < N; ++i) {
group[dsu.find(i)].push_back(i);
}
group.erase(std::remove_if(group.begin(), group.end(), [&](const auto &v) { return v.empty(); }), group.end());
const auto V = (int) group.size();
Vec<int> belong(N);
Vec<int> size(V);
assert(V == N);
for (int i = 0; i < V; ++i) {
size[i] = (int) group[i].size();
for (const auto u: group[i]) {
belong[u] = i;
}
}
Vec<Vec<int>> tree(N);
for (int i = 0; i < M; ++i) {
if (bridge[i]) {
const auto a = belong[A[i]];
const auto b = belong[B[i]];
assert(a != b);
tree[a].push_back(b);
tree[b].push_back(a);
}
}
ll ans = 0;
for (int i = 0; i < V; ++i) {
const ll x = size[i];
ans += x * (x - 1) * (x - 2);
}
Vec<bool> visited(V);
Vec<int> subtree(V);
const auto setup = RecLambda([&](auto&& dfs, const int u) -> void {
visited[u] = true;
subtree[u] = size[u];
for (const auto v: tree[u]) {
if (!visited[v]) {
dfs(v);
subtree[u] += subtree[v];
}
}
});
Vec<int> root;
for (int i = 0; i < V; ++i) {
if (!visited[i]) {
root.push_back(i);
setup(i);
}
}
const auto apply = RecLambda([&](auto&& dfs, const int u, const int p) -> void {
const auto len = (int) tree[u].size();
Vec<int> around(len);
for (int i = 0; i < len; ++i) {
const auto v = tree[u][i];
if (v == p) {
around[i] = N - subtree[u];
}
else {
around[i] = subtree[v];
}
}
const ll sum = N - size[u];
ans += sum * sum * size[u];
for (const ll x: around) {
ans -= x * x * size[u];
}
ans += (ll) (size[u] - 1) * (size[u] - 1) * sum * 2;
for (const auto v: tree[u]) {
if (v != p) {
dfs(v, u);
}
}
});
for (const auto r: root) {
apply(r, r);
}
std::cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
131 ms |
37964 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
484 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
154 ms |
20548 KB |
Output is correct |
2 |
Correct |
154 ms |
20668 KB |
Output is correct |
3 |
Correct |
155 ms |
20540 KB |
Output is correct |
4 |
Correct |
150 ms |
20528 KB |
Output is correct |
5 |
Correct |
177 ms |
20528 KB |
Output is correct |
6 |
Correct |
167 ms |
26644 KB |
Output is correct |
7 |
Correct |
172 ms |
24788 KB |
Output is correct |
8 |
Correct |
170 ms |
23740 KB |
Output is correct |
9 |
Correct |
175 ms |
22588 KB |
Output is correct |
10 |
Incorrect |
165 ms |
20532 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Runtime error |
2 ms |
588 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
145 ms |
20608 KB |
Output is correct |
2 |
Correct |
159 ms |
20328 KB |
Output is correct |
3 |
Runtime error |
167 ms |
27452 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |