#include<bits/stdc++.h>
using namespace std;
#define N 1<<18
int dep[N],id[N],low[N],CC,CC2,in[N],par[N],nod,sz[N];
long long ans;
struct unionfind{
int par[N];
int abp(int n){
return(par[n]==n?n:par[n]=abp(par[n]));
}
void init(){
for(int i=0;i<N;i++)
par[i]=i;
}
void merge(int a,int b){
a=abp(a),b=abp(b);
if(a-b)par[a]=b;
}
} CYCLE;
set<pair<int,int>>st[100100];
vector<int>adj1[N],adj2[N];
void tarjan(int n,int p){
low[n]=id[n]=++CC2;
for(auto i:adj1[n]){
if(i==p)continue;
if(!id[i]){
dep[i]=dep[n]+1;
tarjan(i,n);
low[n]=min(low[n],low[i]);
if(low[i]>id[n])
adj2[n].push_back(i);
} else if(dep[i]<dep[n])
st[n].insert({dep[i],++CC}),
low[n]=min(low[n],low[i]);
}
}
void dfscyc(int n){
for(auto i:adj1[n]){
if(dep[i]-dep[n]-1)
continue;
dfscyc(i);
if(st[i].size()&&st[i].begin()->first<dep[n]){
if(st[n].size()) CYCLE.merge(st[i].begin()
->second,st[n].begin()->second);
st[n].insert(*st[i].begin());
}
}
if(st[n].size())
in[n]=st[n].begin()->second;
}
void dfs2(int n,int p){
for(auto i:adj1[n])
if(dep[i]==dep[n]+1)
dfs2(i,n);
adj2[CYCLE.abp(in[n])].push_back(n);
par[CYCLE.abp(in[n])]=p;
}
void dfs3(int n){
long long sum=0;
for(auto i:adj2[n]){
dfs3(i);
sum+=sz[n]*sz[i];
sz[n]+=sz[i];
}
int x=sz[n];
sz[n]+=n<=nod;
sum+=(nod-sz[n])*x;
ans+=(n>nod?adj2[n].size()-1:1)*sum*2;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
CYCLE.init();
int n,m;
cin>>n>>m;
CC=nod=n;
while(m--){
int a,b;
cin>>a>>b;
adj1[a].push_back(b);
adj1[b].push_back(a);
}
for(int i=1;i<=n;i++)
if(!id[i]){
int prv=CC;
tarjan(i,0),
dfscyc(i),dfs2(i,0);
for(int j=prv;j++<CC;)
if(CYCLE.par[j]==j)
adj2[par[j]].push_back(j);
dfs3(i);
}
cout<<ans<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
23132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
23132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
47232 KB |
Output is correct |
2 |
Correct |
69 ms |
47320 KB |
Output is correct |
3 |
Incorrect |
55 ms |
39376 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23132 KB |
Output is correct |
3 |
Correct |
5 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23388 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
5 ms |
23132 KB |
Output is correct |
7 |
Correct |
4 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23132 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Incorrect |
4 ms |
23132 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
39 ms |
30160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
23384 KB |
Output is correct |
2 |
Correct |
4 ms |
23132 KB |
Output is correct |
3 |
Correct |
4 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
4 ms |
23128 KB |
Output is correct |
7 |
Correct |
4 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23132 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23132 KB |
Output is correct |
11 |
Correct |
4 ms |
23132 KB |
Output is correct |
12 |
Correct |
4 ms |
23132 KB |
Output is correct |
13 |
Correct |
3 ms |
23132 KB |
Output is correct |
14 |
Correct |
4 ms |
23132 KB |
Output is correct |
15 |
Correct |
4 ms |
23132 KB |
Output is correct |
16 |
Incorrect |
4 ms |
23188 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
41 ms |
30160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
23132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
23132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |