Submission #289219

#TimeUsernameProblemLanguageResultExecution timeMemory
289219wiwihoDuathlon (APIO18_duathlon)C++14
0 / 100
184 ms39416 KiB
//#define NDEBUG #include <bits/stdc++.h> #include <bits/extc++.h> #define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define iter(a) a.begin(), a.end() #define riter(a) a.rbegin(), a.rend() #define lsort(a) sort(iter(a)) #define gsort(a) sort(riter(a)) #define pb(a) push_back(a) #define eb(a) emplace_back(a) #define pf(a) push_front(a) #define pob pop_back() #define pof pop_front() #define mp(a, b) make_pair(a, b) #define F first #define S second #define mt make_tuple #define gt(t, i) get<i>(t) #define iceil(a, b) (((a) + (b) - 1) / (b)) #define tomax(a, b) ((a) = max((a), (b))) #define printv(a, b) {bool pvaspace=false; \ for(auto pva : a){ \ if(pvaspace) b << " "; pvaspace=true;\ b << pva;\ }\ b << "\n";} //#define TEST using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef long double ld; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; using tiii = tuple<int, int, int>; const ll MOD = 1000000007; const ll MAX = 2147483647; template<typename A, typename B> ostream& operator<<(ostream& o, pair<A, B> p){ return o << '(' << p.F << ',' << p.S << ')'; } vector<vector<int>> tg, g; int bcc; vector<int> low, in; int tts = 1; stack<int> st; vector<vector<int>> c; vector<bool> iscut; void dfsbcc(int now, int p){ low[now] = in[now] = tts++; for(int i : tg[now]){ if(i == p) continue; if(in[i]) low[now] = min(low[now], in[i]); else{ dfsbcc(i, now); low[now] = min(low[now], low[i]); c[now].eb(i); } if(low[i] >= in[now] && now != 1) iscut[now] = true; } if(now == 1 && c[now].size() > 1) iscut[now] = true; } void dfsbcc2(int now, int p){ st.push(now); for(int i : c[now]){ dfsbcc2(i, now); } if(now == 1){ if(st.size() > 1){ while(!st.empty()){ g[st.top()].eb(bcc); g[bcc].eb(st.top()); st.pop(); } bcc++; } } else if((p != 1 && low[now] >= in[p]) || (p == 1 && c[p].size() > 1)){ while(!st.empty()){ int t = st.top(); g[st.top()].eb(bcc); g[bcc].eb(st.top()); st.pop(); if(t == now) break; } g[bcc].eb(p); g[p].eb(bcc); bcc++; } } vector<int> sz, cnt; ll ans = 0; int n; void dfs1(int now, int p){ sz[now] = now <= n; for(int i : g[now]){ if(i == p) continue; dfs1(i, now); sz[now] += sz[i]; } } void dfs2(int now, int p, int r){ ll s = 0; for(int i : g[now]){ if(i == p) continue; s += (ll) (sz[r] - sz[i]) * sz[i]; if(now <= n && g[i].size() == 2) s-=2/*, cerr << now << "--\n"*/; // cerr << now << " " << i << " " << sz[r] - sz[i] << " " << sz[i] << "\n"; dfs2(i, now, r); } s += (ll) (sz[r] - sz[now]) * sz[now]; if(now <= n) s += sz[r] - 1/*, cerr << now << " " << sz[r] - 1 << "\n"*/; if(now <= n && now != p && g[p].size() == 2) s-=2/*, cerr << now << "--\n"*/; if(now > n && g[now].size() == 2) s-=2/*, cerr << now << "--\n"*/; // cerr << now << " r " << sz[now] << " " << sz[r] - sz[now] << "\n"; // cerr << "test " << now << ' ' << s << "\n"; ans += (ll) cnt[now] * s; } int main(){ StarBurstStream int m; cin >> n >> m; tg.resize(n + 1); g.resize(3 * n + 1); bcc = n + 1; low.resize(n + 1); in.resize(n + 1); c.resize(n + 1); iscut.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(in[i]) continue; dfsbcc(i, i); dfsbcc2(i, i); } // for(int i = 1; i < bcc; i++){ // cerr << i << " "; // printv(g[i], cerr); // } sz.resize(3 * n + 1, -MAX); cnt.resize(3 * n + 1); for(int i = n + 1; i < bcc; i++){ cnt[i] = g[i].size(); } for(int i = 1; i <= n; i++) cnt[i] = -1; // printv(cnt, cerr); for(int i = 1; i < bcc; i++){ if(sz[i] != -MAX) continue; dfs1(i, i); dfs2(i, 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...