#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b){a = b; return true;}
return false;
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b){a = b; return true;}
return false;
}
template <class T>
void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
for(auto i: a) out << i << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 1e5 + 69;
int n, m;
vector<int> graph[N];
int num[N], low[N];
bool isJoint[N];
vector<int> st;
int dfs_cnt;
vector<vector<int>> bbc;
void dfs(int u, int p){
num[u] = low[u] = ++dfs_cnt;
st.push_back(u);
vector<int> child;
for(int v: graph[u]) {
if (v == p) {p = -1; continue;}
if (num[v]){
minimize(low[u], num[v]);
}
else{
child.push_back(v);
dfs(v, u);
minimize(low[u], low[v]);
if (low[v] >= num[u]){
isJoint[u] = true;
}
}
}
if (u == p) isJoint[u] = isJoint[u] && (child.size() >= 2);
if (!isJoint[u]) return;
while(child.size()){
int v = child.back();
child.pop_back();
bbc.push_back({});
while(bbc.back().empty() || bbc.back().back() != v){
bbc.back().push_back(st.back());
st.pop_back();
}
bbc.back().push_back(u);
}
}
vector<int> bc_tree[2*N];
int joint_cnt = 0;
int pos[2*N];
int sz[2 * N];
int generate_block_cut_tree(){
dfs_cnt = 0;
for(int i= 1; i<=n; ++i) if (num[i] == 0){
dfs(i, i);
if (st.size()) {
bbc.push_back(st);
st.clear();
}
}
int idx = 0;
for(int i= 1; i<=n; ++i){
if (!isJoint[i]) continue;
joint_cnt++;
pos[i] = ++idx;
sz[idx] = -1;
}
for(vector<int> i: bbc){
++idx;
sz[idx] = i.size();
for(int j: i){
if (isJoint[j]){
int u = idx, v = pos[j];
bc_tree[u].push_back(v);
bc_tree[v].push_back(u);
}
}
}
return idx;
}
bool visited[N * 2];
int og[N * 2];
ll ans = 0;
void get_sz(int u, int p){
visited[u] = 1;
og[u] = sz[u];
for(int v: bc_tree[u]) if (v != p){
get_sz(v, u);
sz[u] += sz[v];
}
}
void dfs(int u, int p, int source){
if (u <= joint_cnt){
vector<pair<int, int>> child;
for(int v: bc_tree[u]){
if (v != p) child.push_back({sz[v] - 1, v});
else child.push_back({sz[source] - sz[u] - 1, v});
}
int sum = 0;
for(pair<int, int> i: child) {
sum += i.first;
}
// sum -= child.back();
// child.pop_back();
// child.push_back(sz[source] - sz[u] - 1);
// sum += child.back();
for(pair<int, int> i: child){
ans -= 1LL * (og[i.second] - 1) * (sum - i.first) * (sum - i.first - 1);
ans -= 2LL * (og[i.second] - 1) * (sum - i.first);
}
}
for(int v: bc_tree[u]) if (v != p){
dfs(v, u, source);
}
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
// if (m != n - 1) return -1;
for(int i= 0; i<m; ++i){
int u, v; cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
int tree_sz = generate_block_cut_tree();
for(int i= joint_cnt + 1; i<=tree_sz; ++i) if (!visited[i]){
get_sz(i, i);
int M = sz[i];
ans += 1LL * M * (M - 1) * (M - 2);
dfs(i, i, i);
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10764 KB |
Output is correct |
7 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10764 KB |
Output is correct |
7 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
32204 KB |
Output is correct |
2 |
Correct |
57 ms |
32220 KB |
Output is correct |
3 |
Incorrect |
71 ms |
35068 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
10840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
25088 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
10772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
65 ms |
25536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10764 KB |
Output is correct |
7 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10764 KB |
Output is correct |
7 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |