Submission #727652

#TimeUsernameProblemLanguageResultExecution timeMemory
727652SanguineChameleonDuathlon (APIO18_duathlon)C++17
70 / 100
85 ms15752 KiB
#include <bits/stdc++.h> using namespace std; void just_do_it(); int main() { #ifdef KAMIRULEZ freopen("kamirulez.inp", "r", stdin); freopen("kamirulez.out", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); just_do_it(); return 0; } const int maxn = 1e5 + 20; vector<int> adj[maxn]; vector<int> comp; bool can[55][55][55]; bool bad[maxn]; bool flag[maxn]; int par[maxn]; int depth[maxn]; int high[maxn]; int sz[maxn]; int node_cnt = 0; int edge_cnt = 0; int n, m; void dfs1(int x, int a, int b) { can[x][a][b] = true; for (auto c: adj[b]) { if (c != x && !can[x][a][c]) { dfs1(x, a, c); } } } void dfs2(int u, int p, bool sub7 = false) { par[u] = p; high[u] = depth[u]; comp.push_back(u); flag[u] = true; node_cnt++; edge_cnt += adj[u].size(); sz[u] = 1; for (auto v: adj[u]) { if (v == p) { continue; } if (!flag[v]) { depth[v] = depth[u] + 1; dfs2(v, u, sub7); sz[u] += sz[v]; high[u] = min(high[u], high[v]); } else { high[u] = min(high[u], depth[v]); } } } void sub1() { for (int x = 1; x <= n; x++) { for (int a = 1; a <= n; a++) { if (a != x) { dfs1(x, a, a); } } bad[x] = false; } int res = 0; for (int a = 1; a <= n; a++) { for (int b = 1; b <= n; b++) { if (b == a) { continue; } for (int c = 1; c <= n; c++) { if (c == a || c == b) { continue; } bool ok = true; for (int x = 1; x <= n; x++) { if (x == b) { continue; } if (!can[x][a][b] && !can[x][b][c]) { ok = false; break; } } if (ok) { res++; } } } } cout << res; } long long solve(int root) { comp.clear(); node_cnt = 0; edge_cnt = 0; dfs2(root, -1, true); edge_cnt /= 2; if (node_cnt == edge_cnt) { return 1LL * node_cnt * (node_cnt - 1) * (node_cnt - 2); } long long res = 0; for (auto u: comp) { res += 1LL * sz[u] * (node_cnt - sz[u]) * 2; } res -= 1LL * node_cnt * (node_cnt - 1); return res; } long long dfs3(int u) { int cnt = 1; for (auto v: adj[u]) { if (par[v] == u && high[v] >= depth[u]) { cnt += sz[v]; } } long long res = 1LL * cnt * (cnt - 1); for (auto v: adj[u]) { if (par[v] == u && high[v] < depth[u]) { res += dfs3(v); } } return res; } void sub8() { long long res = 0; for (int u = 1; u <= n; u++) { node_cnt = 0; for (int i = 1; i <= n; i++) { flag[i] = false; } depth[u] = 0; dfs2(u, -1); res += 1LL * (node_cnt - 1) * (node_cnt - 2); for (auto v: adj[u]) { if (par[v] == u) { res -= dfs3(v); } } } cout << res; } void just_do_it() { cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } if (n <= 50) { sub1(); return; } if (n <= 1000) { sub8(); return; } long long res = 0; for (int i = 1; i <= n; i++) { if (!flag[i]) { res += solve(i); } } cout << res; return; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...