Submission #1123290

#TimeUsernameProblemLanguageResultExecution timeMemory
1123290DenisovDuathlon (APIO18_duathlon)C++20
0 / 100
103 ms35272 KiB
//#pragma GCC optimize("Ofast", "unroll-loops") //#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4") #include <bits/stdc++.h> #define all(a) a.begin(),a.end() #define len(a) (int)(a.size()) #define mp make_pair #define pb push_back #define fir first #define sec second #define fi first #define se second using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef long double ld; template<typename T> bool umin(T &a, T b) { if (b < a) { a = b; return true; } return false; } template<typename T> bool umax(T &a, T b) { if (a < b) { a = b; return true; } return false; } #ifdef KIVI #define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false) #define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); } #else #define DEBUG while (false) #define LOG(...) #endif const int max_n = (1e5 + 11) * 2, inf = 1000111222; vector <int> edge[max_n]; int tin[max_n], tout[max_n], used[max_n], timer = 0, art[max_n], id[max_n]; vector <int> q; vector <vector <int> > cmp; inline void dfs (int v, int p = -1) { used[v] = 1; tin[v] = timer++; tout[v] = tin[v]; q.pb(v); for (int to : edge[v]) { if (to == p) { continue; } if (used[to]) { umin(tout[v], tin[to]); } else { dfs(to, v); umin(tout[v], tout[to]); if (tout[to] >= tin[v]) { art[v] = tin[v] > 0 || tin[to] > 1; cmp.pb(vector <int>{v}); while (cmp.back().back() != to) { cmp.back().pb(q.back()); q.pop_back(); } } } } } struct dsu { public: int n; vector <int> p, cnt; inline void make_set (int v) { p[v] = v; } dsu (int n) : n(n) { p.resize(n); cnt.assign(n, 1); for (int i = 0; i < n; i++) { make_set(i); } } inline int get (int v) { if (p[v] == v) return v; return p[v] = get(p[v]); /// compressing path } inline bool unite (int a, int b) { a = get(a); b = get(b); if (a == b) return false; if (cnt[a] > cnt[b]) { swap(a, b); } p[a] = b; cnt[b] += cnt[a]; return true; } }; vector <int> e[max_n]; int sz[max_n], n; ll res[max_n]; ll ans = 0; inline void calc (int v, int p = -1) { ans += sz[v] * (sz[v] - 1ll) * (sz[v] - 2ll); ans += sz[v] * (sz[v] - 1ll) * (n - sz[v]) * 2ll; ll cur = sz[v]; for (int to : e[v]) { if (to == p) { continue; } calc(to, v); ans += cur * sz[to] * (sz[v] - cur) * 2ll; sz[v] += sz[to]; } ans += cur * (sz[v] - cur) * (n - sz[v]) * 2ll; } int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); int m; cin >> n >> m; for (int i = 0, a, b; i < m; i++) { a = i + 1, b = i + 2; cin >> a >> b; --a, --b; edge[a].pb(b); edge[b].pb(a); } dfs(0); int new_id = 0; for (int i = 0; i < n; i++) { if (art[i]) { id[i] = new_id++; } } for (auto &i : cmp) { int now = new_id++, cnt = 0; for (int x : i) { if (art[x]) { e[id[x]].pb(now); e[now].pb(id[x]); LOG(id[x], now); } else { ++cnt; id[x] = now; } } for (int x : i) { if (art[x]) { ans += cnt * (cnt - 1ll); } } } LOG(new_id); for (int i = 0; i < n; i++) { ++sz[id[i]]; } calc(0); 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...