Submission #103640

#TimeUsernameProblemLanguageResultExecution timeMemory
103640zubecDuathlon (APIO18_duathlon)C++14
23 / 100
1163 ms1049600 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int N = 100100; int n, m; vector <int> g[N]; ll sz[N]; ll ans = 0; bool used[N]; void dfs(int v, int p){ sz[v] = 1; used[v] = 1; for (int i = 0; i < g[v].size(); i++){ int to = g[v][i]; if (to == p) continue; dfs(to, v); sz[v] += sz[to]; } } void dfs2(int v, int p, int n){ ll cursum = n-sz[v]; for (int i = 0; i < g[v].size(); i++){ int to = g[v][i]; if (to == p) continue; dfs2(to, v, n); ans += cursum*sz[to]*2; cursum += sz[to]; } } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); //freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout); cin >> n >> m; for (int i = 1; i <= m; i++){ int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; i++){ if (!used[i]){ dfs(i, 0); dfs2(i, 0, sz[i]); } } cout << ans; } /** 4 3 1 2 2 3 3 4 5 4 1 2 3 4 4 5 4 1 */

Compilation message (stderr)

count_triplets.cpp: In function 'void dfs(int, int)':
count_triplets.cpp:21:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs2(int, int, int)':
count_triplets.cpp:31:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
#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...