Submission #952413

#TimeUsernameProblemLanguageResultExecution timeMemory
952413ItamarDuathlon (APIO18_duathlon)C++14
31 / 100
1066 ms20816 KiB
#include <iostream> using namespace std; #include <vector> #define vi vector<int> #define ll long long #include <algorithm> #include <set> #include <string> #include <bitset> #include <cmath> #include <math.h> #define pll pair<ll,ll> #define vll vector<ll> #define pi pair<int,int> #include <map> #include <queue> #define pd pair<double,double> #include <stack> const int siz = 1e5 + 1; int c[siz]; vi fr[siz]; bool vis[siz]; int pad[siz]; vi frt[siz]; int padt[siz]; ll w[siz]; int sor[siz]; int cum[siz]; void dfs(int i, int&s, int so) { s++; sor[i] = so; if (vis[i])return; vis[i] = 1; for (int f : fr[i]) { if (!vis[f]) { pad[f] = i; dfs(f,s,so); } else if (f != pad[i] && c[i]==i) { int k = pad[i]; while (k != f) { w[i]++; c[k] = i; k = pad[k]; } w[i]++; c[k] = i; } } } ll ans = 0; int n, m; bool vist[siz]; int dfst(int i, int t) { vist[i] = 1; vll v; int sum = w[i]; for (int f : frt[i]) { if (!vist[f]) { v.push_back(dfst(f,t)); sum += v.back(); } } v.push_back(t - sum); ans += (w[i] * (w[i] - 1)) * (t - 2); for (ll k : v) { ans += w[i] * k * (t - 1 - k); ans -= 2 * k * (w[i] - 1); } return sum; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; fr[a].push_back(b); fr[b].push_back(a); } for (int i = 0; i < n; i++) { w[i] = 1; c[i] = i; } for (int i = 0; i < n; i++) { if (!vis[i]) { pad[i] = i; int s = 0; dfs(i,s,i); cum[i] = s; } } for (int i = 0; i < n; i++) { for (int f : fr[i]) { if (c[i] != c[f]) { frt[c[i]].push_back(c[f]); } } } for(int i = 0; i < n; i++)if(!vist[c[i]])dfst(c[i],cum[sor[i]]); cout << ans; } /* 4 3 1 2 2 3 3 4 */
#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...