Submission #745037

#TimeUsernameProblemLanguageResultExecution timeMemory
745037b00norpDuathlon (APIO18_duathlon)C++14
0 / 100
1087 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; 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) { // considering this is c // s is outside the subtree of node ans += (siz - subtree[node]) * (subtree[node] - 1); // f is outside the subtree of node ans += (subtree[node] - 1) * (siz - subtree[node]); for(int to: g[node]) { if(to == par) continue; dfs_calc(to, node); // s is in subtree of this node ans += (subtree[to]) * (subtree[node] - subtree[to] - 1); // f is in subtree of this node ans += (subtree[node] - subtree[to] - 1) * (subtree[to]); } } void Solve() { int n, m; 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; }
#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...