Submission #974421

#TimeUsernameProblemLanguageResultExecution timeMemory
974421efedmrlrDuathlon (APIO18_duathlon)C++17
31 / 100
112 ms38228 KiB
#include <bits/stdc++.h> #define int long long int #define lli int #define ld long double #define pb push_back #define MP make_pair #define REP(i, n) for(int i = 0; (i) < (n); (i)++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() using namespace std; void fastio() { ios_base::sync_with_stdio(false); cin.tie(NULL); } const int N = 2e5 + 5; const int INF = 1e9 + 500; int n, m; vector<vector<array<int, 2> > > adj(N, vector<array<int, 2> >()); int tim = 0; vector<int> tin(N, -1), low(N, 0); vector<array<int, 2> > edg(N); vector<bool> isBr(N, 0); vector<int> comp(N, -1), cmpsz(N, 0); vector<vector<int> > cadj(N, vector<int>()); vector<int> subsz(N, 0); vector<int> rt; vector<int> vis(N, 0); lli ans = 0; void find_bridge(int node, int par) { tin[node] = low[node] = tim++; for(auto c : adj[node]) { if(c[0] == par) { continue; } if(tin[c[0]] != -1) { low[node] = min(low[node], tin[c[0]]); } else { find_bridge(c[0], node); low[node] = min(low[node], low[c[0]]); // cout << "c[1]:" << c[1] << " node:" << node << " lowc0:" << low[c[0]] << " tin[node]:" << tin[node] << "\n"; if(low[c[0]] > tin[node]) { //bridge isBr[c[1]] = 1; } } } } void get_comp(int node, int cm) { comp[node] = cm; cmpsz[cm]++; for(auto c : adj[node]) { if(comp[c[0]] != -1 || isBr[c[1]]) continue; get_comp(c[0], cm); } } void prec(int node, int par) { subsz[node] = cmpsz[node]; for(auto c : cadj[node]) { if(c == par) continue; prec(c, node); subsz[node] += subsz[c]; } } void dfs(int node, int par, int root) { // cout << "node:" << node << "\n"; lli ret = 0; lli x = subsz[root] - cmpsz[node]; for(auto c : cadj[node]) { if(c == par) { int sbt = subsz[root] - subsz[node]; ret += 1ll * sbt * (x - sbt); continue; } int sbt = subsz[c]; ret += 1ll * sbt * (x - sbt); dfs(c, node, root); } // cout << "node: " << node << " ret:" << ret << "\n"; ans += ret; } void dfs2(int node, int par, int root) { // cout << "node:" << node << "\n"; int conn = 0; vis[node] = 1; for(auto c : adj[node]) { if(!isBr[c[1]]) { if(vis[c[0]]) continue; dfs2(c[0], node, root); continue; } if(c[0] == par) { conn += subsz[root] - subsz[comp[node]]; continue; } conn += subsz[comp[c[0]]]; dfs2(c[0], node, root); } if(cmpsz[comp[node]] < 2) return; // cout << "node:" << node << "conn: " << conn << "\n"; // cout << "add : " << 2ll * (cmpsz[comp[node]] - 1) * (subsz[root] - conn - cmpsz[comp[node]]) << "\n"; ans += 2ll * (cmpsz[comp[node]] - 1) * (subsz[root] - conn - cmpsz[comp[node]]); } void solve() { cin >> n >> m; REP(i, m) { int u, v; cin >> u >> v; edg[i] = {u, v}; adj[u].pb({v, i}); adj[v].pb({u, i}); } for(int i = 1; i <= n; i++) { if(tin[i] != -1) continue; rt.pb(i); find_bridge(i, 0); } int cur = 1; for(int i = 1; i <= n; i++) { if(comp[i] == -1) { get_comp(i, cur++); } // cout << "i:" << i << " comp:" << comp[i] << "\n"; } for(int i = 0; i < m; i++) { if(isBr[i]) { // cout << edg[i][0] << " " << edg[i][1] << endl; cadj[comp[edg[i][0]]].pb(comp[edg[i][1]]); cadj[comp[edg[i][1]]].pb(comp[edg[i][0]]); } } for(auto c : rt) { prec(comp[c], 0); } for(int i = 1; i < cur; i++) { ans += 1ll * cmpsz[i] * (cmpsz[i] - 1) * (cmpsz[i] - 2); } // cout << "ans: " << ans << "\n"; for(int c : rt) { dfs(comp[c], 0, comp[c]); } // cout << "ans:" << ans << "\n"; for(int c : rt) { dfs2(c, 0, comp[c]); } cout << ans << "\n"; } signed main() { fastio(); solve(); }
#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...