Submission #864592

#TimeUsernameProblemLanguageResultExecution timeMemory
864592vnm06Duathlon (APIO18_duathlon)C++14
23 / 100
40 ms19788 KiB
#include<bits/stdc++.h> #define endl '\n' using namespace std; long long n, m; vector<int> gr[300005]; long long ans=0, br=0; bool used[300005], used_br[300005]; long long dp_v[300005]; void find_br(int v) { used_br[v]=1; br++; int brs=gr[v].size(); for(int i=0; i<brs; i++) { if(used_br[gr[v][i]]) continue; find_br(gr[v][i]); } } void dfs(int v) { used[v]=1; dp_v[v]=1; int brs=gr[v].size(); long long sumv=0; for(int i=0; i<brs; i++) { if(used[gr[v][i]]) continue; dfs(gr[v][i]); ans+=dp_v[gr[v][i]]*sumv; sumv+=dp_v[gr[v][i]]; dp_v[v]+=dp_v[gr[v][i]]; } ans+=sumv*(br-sumv-1); } void solve_component(int v) { br=0; find_br(v); dfs(v); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=0; i<m; i++) { int v, u; cin>>v>>u; gr[v].push_back(u); gr[u].push_back(v); } for(int i=1; i<=n; i++) { if(used[i]) continue; solve_component(i); } cout<<2*ans<<endl; return 0; } /** 8 7 1 2 2 3 2 4 3 5 5 6 6 7 6 8 */
#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...