This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int M = 3e5+5, MOD = 1e9+7;
int d[M], noc[M], n, dp[M], vis[M], cnt = 1;
vector<int> node[M];
void dfs(int s, int p = 1) {
noc[s]++;
vis[s] = cnt;
for (int i:node[s]) {
if (!vis[i]) {
d[i] = d[s]+1;
dfs(i, s);
noc[s] += noc[i];
}
}
}
void dfs2(int s, int p = 1) {
vis[s] = cnt;
for (int i:node[s]) {
if (!vis[i]) {
dp[i] = dp[s] + n - 2*noc[i];
dfs2(i, s);
}
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
node[a].push_back(b);
node[b].push_back(a);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i, i);
for (int i = 1; i <= n; i++) dp[1] += d[i], vis[i] = (vis[i] == cnt?0:vis[i]);
dfs2(i, i);
for (int i = 1; i <= n; i++) ans += dp[i] - n + 1, ans %= MOD;
cnt++;
}
}
cout << ans << endl;
return 0;
}
/*
4 3
1 2 2 3 3 4
*/
# | 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... |