#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <algorithm>
using namespace std;
const int N = 100005;
int n, m, u, v, nod, cnt = 0, sz[2 * N], sub[2 * N], num[N], low[N];
long long ans = 0;
bool vis[2 * N];
vector<int> st, com[N], adj[N], cut[2 * N];
vector<vector<int>> bi;
void process(vector<int> &ve)
{
sort(ve.begin(), ve.end());
for (int i = 0; i < ve.size() - 1; i++)
assert(ve[i] < ve[i + 1]);
ve.erase(unique(ve.begin(), ve.end()), ve.end());
++nod;
for (int &v : ve)
{
sz[nod]++;
com[v].push_back(nod);
}
}
void DFS(int u, int p = 0)
{
low[u] = num[u] = ++cnt;
st.push_back(u);
for (int &v : adj[u])
if (v != p)
{
if (num[v] > 0)
low[u] = min(low[u], num[v]);
else
{
DFS(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= num[u])
{
int v; bi.push_back({});
do
{
v = st.back(); st.pop_back();
bi.back().push_back(v);
} while (u != v);
process(bi.back());
st.push_back(u);
}
}
}
}
int get_subtree(int u, int p = 0)
{
sub[u] = sz[u];
for (int &v : cut[u])
if (v != p)
sub[u] += get_subtree(v, u);
return sub[u];
}
void find_ans(int u, int tot)
{
vis[u] = true;
int ver = cut[u].size(), big = u > n;
ans += 1LL * sz[u] * (sz[u] - 1) * (ver * big + sz[u] - 2);
ans += 2LL * sz[u] * ((ver - 1) * big + sz[u] - 1) * (tot - sz[u]);
int cur = tot - sub[u];
for (int &v : cut[u])
if (!vis[v])
{
find_ans(v, tot);
ans += 2LL * cur * ((ver - 2) * big + sz[u]) * sub[v];
cur += sub[v];
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
nod = n;
while (m--)
{
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++)
if (num[i] == 0)
DFS(i);
for (int i = 1; i <= n; i++)
if (com[i].size() > 1)
{
sz[i] = 1;
for (int &v : com[i])
{
sz[v]--;
cut[i].push_back(v);
cut[v].push_back(i);
}
}
for (int i = 1; i <= nod; i++)
if (!vis[i])
find_ans(i, get_subtree(i));
cout << ans;
}
Compilation message
count_triplets.cpp: In function 'void process(std::vector<int>&)':
count_triplets.cpp:19:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < ve.size() - 1; i++)
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
13 ms |
9728 KB |
Output is correct |
3 |
Correct |
12 ms |
9728 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
12 ms |
9728 KB |
Output is correct |
6 |
Correct |
12 ms |
9728 KB |
Output is correct |
7 |
Incorrect |
12 ms |
9728 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
13 ms |
9728 KB |
Output is correct |
3 |
Correct |
12 ms |
9728 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
12 ms |
9728 KB |
Output is correct |
6 |
Correct |
12 ms |
9728 KB |
Output is correct |
7 |
Incorrect |
12 ms |
9728 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
25932 KB |
Output is correct |
2 |
Correct |
146 ms |
25860 KB |
Output is correct |
3 |
Correct |
247 ms |
29588 KB |
Output is correct |
4 |
Correct |
223 ms |
27308 KB |
Output is correct |
5 |
Correct |
231 ms |
25556 KB |
Output is correct |
6 |
Correct |
271 ms |
29280 KB |
Output is correct |
7 |
Correct |
277 ms |
27948 KB |
Output is correct |
8 |
Correct |
298 ms |
28392 KB |
Output is correct |
9 |
Correct |
310 ms |
26788 KB |
Output is correct |
10 |
Correct |
271 ms |
26036 KB |
Output is correct |
11 |
Correct |
184 ms |
22824 KB |
Output is correct |
12 |
Correct |
175 ms |
22600 KB |
Output is correct |
13 |
Correct |
162 ms |
22192 KB |
Output is correct |
14 |
Correct |
162 ms |
21800 KB |
Output is correct |
15 |
Correct |
146 ms |
20140 KB |
Output is correct |
16 |
Correct |
159 ms |
19936 KB |
Output is correct |
17 |
Correct |
18 ms |
11572 KB |
Output is correct |
18 |
Correct |
18 ms |
11660 KB |
Output is correct |
19 |
Correct |
17 ms |
11528 KB |
Output is correct |
20 |
Correct |
15 ms |
11656 KB |
Output is correct |
21 |
Correct |
16 ms |
11648 KB |
Output is correct |
22 |
Correct |
14 ms |
11492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9984 KB |
Output is correct |
2 |
Correct |
11 ms |
9984 KB |
Output is correct |
3 |
Correct |
13 ms |
9984 KB |
Output is correct |
4 |
Correct |
11 ms |
9984 KB |
Output is correct |
5 |
Correct |
12 ms |
9984 KB |
Output is correct |
6 |
Correct |
11 ms |
9984 KB |
Output is correct |
7 |
Correct |
12 ms |
9984 KB |
Output is correct |
8 |
Correct |
12 ms |
9984 KB |
Output is correct |
9 |
Correct |
13 ms |
9984 KB |
Output is correct |
10 |
Correct |
13 ms |
9984 KB |
Output is correct |
11 |
Correct |
14 ms |
9984 KB |
Output is correct |
12 |
Correct |
12 ms |
9984 KB |
Output is correct |
13 |
Correct |
13 ms |
9984 KB |
Output is correct |
14 |
Correct |
10 ms |
9984 KB |
Output is correct |
15 |
Correct |
14 ms |
9984 KB |
Output is correct |
16 |
Correct |
11 ms |
9856 KB |
Output is correct |
17 |
Correct |
10 ms |
9984 KB |
Output is correct |
18 |
Correct |
11 ms |
9984 KB |
Output is correct |
19 |
Correct |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
10 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
298 ms |
29028 KB |
Output is correct |
2 |
Correct |
310 ms |
29208 KB |
Output is correct |
3 |
Correct |
321 ms |
29088 KB |
Output is correct |
4 |
Correct |
310 ms |
29228 KB |
Output is correct |
5 |
Correct |
259 ms |
29092 KB |
Output is correct |
6 |
Correct |
417 ms |
37364 KB |
Output is correct |
7 |
Correct |
437 ms |
34272 KB |
Output is correct |
8 |
Correct |
403 ms |
33692 KB |
Output is correct |
9 |
Correct |
379 ms |
32136 KB |
Output is correct |
10 |
Correct |
288 ms |
29148 KB |
Output is correct |
11 |
Correct |
299 ms |
29088 KB |
Output is correct |
12 |
Correct |
249 ms |
29092 KB |
Output is correct |
13 |
Correct |
298 ms |
29204 KB |
Output is correct |
14 |
Correct |
319 ms |
27808 KB |
Output is correct |
15 |
Correct |
212 ms |
26396 KB |
Output is correct |
16 |
Correct |
130 ms |
21340 KB |
Output is correct |
17 |
Correct |
146 ms |
27976 KB |
Output is correct |
18 |
Correct |
153 ms |
27960 KB |
Output is correct |
19 |
Correct |
142 ms |
28196 KB |
Output is correct |
20 |
Correct |
141 ms |
28044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
9984 KB |
Output is correct |
2 |
Correct |
12 ms |
10004 KB |
Output is correct |
3 |
Incorrect |
11 ms |
9984 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
297 ms |
29220 KB |
Output is correct |
2 |
Correct |
320 ms |
29224 KB |
Output is correct |
3 |
Incorrect |
332 ms |
27600 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
13 ms |
9728 KB |
Output is correct |
3 |
Correct |
12 ms |
9728 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
12 ms |
9728 KB |
Output is correct |
6 |
Correct |
12 ms |
9728 KB |
Output is correct |
7 |
Incorrect |
12 ms |
9728 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
13 ms |
9728 KB |
Output is correct |
3 |
Correct |
12 ms |
9728 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
12 ms |
9728 KB |
Output is correct |
6 |
Correct |
12 ms |
9728 KB |
Output is correct |
7 |
Incorrect |
12 ms |
9728 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |