Submission #710568

#TimeUsernameProblemLanguageResultExecution timeMemory
710568lamDuathlon (APIO18_duathlon)C++14
100 / 100
143 ms36704 KiB
#include <bits/stdc++.h> #define int long long using namespace std; const int maxn = 3e5 + 10; int n,m,ans; vector <int> adj[maxn],adj2[maxn]; stack <int> st; int l[maxn],t[maxn],cnt,sl,bcc,s[maxn]; void dfs(int x, int p) { l[x] = t[x] = ++cnt; sl++; st.push(x); for (int i:adj[x]) if (i!=p) { if (t[i]==0) { dfs(i,x); l[x]=min(l[x],l[i]); if (l[i]>=t[x]) { bcc++; // cout<<bcc+n<<' '<<st.size()<<" := "<<x<<' '; adj2[x].push_back(bcc + n); int temp; do { temp = st.top(); st.pop(); // cout<<temp<<" , "; adj2[bcc+n].push_back(temp); } while (temp!=i); // cout<<endl; } } else l[x] = min(l[x],t[i]); } // cout<<x<<" :: "<<l[x]<<' '<<t[x]<<endl; } void dfs2(int x, int p) { s[x] = (x<=n); // cout<<x<<" !! "<<endl; for (int i:adj2[x]) if (i!=p) { dfs2(i,x); s[x]+=s[i]; if (x>n) { ans -= 1LL*((int)adj2[x].size()) * s[i] * (s[i]-1); } } // cout<<x<<' '<<sl<<" -> "<<s[x]<<' '<<(int)adj[x].size()<<endl; if (x>n) ans -= 1LL* (sl - s[x]) * (sl - s[x] - 1) * (int)adj2[x].size(); } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>n>>m; for (int i=1; i<=m; i++) { int u,v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } cnt = bcc = sl = 0; for (int i=1; i<=n; i++) { if (t[i]) continue; sl=0; dfs(i,i); ans += sl*(sl-1)*(sl-2); dfs2(i,i); } cout<<ans<<'\n'; }
#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...