Submission #432666

#TimeUsernameProblemLanguageResultExecution timeMemory
432666SuhaibSawalha1Duathlon (APIO18_duathlon)C++17
13 / 100
327 ms12068 KiB
#include <bits/stdc++.h> using namespace std; int n, m, nodes, vid, found; vector<vector<int>> adj; vector<int> vis; long long ans; void dfs (int u, int t, int k, bool ok = 0) { if (found) { return; } vis[u] = vid; if (u == k) { found = ok; ans += ok; } ok |= u == t; for (int v : adj[u]) { if (vis[v] != vid) { dfs(v, t, k, ok); } } vis[u] = 0; } bool cycle; void dfs2 (int u, int p = -1) { vis[u] = 1; ++nodes; for (int v : adj[u]) { if (!vis[v]) { dfs2(v, u); } else if (v ^ p) { cycle = 1; } } } int dfs3 (int u, int d = 0) { vis[u] = 1; int s = 0; for (int v : adj[u]) { if (!vis[v]) { int f = dfs3(v, d); s += f; ans -= f * 1LL * f; } } ans += s * 2LL * (n - s - 1); ans += s * 1LL * s; return s + 1; } int main (){ ios_base::sync_with_stdio(false); cin.tie(NULL); // freopen("SuhaibSawalha1","r",stdin); cin >> n >> m; adj.resize(n); while (m--) { int u, v; cin >> u >> v, --u, --v; adj[u].push_back(v); adj[v].push_back(u); } vis.resize(n); if (n <= 10) { for (int u = 0; u < n; ++u) { for (int v = 0; v < n; ++v) { for (int k = 0; k < n; ++k) { if (set<int>{u, v, k}.size() == 3) { ++vid; found = 0; dfs(u, v, k); } } } } cout << ans; return 0; } if (all_of(adj.begin(), adj.end(), [](auto &v) {return v.size() <= 2;})) { for (int u = 0; u < n; ++u) { if (!vis[u]) { nodes = cycle = 0; dfs2(u); if (cycle) { ans += nodes * 1LL * (nodes - 1) * (nodes - 2); } else { ans += nodes * 1LL * (nodes - 1) * (nodes - 2) / 3; } } } cout << ans; return 0; } for (int u = 0; u < n; ++u) { if (!vis[u]) { dfs3(u); } } cout << ans; return 0; }
#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...