Submission #260931

#TimeUsernameProblemLanguageResultExecution timeMemory
260931wiwihoDuathlon (APIO18_duathlon)C++14
31 / 100
180 ms33656 KiB
#include <bits/stdc++.h> #define eb emplace_back #define mp make_pair #define F first #define S second #define pii pair<int, int> #define pll pair<ll, ll> #define printv(a, b) {bool pvs = false; \ for(auto i : a){ \ if(pvs) b << ' '; \ b << i; pvs = true; \ } \ b << '\n';} using namespace std; typedef long long ll; const ll MAX = 2147483647; vector<set<int>> g; vector<ll> sz, cnt; ll ans = 0; void dfs(int now, int p){ sz[now] = cnt[now]; for(int i : g[now]){ if(i == p) continue; dfs(i, now); sz[now] += sz[i]; } } ll r = 0; void dfs2(int now, int p){ vector<ll> tmp; ll sum = 0; for(int i : g[now]){ if(i == p) continue; dfs2(i, now); sum += sz[i]; tmp.eb(sz[i]); } sum += r - sz[now]; tmp.eb(r - sz[now]); //cerr << now << " " << cnt[now] << "\n"; //printv(tmp, cerr); for(ll i : tmp){ ans += (sum - i) * i * cnt[now]; if(cnt[now] >= 3) ans += i * (cnt[now] - 1) * (cnt[now] - 2) * 2; if(cnt[now] >= 2) ans += i * (cnt[now] - 1) * 2; } ans += cnt[now] * (cnt[now] - 1) * (cnt[now] - 2); } vector<vector<int>> tg; vector<int> bccid; vector<int> ts, low; int bcc = 1, tts = 0; stack<int> st; void dfst(int now, int p){ low[now] = ts[now] = tts++; st.push(now); for(int i : tg[now]){ if(i == p) continue; if(ts[i] != -1) low[now] = min(low[now], ts[i]); else{ dfst(i, now); low[now] = min(low[now], low[i]); } } if(low[now] > ts[p] || now == p){ int lst = -1; while(lst != now){ bccid[st.top()] = bcc; lst = st.top(); cnt[bcc]++; st.pop(); } bcc++; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; g.resize(n + 1); sz.resize(n + 1); tg.resize(n + 1); bccid.resize(n + 1); ts.resize(n + 1, -1); low.resize(n + 1); cnt.resize(n + 1); for(int i = 0; i < m; i++){ int u, v; cin >> u >> v; tg[u].eb(v); tg[v].eb(u); } for(int i = 1; i <= n; i++){ if(ts[i] == -1) dfst(i, i); } //printv(bccid, cerr); //printv(cnt, cerr); for(int i = 1; i <= n; i++){ for(int j : tg[i]){ if(bccid[i] != bccid[j]) g[bccid[i]].insert(bccid[j]); } } for(int i = 1; i <= n; i++){ if(sz[i]) continue; dfs(i, i); r = sz[i]; dfs2(i, i); } cout << ans << "\n"; 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...