Submission #565856

#TimeUsernameProblemLanguageResultExecution timeMemory
565856Jarif_RahmanDuathlon (APIO18_duathlon)C++17
0 / 100
1073 ms23756 KiB
#include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; int n, m; vector<vector<int>> v, tree, backedge; vector<bool> bl; vector<bool> marked; vector<int> sz; ll ans = 0; void dfs_tree(int nd){ bl[nd] = 1; for(int x: v[nd]){ if(!bl[x]){ bl[x] = 1; tree[nd].pb(x); dfs_tree(x); } else backedge[nd].pb(x); } } void mark(int nd){ marked[nd] = !marked[nd]; for(int x: tree[nd]) mark(x); } void dfs_sz(int nd){ sz[nd] = 1; for(int x: tree[nd]) dfs_sz(x); for(int x: tree[nd]) sz[nd]+=sz[x]; } int SIZE = 0; int dfs(int nd){ int s = 0; for(int x: tree[nd]) s+=dfs(x); ans+=s; bool bkg = 0; for(int x: backedge[nd]) if(!marked[x]) bkg = 1; if(bkg) ans+=SIZE-sz[nd], s++; return s; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; v.assign(n, {}); for(int i = 0; i < n-1; i++){ int a, b; cin >> a >> b; a--, b--; v[a].pb(b); v[b].pb(a); } marked.assign(n, 0); sz.resize(n); for(int i = 0; i < n; i++){ tree.assign(n, {}); backedge.assign(n, {}); bl.assign(n, 0); dfs_tree(i); dfs_sz(i); for(int x: tree[i]){ ans+=ll(sz[i]-1-sz[x])*ll(sz[x]); SIZE=sz[x]; mark(x); dfs(x); mark(x); } } 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...