#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz
#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y)
{
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y)
{
x = y;
return true;
}
return false;
}
const int MOD = 1e9 + 7; //998244353
template<class X, class Y>
void add(X &x, const Y &y) {
x = (x + y);
if(x >= MOD) x -= MOD;
}
template<class X, class Y>
void sub(X &x, const Y &y) {
x = (x - y);
if(x < 0) x += MOD;
}
/* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
const ll INF = 1e9;
const int N = 1e5 + 10;
vector<int> tree[N], g[N], adj[N];
int Time = 0;
int Low[N], Num[N], in[N], out[N], sz[N], up_node[N], depth[N];
vector<int> node;
void dfs(int u, int par) {
Low[u] = Num[u] = ++Time;
in[u] = Time;
sz[u] = 1;
node.pb(u);
for(auto v : g[u]) {
if(v != par) {
if(Num[v]) {
if(minimize(Low[u], Num[v])) up_node[u] = v;
if(Num[v] < Num[u]) adj[v].pb(u);
} else {
tree[u].pb(v);
tree[v].pb(u);
depth[v] = depth[u] + 1;
dfs(v, u);
sz[u] += sz[v];
if(minimize(Low[u], Low[v])) up_node[u] = up_node[v];
}
}
}
out[u] = Time;
}
template <class T> struct FenwickTree {
int n;
vector<T> bit;
FenwickTree(int _n = 0) {
n = _n;
bit.resize(n + 5);
for(int i = 1; i <= n; i++) bit[i] = 0;
}
void update(int pos, T x) {
for(int i = pos; i <= n; i += i & (-i)) bit[i] += x;
}
T get(int pos) {
T ans = 0;
for(int i = pos; i > 0; i -= i & (-i)) ans += bit[i];
return ans;
}
};
void solve() {
int n, m; cin >> n >> m;
for(int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
FenwickTree<int> BIT = FenwickTree<int>(n);
ll ans = 0;
for(int i = 1; i <= n; i++) {
if(!Num[i]) {
depth[1] = 1;
node.clear();
dfs(i, 0);
int c = node.size();
for(auto u : node) {
ans += 1ll * (c - sz[u]) * (sz[u] - 1);
if(up_node[u]) {
ans += 1ll * (c - sz[u]) * (c - sz[u] - 1) / 2;
int v = up_node[u];
ans -= 1ll * (c - sz[v] + 1) * (c - sz[v]) / 2;
}
ans += 1ll * (sz[u] - 1) * (sz[u] - 2) / 2;
sort(all(adj[u]), [&](int x, int y) {
return depth[x] > depth[y];
});
for(auto v : adj[u]) {
if(BIT.get(out[v]) - BIT.get(in[v] - 1) == 0) {
ans -= 1ll * sz[v] * (sz[v] - 1) / 2;
}
BIT.update(in[v], 1);
}
for(auto v : tree[u]) {
if(depth[v] > depth[u] && (BIT.get(out[v]) - BIT.get(in[v] - 1)) == 0) {
ans -= 1ll * sz[v] * (sz[v] - 1) / 2;
}
}
for(auto v : adj[u]) BIT.update(in[v], -1);
}
}
}
cout << ans * 2 << '\n';
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tc = 1, ddd = 0;
// cin >> tc;
while(tc--) {
//ddd++;
//cout << "Case #" << ddd << ": ";
solve();
}
}
Compilation message
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:173:17: warning: unused variable 'ddd' [-Wunused-variable]
173 | int tc = 1, ddd = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
106 ms |
27692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7516 KB |
Output is correct |
2 |
Correct |
4 ms |
7508 KB |
Output is correct |
3 |
Correct |
4 ms |
7508 KB |
Output is correct |
4 |
Correct |
5 ms |
7516 KB |
Output is correct |
5 |
Correct |
5 ms |
7508 KB |
Output is correct |
6 |
Correct |
5 ms |
7508 KB |
Output is correct |
7 |
Correct |
5 ms |
7508 KB |
Output is correct |
8 |
Correct |
6 ms |
7508 KB |
Output is correct |
9 |
Correct |
4 ms |
7424 KB |
Output is correct |
10 |
Correct |
5 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7516 KB |
Output is correct |
12 |
Correct |
5 ms |
7508 KB |
Output is correct |
13 |
Correct |
5 ms |
7508 KB |
Output is correct |
14 |
Correct |
5 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7376 KB |
Output is correct |
16 |
Correct |
4 ms |
7384 KB |
Output is correct |
17 |
Correct |
4 ms |
7508 KB |
Output is correct |
18 |
Correct |
4 ms |
7512 KB |
Output is correct |
19 |
Correct |
5 ms |
7508 KB |
Output is correct |
20 |
Correct |
5 ms |
7508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
18116 KB |
Output is correct |
2 |
Correct |
85 ms |
18252 KB |
Output is correct |
3 |
Correct |
84 ms |
18208 KB |
Output is correct |
4 |
Correct |
93 ms |
18120 KB |
Output is correct |
5 |
Correct |
80 ms |
18108 KB |
Output is correct |
6 |
Correct |
97 ms |
23132 KB |
Output is correct |
7 |
Correct |
91 ms |
21372 KB |
Output is correct |
8 |
Correct |
91 ms |
20552 KB |
Output is correct |
9 |
Correct |
89 ms |
19700 KB |
Output is correct |
10 |
Correct |
93 ms |
17948 KB |
Output is correct |
11 |
Correct |
81 ms |
18088 KB |
Output is correct |
12 |
Correct |
96 ms |
17940 KB |
Output is correct |
13 |
Correct |
94 ms |
18028 KB |
Output is correct |
14 |
Correct |
79 ms |
17420 KB |
Output is correct |
15 |
Correct |
73 ms |
17084 KB |
Output is correct |
16 |
Correct |
47 ms |
15324 KB |
Output is correct |
17 |
Correct |
56 ms |
18740 KB |
Output is correct |
18 |
Correct |
64 ms |
18704 KB |
Output is correct |
19 |
Correct |
68 ms |
19004 KB |
Output is correct |
20 |
Correct |
66 ms |
18792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7516 KB |
Output is correct |
2 |
Correct |
5 ms |
7508 KB |
Output is correct |
3 |
Incorrect |
5 ms |
7416 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
18212 KB |
Output is correct |
2 |
Correct |
103 ms |
18076 KB |
Output is correct |
3 |
Incorrect |
117 ms |
19184 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |