//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;
struct DSU{
vector<int>rep, sz;
DSU(int n){
rep.assign(n+1, 0);
iota(rep.begin(), rep.end(), 0);
sz.assign(n+1, 1);
}
int f(int a){
return a == rep[a] ? a : rep[a] = f(rep[a]);
}
void u(int a, int b){
a = f(a); b = f(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
rep[b] = a;
}
};
void solve(){
int n, m; cin >> n >> m;
vector<vector<T>>g(n+1);
vector<T>edges;
vector<bool>skip(m);
for (int i = 0; i<m; i++){
int a, b; cin >> a >> b;
g[a].emplace_back(b, i);
g[b].emplace_back(a, i);
edges.emplace_back(a, b);
}
vector<int>low(n+1), pre(n+1);
int tt = 0;
vector<bool>vis(n+1);
function<void(int, int)>dfs = [&](int v, int pa){
pre[v] = ++tt;
low[v] = pre[v];
vis[v] = 1;
for (auto [x, i]: g[v]){
if (x == pa) continue;
if (vis[x]){
low[v] = min(low[v], pre[x]);
} else {
// debug(v, x);
dfs(x, v);
low[v] = min(low[v], low[x]);
// debug(v, pre[v], low[x]);
if (pre[v] < low[x]){
debug(x, v);
skip[i] = 1;
}
}
}
};
dfs(1, 1);
DSU dsu(n+1);
for (int i = 0; i<m; i++){
if (!skip[i]){
dsu.u(edges[i].first, edges[i].second);
}
}
vector<vector<int>>G(n+1);
vector<int>sz(n+1);
for (int i = 0; i<m; i++){
if (!skip[i]) continue;
//most
auto [a, b] = edges[i];
a = dsu.f(a);b = dsu.f(b);
G[a].emplace_back(b);
G[b].emplace_back(a);
}
int r = -1;
for (int i = 1; i<=n; i++){
if (r == -1 && dsu.f(i) == i) r = i;
sz[dsu.f(i)] = dsu.sz[dsu.f(i)];
}
debug(sz);
int ans = 0, ans2 = 0;
function<void(int, int)>dfessa = [&](int v, int pa){
int sq = 0, s = 0;
for (auto x: G[v]){
if (x == pa) continue;
dfessa(x, v);
sq += sz[x] * sz[x];
s += sz[x];
}
//do rodzica
sq += (n-s-sz[v])*(n-s-sz[v]);
s = n-sz[v];
debug(v, 2 * sz[v] * (sz[v]-1) * s);
ans += 2 * sz[v] * (s*s-sq)/2; //jeden znajduje sie tutaj
ans += 2 * (sz[v]-2) * (sz[v]-1) * s; //dwa znajduja sie w jednej spojnej
ans += 2 * s * (sz[v]-1);
ans += sz[v] * (sz[v]-1) * (sz[v]-2);
debug(v, ans);
for (auto x: G[v]){
if (x != pa){
sz[v] += sz[x];
}
}
};
debug(r);
dfessa(r, r);
debug(ans, ans2);
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
Compilation message
count_triplets.cpp: In function 'void solve()':
count_triplets.cpp:111:18: warning: unused variable 'ans2' [-Wunused-variable]
111 | int ans = 0, ans2 = 0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
29652 KB |
Output is correct |
2 |
Correct |
55 ms |
29636 KB |
Output is correct |
3 |
Incorrect |
44 ms |
23236 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
720 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
20660 KB |
Output is correct |
2 |
Correct |
56 ms |
20676 KB |
Output is correct |
3 |
Correct |
59 ms |
20660 KB |
Output is correct |
4 |
Correct |
55 ms |
20712 KB |
Output is correct |
5 |
Correct |
53 ms |
20672 KB |
Output is correct |
6 |
Correct |
69 ms |
26780 KB |
Output is correct |
7 |
Correct |
65 ms |
25280 KB |
Output is correct |
8 |
Correct |
66 ms |
24260 KB |
Output is correct |
9 |
Correct |
63 ms |
22768 KB |
Output is correct |
10 |
Incorrect |
46 ms |
19032 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
20676 KB |
Output is correct |
2 |
Correct |
64 ms |
20584 KB |
Output is correct |
3 |
Incorrect |
62 ms |
20584 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |