Submission #49632

#TimeUsernameProblemLanguageResultExecution timeMemory
49632SpaimaCarpatilorDuathlon (APIO18_duathlon)C++17
0 / 100
1119 ms1048576 KiB
#include<bits/stdc++.h> using namespace std; int nodes, N, M, lev[100009], dp[100009], vol[200009], papa[200009]; long long sumProd[200009]; stack < pair < int, int > > stk; vector < int > v[100009], h[200009]; void addComp (pair < int, int > whenToStop) { pair < int, int > curr; vector < int > currComp; do { curr = stk.top (); stk.pop (); currComp.push_back (curr.first); currComp.push_back (curr.second); }while (curr != whenToStop); sort (currComp.begin (), currComp.end ()); currComp.erase (unique (currComp.begin (), currComp.end ()), currComp.end ()); /* for (auto it : currComp) printf ("%d ", it); printf ("\n");*/ nodes ++; for (auto it : currComp) h[it].push_back (nodes), h[nodes].push_back (it); } void dfs (int nod, int tata) { dp[nod] = lev[nod]; for (auto it : v[nod]) if (lev[it] == 0) { lev[it] = lev[nod] + 1; stk.push ({it, nod}); dfs (it, nod); if (dp[it] < dp[nod]) dp[nod] = dp[it]; if (dp[it] >= lev[nod]) addComp ({it, nod}); } for (auto it : v[nod]) if (it != tata) { if (lev[it] < dp[nod]) dp[nod] = lev[it]; } } void init (int nod, int tata) { vol[nod] = (nod <= N), papa[nod] = tata; int sum = 0; for (auto it : h[nod]) if (it != tata) init (it, nod), vol[nod] += vol[it], sumProd[nod] += 1LL * sum * vol[it], sum += vol[it]; sumProd[nod] += 1LL * sum * (N - vol[nod]); sumProd[nod] <<= 1LL; } namespace treeSubtask { int vol[100009]; void dfs (int nod, int tata) { vol[nod] = 1; for (auto it : v[nod]) if (it != tata) dfs (it, nod), vol[nod] += vol[it]; } long long solveTree () { dfs (1, -1); long long ans = 0; for (int x=1; x<=N; x++) { int sum = 0; for (auto it : v[x]) { int currSize = 0; if (vol[it] > vol[x]) currSize = N - vol[x]; else currSize = vol[it]; ans += 1LL * sum * currSize; sum += currSize; } } return ans * 2LL; } }; int main () { //freopen ("input", "r", stdin); //freopen ("output", "w", stdout); scanf ("%d %d", &N, &M); while (M --) { int x, y; scanf ("%d %d", &x, &y); v[x].push_back (y); v[y].push_back (x); } printf ("%lld\n", treeSubtask::solveTree ()); return 0; lev[1] = 1, nodes = N; dfs (1, -1), init (1, -1); long long ans = 0; for (int x=1; x<=N; x++) { long long before = ans; ans += sumProd[x]; for (auto comp : h[x]) { ans += sumProd[comp]; if (papa[comp] == x) ans -= 2LL * (N - vol[comp]) * vol[comp]; else ans -= 2LL * (N - vol[x]) * vol[x]; } } printf ("%lld\n", ans); return 0; }

Compilation message (stderr)

count_triplets.cpp: In function 'int main()':
count_triplets.cpp:115:15: warning: unused variable 'before' [-Wunused-variable]
     long long before = ans;
               ^~~~~~
count_triplets.cpp:100:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d %d", &N, &M);
 ~~~~~~^~~~~~~~~~~~~~~~~
count_triplets.cpp:104:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d", &x, &y);
     ~~~~~~^~~~~~~~~~~~~~~~~
#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...