This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#ifdef PERVEEVM_LOCAL
#define debug(x) std::cerr << (#x) << ":\t" << (x) << std::endl
#else
#define debug(x) 238;
#endif
#define fastIO std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0)
#define NAME "File"
using ll = long long;
using ld = long double;
#ifdef PERVEEVM_LOCAL
std::mt19937 rnd(238);
#else
std::mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
const double PI = atan2(0.0, -1.0);
const int INF = 0x3f3f3f3f;
const ll LINF = (ll)2e18;
const int N = 200100;
const int LG = 18;
std::vector<int> g[N];
bool used[N];
int cmpId[N];
int sz = 0, curCmp = 0, timer = 0;
int dp[LG][N];
int tin[N], tout[N];
int h[N];
void dfs(int v, int p, int d) {
used[v] = true;
++sz;
cmpId[v] = curCmp;
tin[v] = timer++;
h[v] = d;
dp[0][v] = p;
for (auto to : g[v]) {
if (!used[to]) {
dfs(to, v, d + 1);
}
}
tout[v] = timer++;
}
void precalcLCA() {
for (int i = 1; i < LG; ++i) {
for (int j = 0; j < N; ++j) {
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}
}
bool isParent(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int lca(int a, int b) {
if (isParent(a, b)) {
return a;
}
if (isParent(b, a)) {
return b;
}
for (int i = LG - 1; i >= 0; --i) {
int pa = dp[i][a];
if (!isParent(pa, b)) {
a = pa;
}
}
return dp[0][a];
}
int dist(int a, int b) {
return h[a] + h[b] - 2 * h[lca(a, b)];
}
void run() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
int from, to;
scanf("%d%d", &from, &to);
g[from - 1].push_back(to - 1);
g[to - 1].push_back(from - 1);
}
for (int i = 0; i < n; ++i) {
if (!used[i]) {
dfs(i, i, 0);
++curCmp;
}
}
precalcLCA();
ll ans = 0;
for (int from = 0; from < n; ++from) {
for (int to = 0; to < n; ++to) {
if (from == to || cmpId[from] != cmpId[to]) {
continue;
}
ans += dist(from, to) - 1;
}
}
printf("%lld\n", ans);
}
int main(void) {
// freopen(NAME".in", "r", stdin);
// freopen(NAME".out", "w", stdout);
auto start = std::chrono::high_resolution_clock::now();
run();
auto end = std::chrono::high_resolution_clock::now();
#ifdef PERVEEVM_LOCAL
std::cerr << "Execution time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
<< " ms" << std::endl;
#endif
return 0;
}
Compilation message (stderr)
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:132:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
auto start = std::chrono::high_resolution_clock::now();
^~~~~
count_triplets.cpp:134:10: warning: variable 'end' set but not used [-Wunused-but-set-variable]
auto end = std::chrono::high_resolution_clock::now();
^~~
count_triplets.cpp: In function 'void run()':
count_triplets.cpp:95:10: 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:99:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &from, &to);
~~~~~^~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |