#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
const int maxn = 2e5 + 5;
vector<int> adj[maxn], nadj[maxn];
int in[maxn], low[maxn], sq[maxn], sz[maxn], dfn = 0, cnt = 0;
int n, m, ans = 0;
stack<int> st;
void dfs(int pos){
in[pos] = low[pos] = ++dfn;
st.push(pos);
for(auto x : adj[pos]){
if(in[x]) low[pos] = min(low[pos], in[x]);
else{
dfs(x);
if(low[x] >= in[pos]){
int cur = n++;
sq[cur] = 1, in[cur] = 1;
while(st.top() != pos){
nadj[cur].pb(st.top());
nadj[st.top()].pb(cur);
st.pop();
}
nadj[cur].pb(pos);
nadj[pos].pb(cur);
int sz = nadj[cur].size();
}
}
}
}
void dfs2(int pos, int prev){
sz[pos] = 1 - sq[pos];
for(auto x : nadj[pos]){
if(x == prev) continue;
dfs2(x, pos);
sz[pos] += sz[x];
}
}
void dfs3(int pos, int prev, int tsz){
// cout<<"d : "<<tsz<<" "<<pos<<" "<<sz[pos]<<"\n";
if(!sq[pos]){
ans += (tsz - 1) * (tsz - 1);
ans -= (tsz - sz[pos]) * (tsz - sz[pos]);
for(auto x : nadj[pos]){
if(x == prev) continue;
ans -= sz[x] * sz[x];
}
}
else if(nadj[pos].size() >= 3){
int lft = tsz - sz[pos], mid = nadj[pos].size() - 2;
ans += tsz * tsz * mid;
ans -= lft * lft * mid;
for(auto x : nadj[pos]){
if(x == prev) continue;
ans -= sz[x] * sz[x] * mid;
}
}
for(auto x : nadj[pos]){
if(x == prev) continue;
dfs3(x, pos, tsz);
}
}
signed main(void){
fastio;
cin>>n>>m;
for(int i = 0; i < m; i++){
int a, b;
cin>>a>>b;
a--, b--;
adj[a].pb(b);
adj[b].pb(a);
}
for(int i = 0; i < n; i++){
if(!in[i]){
dfs(i);
dfs2(i, -1);
dfs3(i, -1, sz[i]);
}
}
cout<<ans<<"\n";
}
Compilation message
count_triplets.cpp: In function 'void dfs(int)':
count_triplets.cpp:33:21: warning: unused variable 'sz' [-Wunused-variable]
33 | int sz = nadj[cur].size();
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12804 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Incorrect |
3 ms |
12636 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12804 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Incorrect |
3 ms |
12636 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
77 ms |
37972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12888 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12892 KB |
Output is correct |
5 |
Correct |
3 ms |
12888 KB |
Output is correct |
6 |
Correct |
3 ms |
12980 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12888 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
4 ms |
12892 KB |
Output is correct |
19 |
Correct |
3 ms |
12888 KB |
Output is correct |
20 |
Correct |
3 ms |
12888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
59 ms |
22356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12888 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Incorrect |
3 ms |
12892 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
64 ms |
22572 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12804 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Incorrect |
3 ms |
12636 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12804 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Incorrect |
3 ms |
12636 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |