Submission #745049

#TimeUsernameProblemLanguageResultExecution timeMemory
745049b00norpDuathlon (APIO18_duathlon)C++14
23 / 100
1080 ms1048576 KiB
#include <bits/stdc++.h> using namespace std; #define int long long mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count()); const int INF = 1e18, N = 1e5 + 5; vector<int> g[N]; int siz; bool vis[N]; int subtree[N]; int ans = 0; int n, m; void dfsini(int node) { vis[node] = true; subtree[node] = 1; siz++; for(int to: g[node]) { if(vis[to]) continue; dfsini(to); subtree[node] += subtree[to]; } } void dfs_calc(int node, int par = -1) { ans += (siz - subtree[node]) * (subtree[node] - 1); for(int to: g[node]) { if(to == par) continue; ans += (subtree[to]) * (siz - subtree[to] - 1); } for(int to: g[node]) { if(to == par) continue; dfs_calc(to, node); } } void Solve() { 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(!vis[i]) { siz = 0; dfsini(i); dfs_calc(i); } } cout << ans << "\n"; } int32_t main() { auto begin = std::chrono::high_resolution_clock::now(); ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; for(int i = 1; i <= t; i++) { //cout << "Case #" << i << ": "; Solve(); } auto end = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin); //cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; return 0; } /* Sample 1: 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...