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;
const int maxn = 2e5+5;
vector<int> g[maxn];
int sz[maxn], dp[maxn];
vector<int> blg[maxn];
int low[maxn], num[maxn];
int n, m, bcsz, old_n;
long long ans = 0;
int tdfs;
stack<int> stk;
void addedge(int a, int b){
// cout << a << " " << b << endl;
blg[a].push_back(b);
blg[b].push_back(a);
}
void dfs(int x, int p){
n++;
stk.push(x);
low[x] = num[x] = ++tdfs;
for(auto v:g[x]){
if(v == p)continue;
if(num[v])low[x] = min(low[x], num[v]);
else {
dfs(v, x);
low[x] = min(low[x], low[v]);
}
}
if(p){
if(low[x] == num[p]){
bcsz++;
while(stk.top() != x){
addedge(stk.top(), bcsz);
sz[bcsz]++;
stk.pop();
}
addedge(x, bcsz);
sz[bcsz]++;
stk.pop();
addedge(p, bcsz);
}
else if(low[x] == num[x]){
stk.pop();
bcsz++;
sz[bcsz] = 1;
addedge(x, bcsz);
addedge(p, bcsz);
}
}
}
long long dfs2(int x, int p){
dp[x] = (x <= old_n);
long long re = 0;
for(auto v: blg[x]){
if(v == p)continue;
re += dfs2(v, x);
dp[x] += dp[v];
if(x <= old_n){
re += 1LL*dp[v]*(n - dp[v] - 1);
}
else {
re += 1LL*(blg[x].size()-2)*dp[v]*(n-dp[v]);
}
}
if(x <= old_n){
re += 1LL*(n - dp[x])*(dp[x] - 1);
}
else {
re += 1LL*(blg[x].size()-2)*(n - dp[x])*(dp[x]);
}
return re;
}
int main() {
cin >> n >> m;
while(m--){
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
bcsz = n;
old_n = n;
for(int i = 1; i <= old_n; i++){
if(!num[i]){
n = 0;
dfs(i, 0);
ans += dfs2(i, 0);
}
}
cout << ans;
}
# | 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... |