#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5, MOD = 998244353;
#define int ll
int n,m,tin[N],fup[N],timer;
bool vis[N];
vector<int> g[N];
void dfs(int v,int pr = -1){
vis[v]=1;
fup[v] = tin[v] = ++timer;
for(int to:g[v]){
if(to == pr) continue;
if(!vis[to]){
dfs(to,v);
fup[v] = min(fup[v],fup[to]);
}else{
fup[v] = min(fup[v],tin[to]);
}
}
}
int mxc=0,c[N];
ll sz[N];
void paint(int v,int cl){
c[v] = cl;
sz[cl]++;
for(int to:g[v]){
if(!c[to]){
if(fup[to] > tin[v]){
mxc++;
paint(to,mxc);
}else{
paint(to,cl);
}
}
}
}
vector<int> G[N];
int P[N];
int get(int v){
if(P[v] == v) return v;
return P[v]= get(P[v]);
}
void merge(int a,int b){
a = get(a);
b = get(b);
P[a] = b;
}
int sum[N];
void dfs2(int v,int pr = -1){
sum[v] = sz[v];
for(int to:G[v]){
if(to == pr) continue;
dfs2(to,v);
sum[v] += sum[to];
}
}
ll res = 0;
int n1;
void dfs3(int v,int pr = -1,int S = 0){
ll ss =0 ,ff = S - sum[v];
for(int to:G[v]){
if(to != pr){
dfs3(to,v,S);
ss += ff * 1ll * sum[to];
ff += sum[to];
}
}
ss *= 2;
res += ss * 1ll * sz[v];
}
int us[N],us1[N];
int S = 0;
void go1(int v,int pr= -1){
us1[v] = 1;
S++;
for(int to:g[v]){
if(!us1[to]){
go1(to,v);
}
}
}
void go(int v,int pr = -1) {
us[v] = 1;
ll ss = 0,ff = 0;
if(pr != -1 && c[pr] != c[v]){
ff = S - sum[c[v]];
}
for(int to:g[v]) {
if (to == pr) continue;
if(!us[to]){
go(to,v);
if(c[to] != c[v]){
ss += ff * sum[c[to]];
ff += sum[c[to]];
}
}
}
if(sz[c[v]] != 1){
ss *= 2;
// cout << v << ' ' << ss << '\n';
res -= ss * sz[v];
res += ss;
}
}
void test(){
cin >> n >> m;
n1 = n;
for(int i = 1;i <= m;i++){
int a,b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i = 1;i <= n;i++){
P[i] = i;
if(!vis[i]){
dfs(i);
}
}
for(int i = 1;i <= n;i++){
if(!c[i]){
mxc++;
paint(i,mxc);
}
}
for(int i = 1;i <= n;i++){
for(int to:g[i]){
int x = c[i],y = c[to];
if(get(x)!=get(y)){
G[x].push_back(y);
G[y].push_back(x);
merge(x,y);
}
}
}
n = mxc;
for(int i = 1;i <= n;i++) {
vis[i]=0;
assert(sz[i]);
res += sz[i] * (sz[i] - 1) * (sz[i] - 2);
}
ll A = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(i == j || get(i)!=get(j)) continue;
A += sz[i] * sz[j] * (sz[j] - 1)/2 + (sz[j]-1) * (sz[j]-2) /2*sz[i];
}
}
res += A*2;
for(int i = 1;i <= n;i++){
if(!sum[i]){
dfs2(i);
dfs3(i,-1,sum[i]);
}
}
n = n1;
for(int i = 1;i <= n;i++){
if(!us1[i]){
S=0;
go1(i);
go(i);
}
}
cout << res << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int T = 1;
// cin >> T;
while (T--){
test();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20980 KB |
Output is correct |
3 |
Correct |
4 ms |
20824 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20980 KB |
Output is correct |
3 |
Correct |
4 ms |
20824 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
31900 KB |
Output is correct |
2 |
Correct |
89 ms |
31820 KB |
Output is correct |
3 |
Execution timed out |
1057 ms |
27928 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
20828 KB |
Output is correct |
2 |
Correct |
7 ms |
20828 KB |
Output is correct |
3 |
Correct |
7 ms |
20824 KB |
Output is correct |
4 |
Correct |
7 ms |
21300 KB |
Output is correct |
5 |
Correct |
7 ms |
21084 KB |
Output is correct |
6 |
Correct |
6 ms |
21084 KB |
Output is correct |
7 |
Correct |
10 ms |
21084 KB |
Output is correct |
8 |
Correct |
9 ms |
20824 KB |
Output is correct |
9 |
Correct |
7 ms |
20828 KB |
Output is correct |
10 |
Correct |
7 ms |
20828 KB |
Output is correct |
11 |
Correct |
7 ms |
20828 KB |
Output is correct |
12 |
Correct |
9 ms |
20828 KB |
Output is correct |
13 |
Correct |
6 ms |
20824 KB |
Output is correct |
14 |
Correct |
7 ms |
21060 KB |
Output is correct |
15 |
Correct |
7 ms |
21044 KB |
Output is correct |
16 |
Correct |
6 ms |
21024 KB |
Output is correct |
17 |
Correct |
6 ms |
20828 KB |
Output is correct |
18 |
Correct |
7 ms |
20828 KB |
Output is correct |
19 |
Correct |
7 ms |
21084 KB |
Output is correct |
20 |
Correct |
8 ms |
21084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1099 ms |
27180 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
20824 KB |
Output is correct |
2 |
Correct |
8 ms |
20828 KB |
Output is correct |
3 |
Incorrect |
5 ms |
20828 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1049 ms |
26872 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20980 KB |
Output is correct |
3 |
Correct |
4 ms |
20824 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20980 KB |
Output is correct |
3 |
Correct |
4 ms |
20824 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |